jobs.go

 1// Package jobs provides background job processing functionality.
 2package jobs
 3
 4import (
 5	"context"
 6	"sync"
 7)
 8
 9// Job is a job that can be registered with the scheduler.
10type Job struct {
11	ID     int
12	Runner Runner
13}
14
15// Runner is a job runner.
16type Runner interface {
17	Spec(context.Context) string
18	Func(context.Context) func()
19}
20
21var (
22	mtx  sync.Mutex
23	jobs = make(map[string]*Job, 0)
24)
25
26// Register registers a job.
27func Register(name string, runner Runner) {
28	mtx.Lock()
29	defer mtx.Unlock()
30	jobs[name] = &Job{Runner: runner}
31}
32
33// List returns a map of registered jobs.
34func List() map[string]*Job {
35	mtx.Lock()
36	defer mtx.Unlock()
37	return jobs
38}