Exemple #1
0
// RunGC will run gc in the cycleTime interval, collect the queue set data after jobLifetime seconds
func (d *Dispatcher) RunGC(queue *Queue, cycleTime, jobLifetime time.Duration) {
	if cycleTime > 0 && jobLifetime > 0 {
		go func() {
			for {
				time.Sleep(cycleTime * time.Second)

				remCount, err := queue.GC(jobLifetime)

				if err != nil {
					slog.SimpleLog(d.LogName, "ERROR", map[string]interface{}{
						"method":    "Dispatcher.runGC",
						"queueName": queue.Name,
						"error":     err,
					})
				}

				if remCount > 0 {
					slog.SimpleLog(d.LogName, "INFO", map[string]interface{}{
						"method":    "Dispatcher.runGC",
						"queueName": queue.Name,
						"remCount":  remCount,
					})
				}
			}
		}()
	}
}
Exemple #2
0
// pop queue's job and do it in parallel
func (d *Dispatcher) Do(queue *Queue, workerNum int, doJobFunc jobFunc) {
	ch := make(chan bool, workerNum)
	for {
		ch <- true
		go func() {
			defer d.errDefer()
			var (
				job []byte
				err error
			)

			for {
				job, err = queue.Pop()
				if err != nil {
					if err.Error() != "empty" {
						slog.SimpleLog(d.LogName, "ERROR", map[string]interface{}{
							"method":    "queue.Pop",
							"queueName": queue.Name,
							"error":     err,
						})
					}
					time.Sleep(d.CheckQueueInterval * time.Second)
				} else {
					break
				}
			}

			err = d.doJob(job, doJobFunc)

			if err != nil {
				slog.SimpleLog(d.LogName, "ERROR", map[string]interface{}{
					"method":    "Dispatcher.doJob",
					"queueName": queue.Name,
					"job":       string(job),
					"error":     err,
				})
				if err = d.retry(job, doJobFunc); err != nil {
					slog.SimpleLog(d.LogName, "ERROR", map[string]interface{}{
						"method":    "Dispatcher.retry",
						"queueName": queue.Name,
						"job":       string(job),
						"error":     err,
					})
				}
			}

			if err = queue.Complete(job); err != nil {
				slog.SimpleLog(d.LogName, "ERROR", map[string]interface{}{
					"method":    "Dispatcher.Complete",
					"queueName": queue.Name,
					"job":       string(job),
					"error":     err,
				})
			}
			<-ch
		}()
	}
}
Exemple #3
0
// errDefer log the error and avoid crash
func (d *Dispatcher) errDefer() {
	if r := recover(); r != nil {
		trace := make([]byte, 1<<16)
		n := runtime.Stack(trace, true)
		slog.SimpleLog(d.LogName, "ERROR", map[string]interface{}{
			"method": "Dispatcher.errDefer",
			"error":  r,
			"trace":  string(trace[:int(math.Min(float64(n), float64(7000)))]),
		})
	}
}