forked from go-co-op/gocron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
1028 lines (859 loc) · 24.4 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gocron_test
import (
"fmt"
"log"
"time"
"github.com/go-co-op/gocron"
)
var task = func() {}
// ---------------------------------------------------------------------
// ----------------------JOB-FUNCTIONS----------------------------------
// ---------------------------------------------------------------------
func ExampleJob_Error() {
s := gocron.NewScheduler(time.UTC)
_, err := s.Every(1).Day().At("bad time").Do(func() {})
fmt.Println(err)
// Output:
// gocron: the given time format is not supported
}
func ExampleJob_FinishedRunCount() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
go func() {
for {
fmt.Println("Count of finished job runs", job.FinishedRunCount())
time.Sleep(time.Second)
}
}()
s.StartAsync()
}
func ExampleJob_IsRunning() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(10).Seconds().Do(func() { time.Sleep(2 * time.Second) })
fmt.Println(j.IsRunning())
s.StartAsync()
time.Sleep(time.Second)
fmt.Println(j.IsRunning())
time.Sleep(time.Second)
s.Stop()
time.Sleep(1 * time.Second)
fmt.Println(j.IsRunning())
// Output:
// false
// true
// false
}
func ExampleJob_LastRun() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
s.StartAsync()
fmt.Println("Last run:", job.LastRun())
}
func ExampleJob_LimitRunsTo() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
job.LimitRunsTo(2)
s.StartAsync()
}
func ExampleJob_NextRun() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
go func() {
for {
fmt.Println("Next run", job.NextRun())
time.Sleep(time.Second)
}
}()
s.StartAsync()
}
func ExampleJob_PreviousRun() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
s.StartAsync()
fmt.Println("Previous run:", job.PreviousRun())
}
func ExampleJob_RegisterEventListeners() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every("1s").Name("my_func").Do(func() error { return fmt.Errorf("error") })
job.RegisterEventListeners(
gocron.AfterJobRuns(func(jobName string) {
fmt.Printf("afterJobRuns: %s\n", jobName)
}),
gocron.BeforeJobRuns(func(jobName string) {
fmt.Printf("beforeJobRuns: %s\n", jobName)
}),
gocron.WhenJobReturnsError(func(jobName string, err error) {
fmt.Printf("whenJobReturnsError: %s, %v\n", jobName, err)
}),
gocron.WhenJobReturnsNoError(func(jobName string) {
fmt.Printf("whenJobReturnsNoError: %s\n", jobName)
}),
)
s.StartAsync()
time.Sleep(100 * time.Millisecond)
s.Stop()
// Output:
// beforeJobRuns: my_func
// whenJobReturnsError: my_func, error
// afterJobRuns: my_func
}
func ExampleJob_RunCount() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
go func() {
for {
fmt.Println("Run count", job.RunCount())
time.Sleep(time.Second)
}
}()
s.StartAsync()
}
func ExampleJob_ScheduledAtTime() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Day().At("10:30").Do(task)
s.StartAsync()
fmt.Println(job.ScheduledAtTime())
// if multiple times are set, the earliest time will be returned
job1, _ := s.Every(1).Day().At("10:30;08:00").Do(task)
fmt.Println(job1.ScheduledAtTime())
// Output:
// 10:30
// 08:00
}
func ExampleJob_ScheduledAtTimes() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Day().At("10:30;08:00").Do(task)
s.StartAsync()
fmt.Println(job.ScheduledAtTimes())
// Output:
// [08:00 10:30]
}
func ExampleJob_ScheduledTime() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Day().At("10:30").Do(task)
s.StartAsync()
fmt.Println(job.ScheduledTime())
}
func ExampleJob_SetEventListeners() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Week().Do(task)
job.SetEventListeners(func() {}, func() {})
}
func ExampleJob_SingletonMode() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every(1).Second().Do(task)
job.SingletonMode()
}
func ExampleJob_Tag() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every("1s").Do(task)
job.Tag("tag1", "tag2", "tag3")
s.StartAsync()
fmt.Println(job.Tags())
// Output:
// [tag1 tag2 tag3]
}
func ExampleJob_Tags() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every("1s").Do(task)
job.Tag("tag1", "tag2", "tag3")
s.StartAsync()
fmt.Println(job.Tags())
// Output:
// [tag1 tag2 tag3]
}
func ExampleJob_Untag() {
s := gocron.NewScheduler(time.UTC)
job, _ := s.Every("1s").Do(task)
job.Tag("tag1", "tag2", "tag3")
s.StartAsync()
fmt.Println(job.Tags())
job.Untag("tag2")
fmt.Println(job.Tags())
// Output:
// [tag1 tag2 tag3]
// [tag1 tag3]
}
func ExampleJob_Weekday() {
s := gocron.NewScheduler(time.UTC)
weeklyJob, _ := s.Every(1).Week().Monday().Do(task)
weekday, _ := weeklyJob.Weekday()
fmt.Println(weekday)
dailyJob, _ := s.Every(1).Day().Do(task)
_, err := dailyJob.Weekday()
fmt.Println(err)
// Output:
// Monday
// gocron: job not scheduled weekly on a weekday
}
func ExampleJob_Weekdays() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Week().Monday().Wednesday().Friday().Do(task)
fmt.Println(j.Weekdays())
// Output:
// [Monday Wednesday Friday]
}
// ---------------------------------------------------------------------
// -------------------SCHEDULER-FUNCTIONS-------------------------------
// ---------------------------------------------------------------------
func ExampleScheduler_At() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("10:30").Do(task)
_, _ = s.Every(1).Monday().At("10:30:01").Do(task)
// multiple
_, _ = s.Every(1).Monday().At("10:30;18:00").Do(task)
_, _ = s.Every(1).Monday().At("10:30").At("18:00").Do(task)
}
func ExampleScheduler_ChangeLocation() {
s := gocron.NewScheduler(time.UTC)
fmt.Println(s.Location())
location, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
log.Fatalf("Error loading location: %s", err)
}
s.ChangeLocation(location)
fmt.Println(s.Location())
// Output:
// UTC
// America/Los_Angeles
}
func ExampleScheduler_Clear() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().Do(task)
_, _ = s.Every(1).Minute().Do(task)
_, _ = s.Every(1).Month(1).Do(task)
fmt.Println(len(s.Jobs())) // Print the number of jobs before clearing
s.Clear() // Clear all the jobs
fmt.Println(len(s.Jobs())) // Print the number of jobs after clearing
s.StartAsync()
// Output:
// 3
// 0
}
func ExampleScheduler_Cron() {
s := gocron.NewScheduler(time.UTC)
// parsing handled by https://pkg.go.dev/github.com/robfig/cron/v3
// which follows https://en.wikipedia.org/wiki/Cron
_, _ = s.Cron("*/1 * * * *").Do(task) // every minute
_, _ = s.Cron("0 1 * * *").Do(task) // every day at 1 am
_, _ = s.Cron("0 0 * * 6,0").Do(task) // weekends only
}
func ExampleScheduler_CronWithSeconds() {
s := gocron.NewScheduler(time.UTC)
// parsing handled by https://pkg.go.dev/github.com/robfig/cron/v3
// which follows https://en.wikipedia.org/wiki/Cron
_, _ = s.CronWithSeconds("*/1 * * * * *").Do(task) // every second
_, _ = s.CronWithSeconds("0-30 * * * * *").Do(task) // every second 0-30
}
func ExampleScheduler_CustomTime() {
// Implement your own custom time struct
//
// type myCustomTime struct{}
//
// var _ gocron.TimeWrapper = (*myCustomTime)(nil)
//
// func (m myCustomTime) Now(loc *time.Location) time.Time {
// panic("implement me")
// }
//
// func (m myCustomTime) Sleep(duration time.Duration) {
// panic("implement me")
// }
//
// func (m myCustomTime) Unix(sec int64, nsec int64) time.Time {
// panic("implement me")
// }
//
// mct := myCustomTime{}
//
// s := gocron.NewScheduler(time.UTC)
// s.CustomTime(mct)
}
func ExampleScheduler_CustomTimer() {
s := gocron.NewScheduler(time.UTC)
s.CustomTimer(func(d time.Duration, f func()) *time.Timer {
// force jobs with 1 minute interval to run every second
if d == time.Minute {
d = time.Second
}
return time.AfterFunc(d, f)
})
// this job will run every 1 second
_, _ = s.Every("1m").Do(task)
}
func ExampleScheduler_Day() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("24h").Do(task)
_, _ = s.Every(1).Day().Do(task)
_, _ = s.Every(1).Days().Do(task)
}
func ExampleScheduler_Days() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("24h").Do(task)
_, _ = s.Every(1).Day().Do(task)
_, _ = s.Every(1).Days().Do(task)
}
func ExampleScheduler_Do() {
s := gocron.NewScheduler(time.UTC)
j1, err := s.Every(1).Second().Do(task)
fmt.Printf("Job: %v, Error: %v", j1, err)
taskWithParameters := func(param1, param2 string) {}
j2, err := s.Every(1).Second().Do(taskWithParameters, "param1", "param2")
fmt.Printf("Job: %v, Error: %v", j2, err)
s.StartAsync()
}
func ExampleScheduler_DoWithJobDetails() {
task := func(in string, job gocron.Job) {
fmt.Printf("this job's last run: %s this job's next run: %s\n", job.LastRun(), job.NextRun())
fmt.Printf("in argument is %s\n", in)
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-job.Context().Done():
fmt.Printf("function has been canceled, performing cleanup and exiting gracefully\n")
return
case <-ticker.C:
fmt.Printf("performing a hard job that takes a long time that I want to kill whenever I want\n")
}
}
}
var err error
s := gocron.NewScheduler(time.UTC)
s.SingletonModeAll()
j, err := s.Every(1).Hour().Tag("myJob").DoWithJobDetails(task, "foo")
if err != nil {
log.Fatalln("error scheduling job", err)
}
s.StartAsync()
// Simulate some more work
time.Sleep(time.Second)
// I want to stop the job, together with the underlying goroutine
fmt.Printf("now I want to kill the job\n")
err = s.RemoveByTag("myJob")
if err != nil {
log.Fatalln("error removing job by tag", err)
}
// Wait a bit so that we can see that the job is exiting gracefully
time.Sleep(time.Second)
fmt.Printf("Job: %#v, Error: %#v", j, err)
}
func ExampleScheduler_Every() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().Do(task)
_, _ = s.Every(1 * time.Second).Do(task)
_, _ = s.Every("1s").Do(task)
s.StartAsync()
}
func ExampleScheduler_EveryRandom() {
s := gocron.NewScheduler(time.UTC)
// every 1 - 5 seconds randomly
_, _ = s.EveryRandom(1, 5).Seconds().Do(task)
// every 5 - 10 hours randomly
_, _ = s.EveryRandom(5, 10).Hours().Do(task)
s.StartAsync()
}
func ExampleScheduler_Friday() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Friday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
// Output:
// Friday
}
func ExampleScheduler_GetAllTags() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().Tag("tag1").Do(task)
_, _ = s.Every(1).Second().Tag("tag2").Do(task)
fmt.Println(s.GetAllTags())
}
func ExampleScheduler_Hour() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1h").Do(task)
_, _ = s.Every(1).Hour().Do(task)
_, _ = s.Every(1).Hours().Do(task)
}
func ExampleScheduler_Hours() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1h").Do(task)
_, _ = s.Every(1).Hour().Do(task)
_, _ = s.Every(1).Hours().Do(task)
}
func ExampleScheduler_IsRunning() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1s").Do(task)
fmt.Println(s.IsRunning())
s.StartAsync()
fmt.Println(s.IsRunning())
// Output:
// false
// true
}
func ExampleScheduler_Job() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every("1s").Do(func() {})
s.StartAsync()
time.Sleep(10 * time.Second)
_, _ = s.Job(j).Every("10m").Update()
time.Sleep(30 * time.Minute)
_, _ = s.Job(j).Every(1).Day().At("02:00").Update()
}
func ExampleScheduler_Jobs() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
fmt.Println(len(s.Jobs()))
// Output:
// 3
}
func ExampleScheduler_JobsMap() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
fmt.Printf("map of job uuid to job: %+v", s.JobsMap())
}
func ExampleScheduler_Len() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
_, _ = s.Every("1s").Do(task)
fmt.Println(s.Len())
// Output:
// 3
}
func ExampleScheduler_LimitRunsTo() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Second().LimitRunsTo(1).Do(task)
s.StartAsync()
time.Sleep(time.Millisecond * 100)
fmt.Println(j.RunCount())
// Output:
// 1
}
func ExampleScheduler_Location() {
s := gocron.NewScheduler(time.UTC)
fmt.Println(s.Location())
// Output:
// UTC
}
func ExampleScheduler_Midday() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().Midday().Do(task)
s.StartAsync()
}
func ExampleScheduler_Millisecond() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Millisecond().Do(task)
_, _ = s.Every(1).Milliseconds().Do(task)
_, _ = s.Every("1ms").Seconds().Do(task)
_, _ = s.Every(time.Millisecond).Seconds().Do(task)
}
func ExampleScheduler_Milliseconds() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Millisecond().Do(task)
_, _ = s.Every(1).Milliseconds().Do(task)
_, _ = s.Every("1ms").Seconds().Do(task)
_, _ = s.Every(time.Millisecond).Seconds().Do(task)
}
func ExampleScheduler_Minute() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1m").Do(task)
_, _ = s.Every(1).Minute().Do(task)
_, _ = s.Every(1).Minutes().Do(task)
}
func ExampleScheduler_Minutes() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1m").Do(task)
_, _ = s.Every(1).Minute().Do(task)
_, _ = s.Every(1).Minutes().Do(task)
}
func ExampleScheduler_Monday() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Monday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
// Output:
// Monday
}
func ExampleScheduler_Month() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Month().Do(task)
_, _ = s.Every(1).Month(1).Do(task)
_, _ = s.Every(1).Months(1).Do(task)
_, _ = s.Every(1).Month(1, 2).Do(task)
_, _ = s.Month(1, 2).Every(1).Do(task)
_, _ = s.Every(1).Month(-1).Do(task)
_, _ = s.Every(1).Month(-2).Do(task)
}
func ExampleScheduler_MonthFirstWeekday() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.MonthFirstWeekday(time.Monday).Do(task)
s.StartAsync()
}
func ExampleScheduler_MonthLastDay() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).MonthLastDay().Do(task)
_, _ = s.Every(2).MonthLastDay().Do(task)
_, _ = s.Every(1).MonthLastDay(-1).Do(task)
_, _ = s.Every(1).MonthLastDay(-2).Do(task)
}
func ExampleScheduler_Months() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Month(1).Do(task)
_, _ = s.Every(1).Months(1).Do(task)
_, _ = s.Every(1).Months(-1).Do(task)
_, _ = s.Every(1).Months(-2).Do(task)
}
func ExampleScheduler_NextRun() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("10:30").Do(task)
s.StartAsync()
s.NextRun()
}
func ExampleScheduler_PauseJobExecution() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every("1s").Do(task)
s.StartAsync()
s.PauseJobExecution(true)
// jobs don't run
s.PauseJobExecution(false)
// jobs run again
}
func ExampleScheduler_RegisterEventListeners() {
s := gocron.NewScheduler(time.UTC)
s.Every("1s").Name("my_func_1").Do(func() error { return fmt.Errorf("error_1") })
s.Every("1s").Name("my_func_2").
StartAt(time.Now().UTC().Add(50 * time.Millisecond)).
Do(func() error { return fmt.Errorf("error_2") })
s.RegisterEventListeners(
gocron.AfterJobRuns(func(jobName string) {
fmt.Printf("afterJobRuns: %s\n", jobName)
}),
gocron.BeforeJobRuns(func(jobName string) {
fmt.Printf("beforeJobRuns: %s\n", jobName)
}),
gocron.WhenJobReturnsError(func(jobName string, err error) {
fmt.Printf("whenJobReturnsError: %s, %v\n", jobName, err)
}),
gocron.WhenJobReturnsNoError(func(jobName string) {
fmt.Printf("whenJobReturnsNoError: %s\n", jobName)
}),
)
s.StartAsync()
time.Sleep(120 * time.Millisecond)
s.Stop()
// Output:
// beforeJobRuns: my_func_1
// whenJobReturnsError: my_func_1, error_1
// afterJobRuns: my_func_1
// beforeJobRuns: my_func_2
// whenJobReturnsError: my_func_2, error_2
// afterJobRuns: my_func_2
}
func ExampleScheduler_Remove() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Do(task)
s.StartAsync()
s.Remove(task)
fmt.Println(s.Len())
// Output:
// 0
}
func ExampleScheduler_RemoveByID() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Week().Do(task)
_, _ = s.Every(1).Week().Do(task)
s.StartAsync()
s.RemoveByID(j)
fmt.Println(s.Len())
// Output:
// 1
}
func ExampleScheduler_RemoveByReference() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Week().Do(task)
_, _ = s.Every(1).Week().Do(task)
s.StartAsync()
s.RemoveByReference(j)
fmt.Println(s.Len())
// Output:
// 1
}
func ExampleScheduler_RemoveByTag() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Tag("tag1").Do(task)
_, _ = s.Every(1).Week().Tag("tag2").Do(task)
s.StartAsync()
_ = s.RemoveByTag("tag1")
fmt.Println(s.Len())
// Output:
// 1
}
func ExampleScheduler_RemoveByTags() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Tag("tag1", "tag2", "tag3").Do(task)
_, _ = s.Every(1).Week().Tag("tag1", "tag2").Do(task)
_, _ = s.Every(1).Week().Tag("tag1").Do(task)
s.StartAsync()
_ = s.RemoveByTags("tag1", "tag2")
fmt.Println(s.Len())
// Output:
// 1
}
func ExampleScheduler_RemoveByTagsAny() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Tag("tag1", "tag2", "tag3").Do(task)
_, _ = s.Every(1).Week().Tag("tag1").Do(task)
_, _ = s.Every(1).Week().Tag("tag2").Do(task)
_, _ = s.Every(1).Week().Tag("tag3").Do(task)
s.StartAsync()
_ = s.RemoveByTagsAny("tag1", "tag3")
fmt.Println(s.Len())
// Output:
// 1
}
func ExampleScheduler_RunAll() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("10:00").Do(task)
_, _ = s.Every(2).Day().At("10:00").Do(task)
_, _ = s.Every(3).Day().At("10:00").Do(task)
s.StartAsync()
s.RunAll()
}
func ExampleScheduler_RunAllWithDelay() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("10:00").Do(task)
_, _ = s.Every(2).Day().At("10:00").Do(task)
_, _ = s.Every(3).Day().At("10:00").Do(task)
s.StartAsync()
s.RunAllWithDelay(10 * time.Second)
}
func ExampleScheduler_RunByTag() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("10:00").Do(task)
_, _ = s.Every(2).Day().Tag("tag").At("10:00").Do(task)
s.StartAsync()
_ = s.RunByTag("tag")
}
func ExampleScheduler_RunByTagWithDelay() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().Tag("tag").At("10:00").Do(task)
_, _ = s.Every(2).Day().Tag("tag").At("10:00").Do(task)
s.StartAsync()
_ = s.RunByTagWithDelay("tag", 2*time.Second)
}
func ExampleScheduler_Saturday() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Saturday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
// Output:
// Saturday
}
func ExampleScheduler_Second() {
s := gocron.NewScheduler(time.UTC)
// the default unit is seconds
// these are all the same
_, _ = s.Every(1).Do(task)
_, _ = s.Every(1).Second().Do(task)
_, _ = s.Every(1).Seconds().Do(task)
_, _ = s.Every("1s").Seconds().Do(task)
_, _ = s.Every(time.Second).Seconds().Do(task)
}
func ExampleScheduler_Seconds() {
s := gocron.NewScheduler(time.UTC)
// the default unit is seconds
// these are all the same
_, _ = s.Every(1).Do(task)
_, _ = s.Every(1).Second().Do(task)
_, _ = s.Every(1).Seconds().Do(task)
_, _ = s.Every("1s").Seconds().Do(task)
_, _ = s.Every(time.Second).Seconds().Do(task)
}
func ExampleScheduler_SetMaxConcurrentJobs() {
s := gocron.NewScheduler(time.UTC)
s.SetMaxConcurrentJobs(1, gocron.RescheduleMode)
_, _ = s.Every(1).Seconds().Do(func() {
fmt.Println("This will run once every 5 seconds even though it is scheduled every second because maximum concurrent job limit is set.")
time.Sleep(5 * time.Second)
})
}
func ExampleScheduler_SingletonMode() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().SingletonMode().Do(task)
}
func ExampleScheduler_SingletonModeAll() {
s := gocron.NewScheduler(time.UTC)
s.SingletonModeAll()
_, _ = s.Every(1).Second().Do(task)
}
func ExampleScheduler_StartAsync() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(3).Seconds().Do(task)
s.StartAsync()
}
func ExampleScheduler_StartAt() {
s := gocron.NewScheduler(time.UTC)
specificTime := time.Date(2019, time.November, 10, 15, 0, 0, 0, time.UTC)
_, _ = s.Every(1).Hour().StartAt(specificTime).Do(task)
s.StartBlocking()
}
func ExampleScheduler_StartBlocking() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(3).Seconds().Do(task)
s.StartBlocking()
}
func ExampleScheduler_StartImmediately() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Cron("0 0 * * 6,0").StartImmediately().Do(task)
s.StartBlocking()
}
func ExampleScheduler_Stop() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Second().Do(task)
s.StartAsync()
s.Stop()
fmt.Println(s.IsRunning())
s = gocron.NewScheduler(time.UTC)
go func() {
time.Sleep(1 * time.Second)
s.Stop()
}()
s.StartBlocking()
fmt.Println(".Stop() stops the blocking start")
// Output:
// false
// .Stop() stops the blocking start
}
func ExampleScheduler_Sunday() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Sunday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
// Output:
// Sunday
}
func ExampleScheduler_Tag() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Week().Tag("tag").Do(task)
fmt.Println(j.Tags())
// Output:
// [tag]
}
func ExampleScheduler_TagsUnique() {
s := gocron.NewScheduler(time.UTC)
s.TagsUnique()
_, _ = s.Every(1).Week().Tag("foo").Do(task)
_, err := s.Every(1).Week().Tag("foo").Do(task)
fmt.Println(err)
// Output:
// gocron: a non-unique tag was set on the job: foo
}
func ExampleScheduler_TaskPresent() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Do(task)
fmt.Println(s.TaskPresent(task))
// Output:
// true
}
func ExampleScheduler_Thursday() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Thursday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
// Output:
// Thursday
}
func ExampleScheduler_Tuesday() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Tuesday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
// Output:
// Tuesday
}
func ExampleScheduler_Update() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every("1s").Do(task)
s.StartAsync()
time.Sleep(10 * time.Second)
_, _ = s.Job(j).Every("10m").Update()
time.Sleep(30 * time.Minute)
_, _ = s.Job(j).Every(1).Day().At("02:00").Update()
}
func ExampleScheduler_WaitForSchedule() {
s := gocron.NewScheduler(time.UTC)
// job will run 5 minutes from the scheduler starting
_, _ = s.Every("5m").WaitForSchedule().Do(task)
// job will run immediately and 5 minutes from the scheduler starting
_, _ = s.Every("5m").Do(task)
s.StartAsync()
}
func ExampleScheduler_WaitForScheduleAll() {
s := gocron.NewScheduler(time.UTC)
s.WaitForScheduleAll()
// all jobs will run 5 minutes from the scheduler starting
_, _ = s.Every("5m").Do(task)
_, _ = s.Every("5m").Do(task)
s.StartAsync()
}
func ExampleScheduler_Wednesday() {
s := gocron.NewScheduler(time.UTC)
j, _ := s.Every(1).Day().Wednesday().Do(task)
s.StartAsync()
wd, _ := j.Weekday()
fmt.Println(wd)
// Output:
// Wednesday
}
func ExampleScheduler_Week() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Do(task)
_, _ = s.Every(1).Weeks().Do(task)
_, _ = s.Every(1).Week().Monday().Wednesday().Friday().Do(task)
}
func ExampleScheduler_Weekday() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Weekday(time.Monday).Do(task)
_, _ = s.Every(1).Weeks().Weekday(time.Tuesday).Weekday(time.Friday).Do(task)
}
func ExampleScheduler_Weeks() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Week().Do(task)
_, _ = s.Every(1).Weeks().Do(task)
_, _ = s.Every(2).Weeks().Monday().Wednesday().Friday().Do(task)
}