-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserve.go
193 lines (162 loc) · 4.75 KB
/
serve.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
package main
import (
"errors"
"flag"
"fmt"
"log"
"log/syslog"
"os"
"path/filepath"
"time"
"bitbucket.org/kardianos/osext"
"bytes"
"github.com/d3media/llconf/compiler"
libpromise "github.com/d3media/llconf/promise"
)
var serve = &Command{
Name: "serve",
Usage: "serve [arguments...]",
ShortHelp: "serve",
LongHelp: "bla",
Run: runServ,
}
var serve_cfg struct {
root_promise string
verbose bool
use_syslog bool
interval int
inp_dir string
workdir string
runlog_path string
debug bool
}
func init() {
serve.Flag.IntVar(&serve_cfg.interval, "interval", 300, "set the minium time between promise-tree evaluation")
serve.Flag.BoolVar(&serve_cfg.verbose, "verbose", false, "enable verbose output")
serve.Flag.StringVar(&serve_cfg.root_promise, "promise", "done", "the promise that will be used as the root of the promise tree")
serve.Flag.StringVar(&serve_cfg.inp_dir, "input-folder", "", "the folder containing input files")
serve.Flag.BoolVar(&serve_cfg.use_syslog, "syslog", false, "output to syslog")
serve.Flag.StringVar(&serve_cfg.runlog_path, "runlog", "", "path to the runlog")
serve.Flag.BoolVar(&serve_cfg.debug, "debug", false, "turn on debugging output")
}
func runServ(args []string) {
parseArguments(args)
logi, loge := setupLogging()
quit := make(chan int)
var promise_tree libpromise.Promise
for {
go func(q chan int) {
time.Sleep(time.Duration(serve_cfg.interval) * time.Second)
q <- 0
}(quit)
new_promise_tree, err := updatePromise(serve_cfg.inp_dir, serve_cfg.root_promise)
if err == nil {
promise_tree = new_promise_tree
} else {
loge.Printf("error while parsing input folder: %v\n", err)
}
if promise_tree != nil {
checkPromise(promise_tree, logi, loge, flag.Args())
} else {
fmt.Fprintf(os.Stderr, "could not find any valid promises\n")
}
<-quit
}
}
func setupLogging() (logi, loge *log.Logger) {
if serve_cfg.use_syslog {
logi, _ = syslog.NewLogger(syslog.LOG_NOTICE, log.LstdFlags)
loge, _ = syslog.NewLogger(syslog.LOG_ERR, log.LstdFlags)
return
} else {
logi = log.New(os.Stdout, "llconf (info)", log.LstdFlags)
loge = log.New(os.Stderr, "llconf (err)", log.LstdFlags|log.Lshortfile)
return
}
}
func parseArguments(args []string) {
switch len(args) {
case 0:
fmt.Fprintf(os.Stderr, "no workdir specified\n")
os.Exit(1)
return
case 1:
serve_cfg.workdir = args[0]
default:
fmt.Fprintf(os.Stderr, "argument count mismatch")
os.Exit(1)
}
if serve_cfg.inp_dir == "" {
serve_cfg.inp_dir = filepath.Join(serve_cfg.workdir, "input")
}
if serve_cfg.runlog_path == "" {
serve_cfg.runlog_path = filepath.Join(serve_cfg.workdir, "runlog")
}
// when run as daemon, the home folder isn't set
home := os.Getenv("HOME")
if home == "" {
os.Setenv("HOME", serve_cfg.workdir)
}
}
func updatePromise(folder, root string) (libpromise.Promise, error) {
promises, err := compiler.Compile(folder)
if err != nil {
return nil, err
}
if promise, promise_present := promises[root]; promise_present {
return promise, nil
} else {
return nil, errors.New("root promise (" + root + ") unknown")
}
}
func checkPromise(p libpromise.Promise, logi, loge *log.Logger, args []string) {
vars := libpromise.Variables{}
vars["input_dir"] = serve_cfg.inp_dir
vars["work_dir"] = serve_cfg.workdir
exe, _ := osext.Executable()
vars["executable"] = exe
env := []string{}
logger := libpromise.Logger{
Error: loge,
Info: logi,
Changes: 0,
Tests: 0}
ctx := libpromise.Context{
Logger: &logger,
ExecOutput: &bytes.Buffer{},
Vars: vars,
Args: args,
Env: env,
Debug: serve_cfg.debug,
InDir: ""}
starttime := time.Now().Local()
promises_fullfilled := p.Eval([]libpromise.Constant{}, &ctx, "")
endtime := time.Now().Local()
logi.Printf("%d changes and %d tests executed\n", ctx.Logger.Changes, ctx.Logger.Tests)
if promises_fullfilled {
logi.Printf("evaluation successful\n")
} else {
loge.Printf("error during evaluation\n")
}
writeRunLog(promises_fullfilled, ctx.Logger.Changes, ctx.Logger.Tests, starttime, endtime, serve_cfg.runlog_path)
}
func writeRunLog(success bool, changes, tests int, starttime, endtime time.Time, path string) (err error) {
var output string
duration := endtime.Sub(starttime)
if success {
output = fmt.Sprintf("successful, endtime=%d, duration=%f, c=%d, t=%d\n", endtime.Unix(), duration.Seconds(), changes, tests)
} else {
output = fmt.Sprintf("error, endtime=%d, duration=%f, c=%d, t=%d\n", endtime.Unix(), duration.Seconds(), changes, tests)
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return
}
data := []byte(output)
n, err := f.Write(data)
if err == nil && n < len(data) {
return
}
err = f.Close()
return
}