forked from micro/micro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
216 lines (185 loc) · 4.38 KB
/
run.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Package run is a micro service runtime
package run
import (
"fmt"
"time"
"github.com/micro/cli"
"github.com/micro/go-log"
"github.com/micro/go-micro"
"github.com/micro/go-micro/client"
"github.com/micro/go-run"
"github.com/micro/go-run/runtime/go"
proto "github.com/micro/micro/run/proto"
"golang.org/x/net/context"
)
var (
Name = "go.micro.run"
)
func manage(r run.Runtime, url string, re, u bool) error {
// get the source
log.Logf("fetching %s\n", url)
src, err := r.Fetch(url, run.Update(u))
if err != nil {
return err
}
// build the binary
log.Logf("building %s\n", url)
bin, err := r.Build(src)
if err != nil {
return err
}
for {
// execute the binary
log.Logf("executing %s\n", url)
proc, err := r.Exec(bin)
if err != nil {
return err
}
// wait till exit
log.Logf("running %s\n", url)
// bail if not restarting
if !re {
return r.Wait(proc)
}
// log error since we manage the cycle
if err := r.Wait(proc); err != nil {
log.Logf("exited with err %v\n", err)
}
// cruft log
log.Logf("restarting %s\n", url)
}
}
func runc(ctx *cli.Context) {
if len(ctx.GlobalString("server_name")) > 0 {
Name = ctx.GlobalString("server_name")
}
// Init plugins
for _, p := range Plugins() {
p.Init(ctx)
}
r := gorun.NewRuntime()
// micro run github.com/my/service
// args: github.com/my/service
if len(ctx.Args()) > 0 {
// Notes to self:
// 1. Done: look for flag to loop run cycle infinitely
// 2. Done: look for flag to defer execution to go.micro.run service
// 3. look for daemonize flag
// 4. Done: look for flag to defer update
ki := ctx.Bool("k")
st := ctx.Bool("s")
re := ctx.Bool("r")
up := ctx.Bool("u")
xe := ctx.Bool("x")
// kill a service
if ki {
// call runtime manager service
cl := proto.NewServiceClient(Name, client.DefaultClient)
_, err := cl.Stop(context.TODO(), &proto.StopRequest{
Url: ctx.Args().First(),
})
if err != nil {
fmt.Println(err)
}
return
}
// get status
if st {
// call runtime manager service
cl := proto.NewServiceClient(Name, client.DefaultClient)
rsp, err := cl.Status(context.TODO(), &proto.StatusRequest{
Url: ctx.Args().First(),
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(rsp.Info)
return
}
// defer to service
if xe {
// call runtime manager service
cl := proto.NewServiceClient(Name, client.DefaultClient)
_, err := cl.Run(context.TODO(), &proto.RunRequest{
Url: ctx.Args().First(),
Restart: re,
Update: up,
})
if err != nil {
fmt.Println(err)
}
return
}
// manage the process locally
if err := manage(r, ctx.Args().First(), re, up); err != nil {
fmt.Println(err)
}
// its a cli command, return
return
}
// Initialise Server
service := micro.NewService(
micro.Name(Name),
micro.RegisterTTL(
time.Duration(ctx.GlobalInt("register_ttl"))*time.Second,
),
micro.RegisterInterval(
time.Duration(ctx.GlobalInt("register_interval"))*time.Second,
),
)
m := newManager(r)
prun := micro.NewPublisher(Name+".run", service.Client())
pstop := micro.NewPublisher(Name+".stop", service.Client())
// handlers
proto.RegisterRuntimeHandler(service.Server(), &runtimeHandler{r})
proto.RegisterServiceHandler(service.Server(), &serviceHandler{m, prun, pstop})
// subscribers
sb := &subHandler{m}
// run commands
micro.RegisterSubscriber(Name+".run", service.Server(), sb.Run)
// stop commands
micro.RegisterSubscriber(Name+".stop", service.Server(), sb.Stop)
// Run server
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
func Commands() []cli.Command {
command := cli.Command{
Name: "run",
Usage: "Run the micro runtime",
Action: runc,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "k",
Usage: "Kill service",
},
cli.BoolFlag{
Name: "r",
Usage: "Restart if dies. Default: false",
},
cli.BoolFlag{
Name: "u",
Usage: "Update the source. Default: false",
},
cli.BoolFlag{
Name: "x",
Usage: "Defer run to service. Default: false",
},
cli.BoolFlag{
Name: "s",
Usage: "Get service status",
},
},
}
for _, p := range Plugins() {
if cmds := p.Commands(); len(cmds) > 0 {
command.Subcommands = append(command.Subcommands, cmds...)
}
if flags := p.Flags(); len(flags) > 0 {
command.Flags = append(command.Flags, flags...)
}
}
return []cli.Command{command}
}