Submit enqueues the given job with the given name. If name is non-empty and a job with the same name is already enqueued or running, this is a no-op. If name is empty, no duplicate prevention will occur. The job manager will then run this job as soon as it is able.
(logger *zap.Logger, name string, job func() error)
| 32 | // no-op. If name is empty, no duplicate prevention will occur. The job |
| 33 | // manager will then run this job as soon as it is able. |
| 34 | func (jm *jobManager) Submit(logger *zap.Logger, name string, job func() error) { |
| 35 | jm.mu.Lock() |
| 36 | defer jm.mu.Unlock() |
| 37 | if jm.names == nil { |
| 38 | jm.names = make(map[string]struct{}) |
| 39 | } |
| 40 | if name != "" { |
| 41 | // prevent duplicate jobs |
| 42 | if _, ok := jm.names[name]; ok { |
| 43 | return |
| 44 | } |
| 45 | jm.names[name] = struct{}{} |
| 46 | } |
| 47 | jm.queue = append(jm.queue, namedJob{name, job, logger}) |
| 48 | if jm.activeWorkers < jm.maxConcurrentJobs { |
| 49 | jm.activeWorkers++ |
| 50 | go jm.worker() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func (jm *jobManager) worker() { |
| 55 | defer func() { |