forked from ajvb/kala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (141 loc) · 3.63 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
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
package main
import (
"fmt"
"os"
"runtime"
"time"
"github.com/ajvb/kala/api"
"github.com/ajvb/kala/job"
"github.com/ajvb/kala/job/storage/boltdb"
"github.com/ajvb/kala/job/storage/redis"
redislib "github.com/garyburd/redigo/redis"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
func init() {
log.SetLevel(log.WarnLevel)
}
var (
// DefaultPersistEvery is an interval
DefaultPersistEvery = 5 * time.Second
db job.JobDB
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
app := cli.NewApp()
app.Name = "Kala"
app.Usage = "Modern job scheduler"
app.Version = "0.1"
app.Commands = []cli.Command{
{
Name: "run_command",
Usage: "Run a command as if it was being run by Kala",
Action: func(c *cli.Context) {
if len(c.Args()) == 0 {
log.Fatal("Must include a command")
} else if len(c.Args()) > 1 {
log.Fatal("Must only include a command")
}
cmd := c.Args()[0]
j := &job.Job{
Command: cmd,
}
err := j.RunCmd()
if err != nil {
log.Fatalf("Command Failed with err: %s", err)
} else {
fmt.Println("Command Succeeded!")
}
},
},
{
Name: "run",
Usage: "run kala",
Flags: []cli.Flag{
cli.IntFlag{
Name: "port, p",
Value: 8000,
Usage: "Port for Kala to run on.",
},
cli.BoolFlag{
Name: "no-persist, np",
Usage: "No Persistence Mode - In this mode no data will be saved to the database. Perfect for testing.",
},
cli.StringFlag{
Name: "interface, i",
Value: "",
Usage: "Interface to listen on, default is all.",
},
cli.StringFlag{
Name: "default-owner, do",
Value: "",
Usage: "Default owner. The inputted email will be attached to any job missing an owner",
},
cli.StringFlag{
Name: "jobDB",
Value: "boltdb",
Usage: "Implementation of job database, either 'boltdb' or 'redis'.",
},
cli.StringFlag{
Name: "boltpath",
Value: "",
Usage: "Path to the bolt database file, default is current directory.",
},
cli.StringFlag{
Name: "jobDBAddress",
Value: "127.0.0.1:6379",
Usage: "Network address for the job database, in 'host:port' format.",
},
cli.StringFlag{
Name: "jobDBPassword",
Value: "password",
Usage: "Password for the job database, in 'password' format.",
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Set for verbose logging.",
},
},
Action: func(c *cli.Context) {
if c.Bool("v") {
log.SetLevel(log.DebugLevel)
}
var parsedPort string
port := c.Int("port")
if port != 0 {
parsedPort = fmt.Sprintf(":%d", port)
} else {
parsedPort = ":8000"
}
var connectionString string
if c.String("interface") != "" {
connectionString = c.String("interface") + parsedPort
} else {
connectionString = parsedPort
}
switch c.String("jobDB") {
case "boltdb":
db = boltdb.GetBoltDB(c.String("boltpath"))
case "redis":
if c.String("jobDBPassword") != "" {
option := redislib.DialPassword(c.String("jobDBPassword"))
db = redis.New(c.String("jobDBAddress"), option)
} else {
db = redis.New(c.String("jobDBAddress"), redislib.DialOption{})
}
default:
log.Fatalf("Unknown Job DB implementation '%s'", c.String("jobDB"))
}
if c.Bool("no-persist") {
db = &job.MockDB{}
}
// Create cache
cache := job.NewMemoryJobCache(db)
cache.Start(DefaultPersistEvery)
log.Infof("Starting server on port %s...", connectionString)
log.Fatal(api.StartServer(connectionString, cache, db, c.String("default-owner")))
},
},
}
app.Run(os.Args)
}