Compare commits
No commits in common. "master" and "v0.0.2" have entirely different histories.
@ -19,7 +19,7 @@ script:
|
|||||||
- diff -u <(echo -n) <(gofmt -d .)
|
- diff -u <(echo -n) <(gofmt -d .)
|
||||||
- go test -v -race ./...
|
- go test -v -race ./...
|
||||||
# Only build binaries from the latest Go release.
|
# Only build binaries from the latest Go release.
|
||||||
- if [ "${LATEST}" = "true" ]; then gox -os="linux" -arch="amd64" -output="concron" -ldflags "-X main.Rev=`git rev-parse --short HEAD`" -verbose ./src/; fi
|
- if [ "${LATEST}" = "true" ]; then gox -os="linux" -arch="amd64" -output="concron.." -ldflags "-X main.Rev=`git rev-parse --short HEAD`" -verbose ./src/; fi
|
||||||
- ls -la
|
- ls -la
|
||||||
|
|
||||||
deploy:
|
deploy:
|
||||||
@ -28,7 +28,7 @@ deploy:
|
|||||||
api_key:
|
api_key:
|
||||||
secure: Pojza+fv52KKqst90bIs8wZqGdJLzDZ/itPzxSfdWqZP4Kz/ER4hpyvT6dt1KM6UG2XRFEfySAgPfHCnop3/bvsZVCxXbFfxoU0Mz8/YeOr5sPYlh4L9THRCPYJsZmTD+EYxeLtRxn1RIVf71SUe+EH76X6jqSvXinQLqZQGshb1J+H3/0BSBHL8vwxRLQbMTgSjaD9UewUzyne1SS5fvqkb9j2QsDT5prez6FPxAZd2Ae/ILEyRN4krXMC5TRMSS1isl0TCLIXUqu6Yz8+sEzmXz4Hs9aL0/rfJ6wLJ89ib/Q+GpGH7PEQ/E1dP2+ijBBTiLDUPWFVXUeBkHxtZY67cM7OAmYrwKpFhLzv2WwXEJl7YdZJ9yK7fMwiHDQKpyC6HfvCcmvMmnQRYfnWoRZdPEDIPkW3XSxN9r1xeK9GG1a/4fGl/ELJ57NWNuLwd3OQVmnzY8fq6+zUb0N/Cv21SPpnOGFyoJ7MefCBKqyAsOtPPSbXBho4Rf2EVzwkZF67LKX4YMUzy+DrlGp/lt8DJyydVHjHBfuzBwfGLWl8dqlceasjWx6EN5RCUI9ygjftALB6xqDqWR8nBabHnubepoj/jI3XlRdG1RWmm0GkXY8AVgbfF6zvgX36gjpBdtWOAk+5iFjp+QHczsDffKSV46VcF9vwF+/Dk575nOp8=
|
secure: Pojza+fv52KKqst90bIs8wZqGdJLzDZ/itPzxSfdWqZP4Kz/ER4hpyvT6dt1KM6UG2XRFEfySAgPfHCnop3/bvsZVCxXbFfxoU0Mz8/YeOr5sPYlh4L9THRCPYJsZmTD+EYxeLtRxn1RIVf71SUe+EH76X6jqSvXinQLqZQGshb1J+H3/0BSBHL8vwxRLQbMTgSjaD9UewUzyne1SS5fvqkb9j2QsDT5prez6FPxAZd2Ae/ILEyRN4krXMC5TRMSS1isl0TCLIXUqu6Yz8+sEzmXz4Hs9aL0/rfJ6wLJ89ib/Q+GpGH7PEQ/E1dP2+ijBBTiLDUPWFVXUeBkHxtZY67cM7OAmYrwKpFhLzv2WwXEJl7YdZJ9yK7fMwiHDQKpyC6HfvCcmvMmnQRYfnWoRZdPEDIPkW3XSxN9r1xeK9GG1a/4fGl/ELJ57NWNuLwd3OQVmnzY8fq6+zUb0N/Cv21SPpnOGFyoJ7MefCBKqyAsOtPPSbXBho4Rf2EVzwkZF67LKX4YMUzy+DrlGp/lt8DJyydVHjHBfuzBwfGLWl8dqlceasjWx6EN5RCUI9ygjftALB6xqDqWR8nBabHnubepoj/jI3XlRdG1RWmm0GkXY8AVgbfF6zvgX36gjpBdtWOAk+5iFjp+QHczsDffKSV46VcF9vwF+/Dk575nOp8=
|
||||||
file:
|
file:
|
||||||
- concron
|
- concron.linux.amd64
|
||||||
on:
|
on:
|
||||||
repo: jar3b/concron
|
repo: jar3b/concron
|
||||||
tags: true
|
tags: true
|
||||||
|
@ -24,14 +24,6 @@ arguments:
|
|||||||
|
|
||||||
- `/healthz` - health endpoint, returns code `200` with text `OK`. Useful for kubernetes pods ready/live probes.
|
- `/healthz` - health endpoint, returns code `200` with text `OK`. Useful for kubernetes pods ready/live probes.
|
||||||
|
|
||||||
## global env variables
|
|
||||||
|
|
||||||
- `ALLOWED_TASKS` - list comma-separated task names, only tasks with these names will be processed. example:
|
|
||||||
|
|
||||||
```
|
|
||||||
ALLOWED_TASKS=sleep,hello_world
|
|
||||||
```
|
|
||||||
|
|
||||||
## config format
|
## config format
|
||||||
|
|
||||||
example:
|
example:
|
||||||
|
10
go.mod
10
go.mod
@ -1,11 +1,9 @@
|
|||||||
module github.com/jar3b/concron
|
module github.com/jar3b/concron
|
||||||
|
|
||||||
go 1.13
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/jar3b/logrus-levelpad-formatter v1.0.0
|
github.com/jar3b/logrus-levelpad-formatter v1.0.0
|
||||||
github.com/julienschmidt/httprouter v1.3.0
|
github.com/julienschmidt/httprouter v1.2.0
|
||||||
github.com/robfig/cron v1.2.0
|
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967
|
||||||
github.com/sirupsen/logrus v1.4.2
|
github.com/sirupsen/logrus v1.4.0
|
||||||
gopkg.in/yaml.v2 v2.2.7
|
gopkg.in/yaml.v2 v2.2.2
|
||||||
)
|
)
|
||||||
|
14
src/main.go
14
src/main.go
@ -8,8 +8,6 @@ import (
|
|||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func healthHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
func healthHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
@ -41,18 +39,8 @@ func main() {
|
|||||||
|
|
||||||
initLog(*debug)
|
initLog(*debug)
|
||||||
|
|
||||||
// get allowed tasks
|
|
||||||
var allowedTasks *map[string]bool
|
|
||||||
if os.Getenv("ALLOWED_TASKS") != "" {
|
|
||||||
at := make(map[string]bool, 0)
|
|
||||||
allowedTasks = &at
|
|
||||||
for _, t := range strings.Split(os.Getenv("ALLOWED_TASKS"), ",") {
|
|
||||||
(*allowedTasks)[t] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// manage tasks
|
// manage tasks
|
||||||
taskList, err := tasks.LoadTasks(*configFile, allowedTasks)
|
taskList, err := tasks.LoadTasks(*configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("cannot load %s: %v", *configFile, err)
|
log.Fatalf("cannot load %s: %v", *configFile, err)
|
||||||
return
|
return
|
||||||
|
@ -48,7 +48,6 @@ type Task struct {
|
|||||||
Deadline uint32 `yaml:"deadline"`
|
Deadline uint32 `yaml:"deadline"`
|
||||||
UseSystemEnv *bool `yaml:"useSystemEnv,omitempty"`
|
UseSystemEnv *bool `yaml:"useSystemEnv,omitempty"`
|
||||||
ConcurrencyPolicy string `yaml:"concurrencyPolicy"`
|
ConcurrencyPolicy string `yaml:"concurrencyPolicy"`
|
||||||
ForceOutput *bool `yaml:"forceOutput,omitempty"`
|
|
||||||
|
|
||||||
// internal values
|
// internal values
|
||||||
envVars []string
|
envVars []string
|
||||||
@ -60,9 +59,6 @@ type Task struct {
|
|||||||
// task executions
|
// task executions
|
||||||
execCount int
|
execCount int
|
||||||
execMap map[int]*taskExecution
|
execMap map[int]*taskExecution
|
||||||
|
|
||||||
// allowed to run of not
|
|
||||||
Enabled bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) init(shell string, systemEnvs *map[string]string) error {
|
func (t *Task) init(shell string, systemEnvs *map[string]string) error {
|
||||||
@ -147,11 +143,7 @@ func (t *Task) Run() {
|
|||||||
select {
|
select {
|
||||||
case err := <-execution.stop:
|
case err := <-execution.stop:
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if t.ForceOutput != nil && *(t.ForceOutput) == true {
|
|
||||||
log.Infof("[%-20s][%d] SUCCESS, took %v, output: %s", t.Name, execution.id, time.Since(execution.time), t.buf.String())
|
|
||||||
} else {
|
|
||||||
log.Infof("[%-20s][%d] SUCCESS, took %v", t.Name, execution.id, time.Since(execution.time))
|
log.Infof("[%-20s][%d] SUCCESS, took %v", t.Name, execution.id, time.Since(execution.time))
|
||||||
}
|
|
||||||
log.Debugf("[%-20s][%d] SUCCESS, output: %s", t.Name, execution.id, t.buf.String())
|
log.Debugf("[%-20s][%d] SUCCESS, output: %s", t.Name, execution.id, t.buf.String())
|
||||||
} else {
|
} else {
|
||||||
log.Infof("[%-20s][%d] ERROR, took %v, err: %v, output: %s", t.Name, execution.id, time.Since(execution.time), err, t.buf.String())
|
log.Infof("[%-20s][%d] ERROR, took %v, err: %v, output: %s", t.Name, execution.id, time.Since(execution.time), err, t.buf.String())
|
||||||
@ -166,7 +158,7 @@ type ConfigDescriptiveInfo struct {
|
|||||||
Tasks []*Task `yaml:"tasks"`
|
Tasks []*Task `yaml:"tasks"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (di *ConfigDescriptiveInfo) InitTasks(allowedTasksMap *map[string]bool) []error {
|
func (di *ConfigDescriptiveInfo) InitTasks() []error {
|
||||||
var errList = make([]error, 0)
|
var errList = make([]error, 0)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@ -177,32 +169,15 @@ func (di *ConfigDescriptiveInfo) InitTasks(allowedTasksMap *map[string]bool) []e
|
|||||||
env[pair[0]] = pair[1]
|
env[pair[0]] = pair[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
var taskNamesEnabled []string
|
var taskNames []string
|
||||||
var taskNamesDisabled []string
|
|
||||||
for _, t := range di.Tasks {
|
for _, t := range di.Tasks {
|
||||||
t.Enabled = true
|
|
||||||
if allowedTasksMap != nil {
|
|
||||||
t.Enabled = (*allowedTasksMap)[t.Name]
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = t.init(di.Shell, &env); err != nil {
|
if err = t.init(di.Shell, &env); err != nil {
|
||||||
t.Enabled = false
|
|
||||||
errList = append(errList, err)
|
errList = append(errList, err)
|
||||||
}
|
}
|
||||||
|
taskNames = append(taskNames, t.Name)
|
||||||
if t.Enabled {
|
|
||||||
taskNamesEnabled = append(taskNamesEnabled, t.Name)
|
|
||||||
} else {
|
|
||||||
taskNamesDisabled = append(taskNamesDisabled, t.Name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof(
|
log.Infof("%d tasks loaded - %s", len(di.Tasks), strings.Join(taskNames, ", "))
|
||||||
"%d tasks loaded - enabled: '%s', disabled: '%s'",
|
|
||||||
len(di.Tasks),
|
|
||||||
strings.TrimRight(strings.Join(taskNamesEnabled, ", "), ", "),
|
|
||||||
strings.TrimRight(strings.Join(taskNamesDisabled, ", "), ", "),
|
|
||||||
)
|
|
||||||
|
|
||||||
return errList
|
return errList
|
||||||
}
|
}
|
||||||
|
@ -15,11 +15,6 @@ type Scheduler struct {
|
|||||||
func (s *Scheduler) AddTasks(taskList []*Task) error {
|
func (s *Scheduler) AddTasks(taskList []*Task) error {
|
||||||
s.tasks = taskList
|
s.tasks = taskList
|
||||||
for _, task := range s.tasks {
|
for _, task := range s.tasks {
|
||||||
// pass task if it's not enabled
|
|
||||||
if !task.Enabled {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.cron.AddFunc(task.Crontab, task.Run); err != nil {
|
if err := s.cron.AddFunc(task.Crontab, task.Run); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadTasks(configFileName string, allowedTasks *map[string]bool) (*ConfigDescriptiveInfo, error) {
|
func LoadTasks(configFileName string) (*ConfigDescriptiveInfo, error) {
|
||||||
config := ConfigDescriptiveInfo{}
|
config := ConfigDescriptiveInfo{}
|
||||||
data, err := helpers.ReadBinFile(configFileName)
|
data, err := helpers.ReadBinFile(configFileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -19,7 +19,8 @@ func LoadTasks(configFileName string, allowedTasks *map[string]bool) (*ConfigDes
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
errList := config.InitTasks(allowedTasks)
|
// init tasks
|
||||||
|
errList := config.InitTasks()
|
||||||
for _, err := range errList {
|
for _, err := range errList {
|
||||||
log.Errorf(fmt.Sprintf("parse task error: %v", err))
|
log.Errorf(fmt.Sprintf("parse task error: %v", err))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user