/
daemon.go
716 lines (636 loc) · 17.4 KB
/
daemon.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
package main
import (
"bytes"
"encoding/json"
"errors"
"expvar"
"flag"
"fmt"
"github.com/howeyc/fsnotify"
"github.com/runner-mei/cron"
"log"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"
"time"
)
var (
listenAddress = flag.String("listen", ":37075", "the address of http")
poll_interval = flag.Duration("poll_interval", 1*time.Minute, "the poll interval of db")
is_print = flag.Bool("print", false, "print search paths while config is not found")
root_dir = flag.String("root", ".", "the root directory")
config_file = flag.String("config", "./<program_name>.conf", "the config file path")
java_home = flag.String("java_home", "", "the path of java, should auto search if it is empty")
log_path = flag.String("log_path", "", "the path of log, should auto search if it is empty")
)
func fileExists(nm string) bool {
fs, e := os.Stat(nm)
if nil != e {
return false
}
return !fs.IsDir()
}
func dirExists(nm string) bool {
fs, e := os.Stat(nm)
if nil != e {
return false
}
return fs.IsDir()
}
// func usage() {
// program := filepath.Base(os.Args[0])
// fmt.Fprint(os.Stderr, program, ` [options]
// Options:
// `)
// flag.PrintDefaults()
// }
func abs(s string) string {
r, e := filepath.Abs(s)
if nil != e {
return s
}
return r
}
func main() {
flag.Parse()
if nil != flag.Args() && 0 != len(flag.Args()) {
flag.Usage()
return
}
if "." == *root_dir {
*root_dir = abs(filepath.Dir(os.Args[0]))
dirs := []string{abs(filepath.Dir(os.Args[0])), filepath.Join(abs(filepath.Dir(os.Args[0])), "..")}
for _, s := range dirs {
if dirExists(filepath.Join(s, "conf")) {
*root_dir = s
break
}
}
} else {
*root_dir = abs(*root_dir)
}
if !dirExists(*root_dir) {
log.Println("root directory '" + *root_dir + "' is not exist.")
return
} else {
log.Println("root directory is '" + *root_dir + "'.")
}
e := os.Chdir(*root_dir)
if nil != e {
log.Println("change current dir to \"" + *root_dir + "\"")
}
if 0 == len(*java_home) {
flag.Set("java_home", search_java_home(*root_dir))
log.Println("[warn] java is", *java_home)
}
arguments, e := loadConfig(*root_dir)
if nil != e {
log.Println(e)
return
}
flag.Set("log_path", ensureLogPath(*root_dir, arguments))
backend, e := newBackend(*db_drv, *db_url)
if nil != e {
log.Println(e)
return
}
job_directories := []string{filepath.Join(*root_dir, "lib", "jobs")}
jobs_from_dir, e := loadJobsFromDirectory(job_directories, arguments)
if nil != e {
log.Println(e)
return
}
jobs_from_db, e := loadJobsFromDB(backend, arguments)
if nil != e {
log.Println(e)
return
}
error_jobs := map[string]error{}
cr := cron.New()
for _, job := range jobs_from_dir {
sch, e := Parse(job.expression)
if nil != e {
error_jobs[job.name] = e
log.Println("["+job.name+"] schedule failed,", e)
continue
}
cr.Schedule(job.name, sch, job)
}
for _, job := range jobs_from_db {
sch, e := Parse(job.expression)
if nil != e {
e := errors.New("[" + job.name + "] schedule failed, " + e.Error())
error_jobs[fmt.Sprint(job.id)] = e
log.Println(e)
continue
}
cr.Schedule(fmt.Sprint(job.id), sch, job)
}
expvar.Publish("jobs", expvar.Func(func() interface{} {
ret := map[string]interface{}{}
for nm, e := range error_jobs {
ret[nm] = e.Error()
}
for _, ent := range cr.Entries() {
if export, ok := ent.Job.(Exportable); ok {
m := export.Stats()
m["next"] = ent.Next
m["prev"] = ent.Prev
ret[ent.Id] = m
} else {
ret[ent.Id] = map[string]interface{}{"next": ent.Next, "prev": ent.Prev}
}
}
bs, e := json.MarshalIndent(ret, "", " ")
if nil != e {
return e.Error()
}
rm := json.RawMessage(bs)
return &rm
}))
cr.Start()
defer cr.Stop()
watcher, e := fsnotify.NewWatcher()
if e != nil {
log.Println("[error] new fs watcher failed", e)
return
}
// Process events
go func() {
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev)
if ev.IsCreate() {
nm := strings.ToLower(filepath.Base(ev.Name))
log.Println("[sys] new job -", nm)
job, e := loadJobFromFile(ev.Name, arguments)
if nil != e {
error_jobs[nm] = e
log.Println("["+nm+"] schedule failed,", e)
break
}
sch, e := Parse(job.expression)
if nil != e {
error_jobs[job.name] = e
log.Println("["+job.name+"] schedule failed,", e)
break
}
cr.Schedule(job.name, sch, job)
} else if ev.IsDelete() {
nm := strings.ToLower(filepath.Base(ev.Name))
log.Println("[sys] delete job -", nm)
cr.Unschedule(nm)
delete(error_jobs, nm)
} else if ev.IsModify() {
nm := strings.ToLower(filepath.Base(ev.Name))
log.Println("[sys] reload job -", nm)
cr.Unschedule(nm)
delete(error_jobs, nm)
job, e := loadJobFromFile(ev.Name, arguments)
if nil != e {
error_jobs[nm] = e
log.Println("["+nm+"] schedule failed,", e)
break
}
sch, e := Parse(job.expression)
if nil != e {
error_jobs[job.name] = e
log.Println("["+job.name+"] schedule failed,", e)
break
}
cr.Schedule(job.name, sch, job)
}
case err := <-watcher.Error:
log.Println("error:", err)
case <-time.After(*poll_interval):
if e := reloadJobsFromDB(cr, error_jobs, backend, arguments); nil != e {
log.Println(e)
}
}
}
}()
for _, dir := range job_directories {
e = watcher.Watch(dir)
if e != nil {
log.Println("[sys] watch directory '"+dir+"' failed", e)
return
}
}
log.Println("[schd-jobs] serving at '" + *listenAddress + "'")
e = http.ListenAndServe(*listenAddress, nil)
if e != nil {
log.Fatal(e)
}
}
func reloadJobsFromDB(cr *cron.Cron, error_jobs map[string]error, backend *dbBackend, arguments map[string]interface{}) error {
jobs, e := backend.snapshot(nil)
if nil != e {
return errors.New("load snapshot from db failed, " + e.Error())
}
versions := map[int64]version{}
for _, v := range jobs {
versions[v.id] = v
}
for _, ent := range cr.Entries() {
if job, ok := ent.Job.(*JobFromDB); ok {
if v, ok := versions[job.id]; ok {
if !v.updated_at.Equal(job.updated_at) {
reloadJobFromDB(cr, error_jobs, backend, arguments, job.id, job.name)
}
delete(versions, job.id)
} else {
log.Println("[sys] delete job -", job.name)
cr.Unschedule(fmt.Sprint(job.id))
delete(error_jobs, fmt.Sprint(job.id))
}
}
}
for id, _ := range versions {
reloadJobFromDB(cr, error_jobs, backend, arguments, id, "")
}
return nil
}
func reloadJobFromDB(cr *cron.Cron, error_jobs map[string]error, backend *dbBackend, arguments map[string]interface{}, id int64, name string) {
message_prefix := "[sys] reload job -"
if "" == name {
message_prefix = "[sys] load new job -"
}
job, e := backend.find(id)
if nil != e {
if "" == name {
log.Println(message_prefix, "[", id, "]")
} else {
log.Println(message_prefix, name)
}
return
}
e = afterLoad(job, arguments)
if nil != e {
log.Println(message_prefix, job.name)
return
}
id_str := fmt.Sprint(id)
log.Println(message_prefix, job.name)
cr.Unschedule(id_str)
delete(error_jobs, id_str)
sch, e := Parse(job.expression)
if nil != e {
msg := errors.New("[" + job.name + "] schedule failed," + e.Error())
error_jobs[id_str] = msg
log.Println(msg)
return
}
cr.Schedule(id_str, sch, job)
}
func Parse(spec string) (sch cron.Schedule, e error) {
defer func() {
if o := recover(); nil != o {
e = errors.New(fmt.Sprint(o))
}
}()
return cron.Parse(spec), nil
}
func search_java_home(root string) string {
java_execute := "java.exe"
if "windows" != runtime.GOOS {
java_execute = "java"
}
jp := filepath.Join(root, "runtime_env/jdk/bin", java_execute)
if fileExists(jp) {
return jp
}
jp = filepath.Join(root, "runtime_env/jre/bin", java_execute)
if fileExists(jp) {
return jp
}
ss, _ := filepath.Glob(filepath.Join(root, "**", "java.exe"))
if nil != ss && 0 != len(ss) {
return ss[0]
}
jh := os.Getenv("JAVA_HOME")
if "" != jh {
return filepath.Join(jh, "bin", java_execute)
}
return java_execute
}
func loadJobsFromDB(backend *dbBackend, arguments map[string]interface{}) ([]*JobFromDB, error) {
jobs, e := backend.where(nil)
if nil != e {
return nil, e
}
for _, job := range jobs {
e = afterLoad(job, arguments)
if nil != e {
return nil, e
}
}
return jobs, nil
}
func afterLoad(job *JobFromDB, arguments map[string]interface{}) error {
is_java := false
if "java" == strings.ToLower(job.execute) || "java.exe" == strings.ToLower(job.execute) {
job.execute = *java_home
is_java = true
} else {
job.execute = executeTemplate(job.execute, arguments)
execute_tolow := strings.ToLower(job.execute)
if strings.HasSuffix(execute_tolow, "java") || strings.HasSuffix(execute_tolow, "java.exe") {
is_java = true
}
}
job.directory = executeTemplate(job.directory, arguments)
if nil != job.arguments {
for idx, s := range job.arguments {
job.arguments[idx] = executeTemplate(s, arguments)
}
if is_java {
for i := 0; i < len(job.arguments); i += 2 {
if (i + 1) == len(job.arguments) {
continue
}
if "-cp" == job.arguments[i] || "--classpath" == job.arguments[i] {
classpath, e := loadJavaClasspath(strings.Split(job.arguments[i+1], ";"))
if nil != e {
return errors.New("load classpath of '" + job.name + "' failed, " + e.Error())
}
if nil == classpath && 0 == len(classpath) {
return errors.New("load classpath of '" + job.name + "' failed, it is empty.")
}
if "windows" == runtime.GOOS {
job.arguments[i] = strings.Join(classpath, ";")
} else {
job.arguments[i] = strings.Join(classpath, ":")
}
}
}
}
job.logfile = filepath.Join(*log_path, "job_"+job.name+".log")
}
if nil != job.environments {
for idx, s := range job.environments {
job.environments[idx] = executeTemplate(s, arguments)
}
}
return nil
}
func loadJobsFromDirectory(roots []string, arguments map[string]interface{}) ([]*ShellJob, error) {
jobs := make([]*ShellJob, 0, 10)
for _, root := range roots {
matches, e := filepath.Glob(filepath.Join(root, "*.*"))
if nil != e {
return nil, errors.New("search '" + filepath.Join(root, "*.*") + "' failed, " + e.Error())
}
if nil == matches {
continue
}
for _, nm := range matches {
job, e := loadJobFromFile(nm, arguments)
if nil != e {
return nil, errors.New("load '" + nm + "' failed, " + e.Error())
} else {
log.Println("load '" + nm + "' is ok.")
}
jobs = append(jobs, job)
}
}
return jobs, nil
}
func ensureLogPath(root string, arguments map[string]interface{}) string {
logPath := filepath.Clean(abs(filepath.Join(root, "logs")))
logs := []string{stringWithDefault(arguments, "logPath", logPath),
filepath.Clean(abs(filepath.Join(root, "..", "logs"))),
logPath}
for _, s := range logs {
if dirExists(s) {
logPath = s
break
}
}
if !dirExists(logPath) {
os.Mkdir(logPath, 0660)
}
return logPath
}
func executeTemplate(s string, args map[string]interface{}) string {
if !strings.Contains(s, "{{") {
return s
}
var buffer bytes.Buffer
t, e := template.New("default").Parse(s)
if nil != e {
panic(errors.New("regenerate string failed, " + e.Error()))
}
e = t.Execute(&buffer, args)
if nil != e {
panic(errors.New("regenerate string failed, " + e.Error()))
}
return buffer.String()
}
func loadJobFromFile(file string, args map[string]interface{}) (*ShellJob, error) {
t, e := template.ParseFiles(file)
if nil != e {
return nil, errors.New("read file failed, " + e.Error())
}
args["cd_dir"] = filepath.Dir(file)
var buffer bytes.Buffer
e = t.Execute(&buffer, args)
if nil != e {
return nil, errors.New("regenerate file failed, " + e.Error())
}
var v interface{}
e = json.Unmarshal(buffer.Bytes(), &v)
if nil != e {
log.Println(buffer.String())
return nil, errors.New("ummarshal file failed, " + e.Error())
}
if value, ok := v.(map[string]interface{}); ok {
return loadJobFromMap(file, []map[string]interface{}{value, args})
}
return nil, fmt.Errorf("it is not a map or array - %T", v)
}
func loadJobFromMap(file string, args []map[string]interface{}) (*ShellJob, error) {
name := strings.ToLower(filepath.Base(file))
if 0 == len(name) {
return nil, errors.New("'name' is missing.")
}
expression := stringWithArguments(args, "expression", "")
if "" == expression {
return nil, errors.New("'expression' is missing.")
}
timeout := durationWithArguments(args, "timeout", 10*time.Minute)
if timeout <= 0*time.Second {
return nil, errors.New("'killTimeout' must is greate 0s.")
}
proc := stringWithArguments(args, "execute", "")
if 0 == len(proc) {
return nil, errors.New("'execute' is missing.")
}
arguments := stringsWithArguments(args, "arguments", "", nil, false)
environments := stringsWithArguments(args, "environments", "", nil, false)
directory := stringWithDefault(args[0], "directory", "")
if 0 == len(directory) && 1 < len(args) {
directory = stringWithArguments(args[1:], "root_dir", "")
}
switch strings.ToLower(filepath.Base(proc)) {
case "java", "java.exe":
var e error
arguments, e = loadJavaArguments(arguments, args)
if nil != e {
return nil, e
}
if "java" == proc || "java.exe" == proc {
proc = *java_home
}
}
logfile := filepath.Join(*log_path, "job_"+name+".log")
return &ShellJob{name: name,
timeout: timeout,
expression: expression,
execute: proc,
directory: directory,
environments: environments,
arguments: arguments,
logfile: logfile}, nil
}
func loadJavaClasspath(cp []string) ([]string, error) {
if nil != cp && 0 != len(cp) {
return nil, nil
}
var classpath []string
for _, p := range cp {
if 0 == len(p) {
continue
}
files, e := filepath.Glob(p)
if nil != e {
return nil, e
}
if nil == files {
continue
}
classpath = append(classpath, files...)
}
return classpath, nil
}
func loadJavaArguments(arguments []string, args []map[string]interface{}) ([]string, error) {
var results []string
classpath, e := loadJavaClasspath(stringsWithArguments(args, "java_classpath", ";", nil, false))
if nil != e {
return nil, e
}
if nil != classpath && 0 != len(classpath) {
if "windows" == runtime.GOOS {
results = append(results, "-cp", strings.Join(classpath, ";"))
} else {
results = append(results, "-cp", strings.Join(classpath, ":"))
}
}
debug := stringWithArguments(args, "java_debug", "")
if 0 != len(debug) {
suspend := boolWithArguments(args, "java_debug_suspend", false)
if suspend {
results = append(results, "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005")
} else {
results = append(results, "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005")
}
}
options := stringsWithArguments(args, "java_options", ",", nil, false)
if nil != options && 0 != len(options) {
results = append(results, options...)
}
class := stringWithArguments(args, "java_class", "")
if 0 != len(class) {
results = append(results, class)
}
jar := stringWithArguments(args, "java_jar", "")
if 0 != len(jar) {
results = append(results, jar)
}
if nil != arguments && 0 != len(arguments) {
return append(results, arguments...), nil
}
return results, nil
}
func loadConfig(root string) (map[string]interface{}, error) {
file := ""
if "" == *config_file || "./<program_name>.conf" == *config_file {
program := filepath.Base(os.Args[0])
files := []string{filepath.Clean(abs(filepath.Join(*root_dir, program+".conf"))),
filepath.Clean(abs(filepath.Join(*root_dir, "etc", program+".conf"))),
filepath.Clean(abs(filepath.Join(*root_dir, "conf", program+".conf"))),
filepath.Clean(abs(filepath.Join(*root_dir, "scheduler.conf"))),
filepath.Clean(abs(filepath.Join(*root_dir, "etc", "scheduler.conf"))),
filepath.Clean(abs(filepath.Join(*root_dir, "conf", "scheduler.conf")))}
found := false
for _, nm := range files {
if fileExists(nm) {
found = true
file = nm
break
}
}
if !found && *is_print {
log.Println("config file is not found:")
for _, nm := range files {
log.Println(" ", nm)
}
}
} else {
file = filepath.Clean(abs(*config_file))
if !fileExists(file) {
return nil, errors.New("config '" + file + "' is not exists.")
}
}
var arguments map[string]interface{}
//"autostart_"
if "" != file {
var e error
arguments, e = loadProperties(root, file)
if nil != e {
return nil, e
}
} else {
log.Println("[warn] the default config file is not found.")
}
if nil == arguments {
arguments = loadDefault(root, file)
}
if _, ok := arguments["java"]; !ok {
arguments["java"] = *java_home
}
arguments["root_dir"] = root
arguments["config_file"] = file
arguments["os"] = runtime.GOOS
arguments["arch"] = runtime.GOARCH
return arguments, nil
}
func loadDefault(root, file string) map[string]interface{} {
return map[string]interface{}{"root_dir": root,
"config_file": file,
"java": *java_home,
"os": runtime.GOOS,
"arch": runtime.GOARCH}
}
func loadProperties(root, file string) (map[string]interface{}, error) {
t, e := template.ParseFiles(file)
if nil != e {
return nil, errors.New("read config failed, " + e.Error())
}
args := loadDefault(root, file)
var buffer bytes.Buffer
e = t.Execute(&buffer, args)
if nil != e {
return nil, errors.New("generate config failed, " + e.Error())
}
var arguments map[string]interface{}
e = json.Unmarshal(buffer.Bytes(), &arguments)
if nil != e {
return nil, errors.New("ummarshal config failed, " + e.Error())
}
return arguments, nil
}