forked from ninjasphere/app-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (76 loc) · 1.92 KB
/
main.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
package main
import (
"fmt"
"github.com/ninjasphere/app-scheduler/model"
"github.com/ninjasphere/app-scheduler/rest"
"github.com/ninjasphere/app-scheduler/service"
"github.com/ninjasphere/app-scheduler/ui"
"github.com/ninjasphere/go-ninja/api"
nmodel "github.com/ninjasphere/go-ninja/model"
"github.com/ninjasphere/go-ninja/support"
)
var (
info = ninja.LoadModuleInfo("./package.json")
)
type postConstructable interface {
PostConstruct() error
}
//SchedulerApp describes the scheduler application.
type SchedulerApp struct {
support.AppSupport
service *service.SchedulerService
restServer rest.RestServer
configService *ui.ConfigService
}
// Start is called after the ExportApp call is complete.
func (a *SchedulerApp) Start(m *model.Schedule) error {
if a.service != nil {
return fmt.Errorf("illegal state: scheduler is already running")
}
m = m.Migrate()
m.Version = Version
a.SendEvent("config", m)
a.service = &service.SchedulerService{
Log: a.Log,
Conn: a.Conn,
Model: m,
ConfigStore: func(m *model.Schedule) {
a.SendEvent("config", m)
},
}
err := a.service.Init(a.Info.ID)
if err != nil {
return err
}
a.restServer.Scheduler = a.service
go a.restServer.Start()
a.configService = ui.NewConfigService(a.service, a.Conn)
a.Conn.MustExportService(a.configService, "$app/"+a.GetModuleInfo().ID+"/configure", &nmodel.ServiceAnnouncement{
Schema: "/protocol/configuration",
})
return nil
}
// Stop the scheduler module.
func (a *SchedulerApp) Stop() error {
var err error
// TODO: stop the REST server
if a.service != nil {
tmp := a.service
a.service = nil
a.restServer.Stop()
err = tmp.Scheduler.Stop()
}
return err
}
func main() {
app := &SchedulerApp{}
err := app.Init(info)
if err != nil {
app.Log.Fatalf("failed to initialize app: %v", err)
}
err = app.Export(app)
if err != nil {
app.Log.Fatalf("failed to export app: %v", err)
}
support.WaitUntilSignal()
}