Compare commits

...

4 Commits

7 changed files with 66 additions and 15 deletions

View File

@ -19,7 +19,7 @@ script:
- diff -u <(echo -n) <(gofmt -d .)
- go test -v -race ./...
# 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
deploy:
@ -28,7 +28,7 @@ deploy:
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=
file:
- concron.linux.amd64
- concron
on:
repo: jar3b/concron
tags: true

View File

@ -24,6 +24,14 @@ arguments:
- `/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
example:

10
go.mod
View File

@ -1,9 +1,11 @@
module github.com/jar3b/concron
go 1.13
require (
github.com/jar3b/logrus-levelpad-formatter v1.0.0
github.com/julienschmidt/httprouter v1.2.0
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967
github.com/sirupsen/logrus v1.4.0
gopkg.in/yaml.v2 v2.2.2
github.com/julienschmidt/httprouter v1.3.0
github.com/robfig/cron v1.2.0
github.com/sirupsen/logrus v1.4.2
gopkg.in/yaml.v2 v2.2.7
)

View File

@ -8,6 +8,8 @@ import (
"github.com/julienschmidt/httprouter"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"strings"
)
func healthHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@ -39,8 +41,18 @@ func main() {
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
taskList, err := tasks.LoadTasks(*configFile)
taskList, err := tasks.LoadTasks(*configFile, allowedTasks)
if err != nil {
log.Fatalf("cannot load %s: %v", *configFile, err)
return

View File

@ -48,6 +48,7 @@ type Task struct {
Deadline uint32 `yaml:"deadline"`
UseSystemEnv *bool `yaml:"useSystemEnv,omitempty"`
ConcurrencyPolicy string `yaml:"concurrencyPolicy"`
ForceOutput *bool `yaml:"forceOutput,omitempty"`
// internal values
envVars []string
@ -59,6 +60,9 @@ type Task struct {
// task executions
execCount int
execMap map[int]*taskExecution
// allowed to run of not
Enabled bool
}
func (t *Task) init(shell string, systemEnvs *map[string]string) error {
@ -143,7 +147,11 @@ func (t *Task) Run() {
select {
case err := <-execution.stop:
if err == nil {
log.Infof("[%-20s][%d] SUCCESS, took %v", t.Name, execution.id, time.Since(execution.time))
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.Debugf("[%-20s][%d] SUCCESS, output: %s", t.Name, execution.id, t.buf.String())
} else {
log.Infof("[%-20s][%d] ERROR, took %v, err: %v, output: %s", t.Name, execution.id, time.Since(execution.time), err, t.buf.String())
@ -158,7 +166,7 @@ type ConfigDescriptiveInfo struct {
Tasks []*Task `yaml:"tasks"`
}
func (di *ConfigDescriptiveInfo) InitTasks() []error {
func (di *ConfigDescriptiveInfo) InitTasks(allowedTasksMap *map[string]bool) []error {
var errList = make([]error, 0)
var err error
@ -169,15 +177,32 @@ func (di *ConfigDescriptiveInfo) InitTasks() []error {
env[pair[0]] = pair[1]
}
var taskNames []string
var taskNamesEnabled []string
var taskNamesDisabled []string
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 {
t.Enabled = false
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("%d tasks loaded - %s", len(di.Tasks), strings.Join(taskNames, ", "))
log.Infof(
"%d tasks loaded - enabled: '%s', disabled: '%s'",
len(di.Tasks),
strings.TrimRight(strings.Join(taskNamesEnabled, ", "), ", "),
strings.TrimRight(strings.Join(taskNamesDisabled, ", "), ", "),
)
return errList
}

View File

@ -15,6 +15,11 @@ type Scheduler struct {
func (s *Scheduler) AddTasks(taskList []*Task) error {
s.tasks = taskList
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 {
return err
}

View File

@ -8,7 +8,7 @@ import (
"gopkg.in/yaml.v2"
)
func LoadTasks(configFileName string) (*ConfigDescriptiveInfo, error) {
func LoadTasks(configFileName string, allowedTasks *map[string]bool) (*ConfigDescriptiveInfo, error) {
config := ConfigDescriptiveInfo{}
data, err := helpers.ReadBinFile(configFileName)
if err != nil {
@ -19,8 +19,7 @@ func LoadTasks(configFileName string) (*ConfigDescriptiveInfo, error) {
return nil, err
}
// init tasks
errList := config.InitTasks()
errList := config.InitTasks(allowedTasks)
for _, err := range errList {
log.Errorf(fmt.Sprintf("parse task error: %v", err))
}