forked from bamzi/jobrunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
75 lines (58 loc) · 1.74 KB
/
init.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
package jobrunner
import (
"fmt"
"time"
"github.com/robfig/cron/v3"
)
const DEFAULT_JOB_POOL_SIZE = 10
var (
// Singleton instance of the underlying job scheduler.
MainCron *cron.Cron
// This limits the number of jobs allowed to run concurrently.
workPermits chan struct{}
// Is a single job allowed to run concurrently with itself?
selfConcurrent bool
IsStoreExecutionStatus bool
JobsExecutionStatusChan chan *JobStatus
)
var (
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
reset = string([]byte{27, 91, 48, 109})
functions = []interface{}{makeWorkPermits, isSelfConcurrent}
)
func makeWorkPermits(bufferCapacity int) {
if bufferCapacity <= 0 {
workPermits = make(chan struct{}, DEFAULT_JOB_POOL_SIZE)
} else {
workPermits = make(chan struct{}, bufferCapacity)
}
}
func isSelfConcurrent(cocnurrencyFlag int) {
if cocnurrencyFlag <= 0 {
selfConcurrent = false
} else {
selfConcurrent = true
}
}
func Start(loc *time.Location, isStoreExecutionStatus bool, v ...int) {
if loc == nil {
MainCron = cron.New(cron.WithParser(cron.NewParser(
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
)))
} else {
MainCron = cron.New(cron.WithLocation(loc), cron.WithParser(cron.NewParser(
cron.SecondOptional|cron.Minute|cron.Hour|cron.Dom|cron.Month|cron.Dow|cron.Descriptor,
)))
}
for i, option := range v {
functions[i].(func(int))(option)
}
IsStoreExecutionStatus = isStoreExecutionStatus
if IsStoreExecutionStatus {
JobsExecutionStatusChan = make(chan *JobStatus)
}
MainCron.Start()
fmt.Printf("%s[JobRunner] %v Started... %s \n",
magenta, time.Now().Format("2006/01/02 - 15:04:05"), reset)
}