-
Notifications
You must be signed in to change notification settings - Fork 99
/
init.go
60 lines (44 loc) · 1.15 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
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
)
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(v ...int) {
MainCron = cron.New()
for i,option := range v {
functions[i].(func(int))(option)
}
MainCron.Start()
fmt.Printf("%s[JobRunner] %v Started... %s \n",
magenta, time.Now().Format("2006/01/02 - 15:04:05"), reset)
}