示例#1
0
func init() {
	jobstatus.RedisClient = datastore.RedisClient()
}
示例#2
0
func init() {
	workers.Process("guide-upload-video-job", UploadVideoJob, 1)
	jobstatus.RedisClient = datastore.RedisClient()
}
示例#3
0
func GenerateStreamJob(msg *workers.Msg) {
	jobStartTime := time.Now().Unix()
	jobStartTimeString := strconv.FormatInt(jobStartTime, 10)
	identifier, iderr := msg.Args().Get("identifier").String()
	if identifier == "" || iderr != nil {
		log.Println("No identifier! aborting!")
		return
	}

	client := datastore.RedisClient()
	lockid := "guide-stream-lock-" + identifier
	gotLock, err := client.SetNX(lockid, jobStartTimeString, 3*time.Second).Result()
	if !gotLock || err != nil {
		return
	}

	ticker := time.NewTicker(1 * time.Second)
	cancelChan := make(chan bool, 3)
	go func() {
		for {
			select {
			case <-ticker.C:
				client.Set(lockid, jobStartTimeString, 3*time.Second)
			case <-cancelChan:
				ticker.Stop()
				client.Del(lockid)
				return
			}
		}
	}()
	defer func() {
		cancelChan <- true
	}()

	stream, err := generator.LoadStream(identifier)
	if err != nil {
		log.Println("could not load given identifier", identifier, "aborting!")
		return
	}

	freebies := 0
	if stream.Playlist.Count() == 0 {
		freebies = 3
	}

	out := stream.Generate()
	for command := range out {
		if command.Type == "broadcast-segment" {
			durationMs := time.Duration(command.Segment.TargetDuration * 1000000)
			if freebies > 0 {
				durationMs = 0
				freebies = freebies - 1
			}
			canAdvance := time.After(durationMs * time.Microsecond)
			stream.ToChannel().SavePlaylist()
			<-canAdvance
		}
	}
	cancelChan <- true

	workers.Enqueue("guide-generate-stream-job", "GenerateStreamJob", map[string]string{
		"identifier": identifier,
	})
}