forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
42 lines (33 loc) · 863 Bytes
/
plugin.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
package jobs
import (
"fmt"
"github.com/robfig/cron"
"github.com/robfig/revel"
)
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
)
type JobsPlugin struct {
revel.EmptyPlugin
}
func (p JobsPlugin) OnAppStart() {
if size := revel.Config.IntDefault("jobs.pool", DEFAULT_JOB_POOL_SIZE); size > 0 {
workPermits = make(chan struct{}, size)
}
selfConcurrent = revel.Config.BoolDefault("jobs.selfconcurrent", false)
MainCron.Start()
fmt.Println("Go to /@jobs to see job status.")
}
func (p JobsPlugin) OnAppStop() {
MainCron.Stop()
}
func init() {
MainCron = cron.New()
revel.RegisterPlugin(JobsPlugin{})
}