MCPcopy Index your code
hub / github.com/DoNewsCode/core / Add

Method Add

cron/cron.go:54–86  ·  view source on GitHub ↗

Add adds a new job to the cron scheduler.A list of middleware can be supplied. Note the error returned by the runner will be discarded. It is the user's responsibility to handle the error via middleware.

(spec string, runner func(ctx context.Context) error, middleware ...JobOption)

Source from the content-addressed store, hash-verified

52// Note the error returned by the runner will be discarded. It is the user's
53// responsibility to handle the error via middleware.
54func (c *Cron) Add(spec string, runner func(ctx context.Context) error, middleware ...JobOption) (JobID, error) {
55 schedule, err := c.parser.Parse(spec)
56 if err != nil {
57 return 0, err
58 }
59
60 jobDescriptor := JobDescriptor{
61 RawSpec: spec,
62 Run: runner,
63 Schedule: schedule,
64 next: schedule.Next(c.now()),
65 }
66
67 middleware = append(c.globalMiddleware, middleware...)
68
69 for i := len(middleware) - 1; i >= 0; i-- {
70 middleware[i](&jobDescriptor)
71 }
72
73 c.lock.L.Lock()
74 defer c.lock.L.Unlock()
75 defer c.lock.Broadcast()
76
77 jobDescriptor.ID = JobID(c.nextID)
78 c.nextID++
79
80 if jobDescriptor.Name == "" {
81 jobDescriptor.Name = fmt.Sprintf("job-%d", jobDescriptor.ID)
82 }
83
84 heap.Push(&c.jobDescriptors, &jobDescriptor)
85 return jobDescriptor.ID, nil
86}
87
88// Remove removes a job from the cron scheduler.
89func (c *Cron) Remove(id JobID) {

Callers 15

TestServeIn_signalWatchFunction · 0.45
ProvideCronMethod · 0.45
newServeCmdFunction · 0.45
CloseMethod · 0.45
ParseMethod · 0.45
TestJobOptionFunction · 0.45
FailMethod · 0.45
ProvideCronMethod · 0.45
RunMethod · 0.45
NextMethod · 0.45
TestCron_heapsortFunction · 0.45

Calls 6

nowMethod · 0.95
JobIDTypeAlias · 0.85
ParseMethod · 0.80
PushMethod · 0.80
SprintfMethod · 0.65
NextMethod · 0.45

Tested by 13

TestServeIn_signalWatchFunction · 0.36
ProvideCronMethod · 0.36
ParseMethod · 0.36
TestJobOptionFunction · 0.36
ProvideCronMethod · 0.36
NextMethod · 0.36
TestCron_heapsortFunction · 0.36
TestCron_late_jobFunction · 0.36
TestCron_remove_jobFunction · 0.36
TestMetrics_data_racesFunction · 0.36
TestModule_WatchFunction · 0.36