-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
66 lines (61 loc) · 1.82 KB
/
cron.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
package builtin_libs
import (
. "github.com/zgg-lang/zgg-go/runtime"
cron "github.com/robfig/cron/v3"
)
var (
cronAppClass ValueType
)
func libCron(c *Context) ValueObject {
rv := NewObject()
rv.SetMember("App", cronGetAppClass(), c)
return rv
}
func cronGetAppClass() ValueType {
if cronAppClass == nil {
cronAppClass = NewClassBuilder("App").
Constructor(func(c *Context, this ValueObject, args []Value) {
app := cron.New()
this.SetMember("__app", NewGoValue(app), c)
}).
Method("add", func(c *Context, this ValueObject, args []Value) Value {
var (
spec ValueStr
callback ValueCallable
)
EnsureFuncParams(c, "App.add", args,
ArgRuleRequired("spec", TypeStr, &spec),
ArgRuleRequired("callback", TypeCallable, &callback),
)
app := this.GetMember("__app", c).ToGoValue().(*cron.Cron)
newContext := c.Clone()
id, err := app.AddFunc(spec.Value(), func() {
defer newContext.Recover()
newContext.Invoke(callback, nil, Args(this))
})
if err != nil {
c.RaiseRuntimeError("CronApp.add: add callback error: %s", err)
}
return NewInt(int64(id))
}).
Method("remove", func(c *Context, this ValueObject, args []Value) Value {
var jobId ValueInt
EnsureFuncParams(c, "App.remove", args, ArgRuleRequired("jobId", TypeInt, &jobId))
app := this.GetMember("__app", c).ToGoValue().(*cron.Cron)
app.Remove(cron.EntryID(jobId.AsInt()))
return this
}).
Method("start", func(c *Context, this ValueObject, args []Value) Value {
app := this.GetMember("__app", c).ToGoValue().(*cron.Cron)
app.Start()
return this
}).
Method("stop", func(c *Context, this ValueObject, args []Value) Value {
app := this.GetMember("__app", c).ToGoValue().(*cron.Cron)
app.Stop()
return this
}).
Build()
}
return cronAppClass
}