Documentation ¶
Index ¶
- Variables
- func Commit(worktreeDir string, msg string)
- func ConsumeSchedule(queue <-chan []byte, path string, wg *sync.WaitGroup)
- func DirExists(dirname string) bool
- func ExecTasks(cronicleFile string, taskName string, scheduleName string, now time.Time)
- func GetRepos(conf *Config) map[string]bool
- func Init(croniclePath string, cloneRepo string, deployKey string, defaultConf Config)
- func LoadCron(cronicleFile string, c *cron.Cron, queue chan<- []byte, force bool)
- func LocalRepoDir(croniclePath string, repoURL string) (string, error)
- func MakeViceTransport(queueType string, addr string) vice.Transport
- func MarshallHcl(conf Config, path string) string
- func ProduceSchedule(schedule Schedule, queue chan<- []byte) func()
- func Run(cronicleFile string, runOptions RunOptions)
- func StartCron(cronicleFile string, queue chan<- []byte)
- func StartWorker(path string, runOptions RunOptions)
- type Config
- func (conf Config) Hcl() HclWriteFile
- func (conf *Config) Init(croniclePath string) error
- func (conf Config) JSON() []byte
- func (conf *Config) PropigateTaskProperties(croniclePath string)
- func (conf *Config) ScheduleMap() ScheduleMap
- func (conf *Config) TaskArray() TaskArray
- func (conf *Config) Validate() error
- type Git
- type HclWriteFile
- type Queue
- type Repo
- type Retry
- type RunOptions
- type Schedule
- type ScheduleMap
- type TZFormatter
- type Task
- type TaskArray
- type TaskMap
Constants ¶
This section is empty.
Variables ¶
var ( //ErrBranchAndCommitGiven is thrown because commit and branch are mutually exclusive to identify a repo ErrBranchAndCommitGiven = errors.New("branch and commit can not both be populated") //ErrRepoNotGiven is thrown because a git repo is not given, for the case where Checkout or other git //specific methods are called ErrRepoNotGiven = errors.New("git repo has not been given") //ErrIfRepoGivenAndPathNotGiven is thrown because a repo was given but the path to the local repo has not been provided ErrIfRepoGivenAndPathNotGiven = errors.New("if repo is populated, path must also be given at runtime") //ErrScheduleNameEmpty is thrown because schedule.Name == "", hcl can not be given with schedule "" {} ErrScheduleNameEmpty = errors.New("schedule name can not be an empty string") //ErrTaskNameEmpty is thrown because task.Name == "", hcl can not be given with task "" {} ErrTaskNameEmpty = errors.New("task name can not be an empty string") //ErrRepoGivenAndURLNotGiven is thrown because task.Name == "", hcl can not be given with task "" {} ErrRepoGivenAndURLNotGiven = errors.New("if repo is populated, it must have an assoicated url") )
var ( // CommandEvalContext hcl.EvalContext evaluates the "${date}" argument and carries it through // as a string of the same form that will be used as an arugment later in the code. CommandEvalContext = hcl.EvalContext{ Variables: map[string]cty.Value{ "date": cty.StringVal("${date}"), "datetime": cty.StringVal("${datetime}"), "timestamp": cty.StringVal("${timestamp}"), }, } // TimeArgumentFormatMap maps the CommandEvalContext arguments to time.Format strings for reforamting // arguments given in hcl to timestamps. // ${date}: "2006-01-02" // ${datetime}: "2006-01-02T15:04:05Z07:00" // ${timestamp}: "2006-01-02 15:04:05Z07:00" TimeArgumentFormatMap = map[string]string{ "${date}": "2006-01-02", "${datetime}": time.RFC3339, "${timestamp}": "2006-01-02 15:04:05Z07:00", } )
Functions ¶
func ConsumeSchedule ¶
ConsumeSchedule consumes the byte array of a schedule from the message queue for execution
func ExecTasks ¶
ExecTasks parses the cronicle.hcl config, filters for a specified task and executes the task
func Init ¶
Init initializes a default croniclePath with a .git repository, Basic schedule as code in a cronicle.hcl file and a repos folder.
func LoadCron ¶
LoadCron exeutes GetConfig(cronicleFile) to load the current config from file, checks the given config against the global confPrior, and if there is a change, stops the cron, removes all of the confPrior cron entries and adds the new conf schedules to the cron.
func LocalRepoDir ¶
LocalRepoDir takes a cronicle.hcl path and a github repo URL and converts it to the local clone of that repo
func MakeViceTransport ¶
MakeViceTransport creates a vice.Transport interface from the given queue field in the config
func MarshallHcl ¶
MarshallHcl writes a given Config to an hcl file at path
func ProduceSchedule ¶
ProduceSchedule produces the json of a schdule to the message queue for consumption
func Run ¶
func Run(cronicleFile string, runOptions RunOptions)
Run is the main function of the cron package
func StartCron ¶
StartCron pushes all schedules in the given config to the cron scheduler starts the cron scheduler which publishes the serialzied schedules to the message queue for execution.
func StartWorker ¶
func StartWorker(path string, runOptions RunOptions)
StartWorker listens to a vice transport queue for schedules produced by cronicle run
Types ¶
type Config ¶
type Config struct { // Repo repository containing version controlled cronicle.hcl Repo *Repo `hcl:"repo,block"` // Cron expression that specifies the cronicle heartbeat and cronicle.hcl refresh Heartbeat string `hcl:"heartbeat,optional"` // Repos points at external dependent repos that maintain their own schedules remotly. Repos []string `hcl:"repos,optional"` // Timezone Location to run cron in. i.e. "America/New_York" [IANA Time Zone database] // https://en.wikipedia.org/wiki/List_of_tz_database_time_zones Timezone string `hcl:"timezone,optional"` // GitRemote *GitRemote `hcl:"git,block"` Queue *Queue `hcl:"queue,block"` Schedules []Schedule `hcl:"schedule,block"` }
Config is the configuration structure for the cronicle checker. https://raw.githubusercontent.com/mitchellh/golicense/master/config/config.go TODO: Add Version string `hcl:"version,optional"`
func Default ¶
func Default() Config
Default returns a basic default Config it includes a single schedule that runs every 5 seconds and a single "Hello World" task.
func GetConfig ¶
GetConfig returns the Config specified by the given cronicle.hcl file Including any Cronicle files specified by in the repos directory.
func (Config) Hcl ¶
func (conf Config) Hcl() HclWriteFile
Hcl returns a hcl File object from a given Config
func (*Config) Init ¶
Init populates task repo path, runs git clone for any sub repos, and assigns Git meta data to the task
func (*Config) PropigateTaskProperties ¶
PropigateTaskProperties pushes schedule.Name, schedule.Repo and the repo path down to the task values. It also populates task.Git.ReferenceName with task.Branch or HEAD.
func (*Config) ScheduleMap ¶ added in v0.3.1
func (conf *Config) ScheduleMap() ScheduleMap
ScheduleMap exports a ScheduleMap all schedules in a given config
type Git ¶
type Git struct { Worktree *git.Worktree Repository *git.Repository Head *plumbing.Reference Hash *plumbing.Hash Commit *object.Commit ReferenceName plumbing.ReferenceName // contains filtered or unexported fields }
Git is the struct which associates common data structures from the go-git library.
func Clone ¶
Clone checks for the existance of worktreeDir/.git and clones if it does not exist then executes Git = GetGit(worktreeDir)
type HclWriteFile ¶
type HclWriteFile struct { // File is the hclwrite.File encoded by gohcl.EncodeIntoBody File hclwrite.File // Bytes is the byte array with deduped $$ Bytes []byte }
HclWriteFile contains the encoded hclwrite.File and the byte array of the file with the $$ deduped. This is due the the effect that when writing template arguments with the hcl library an extra $ will be added automatically. i.e. "${date}" becomes "$${date}"
type Queue ¶
type Queue struct { //Type names the message queue technology to be used //options are nsq and redis Type string `hcl:"type,optional"` //host:port of nsqd/nsqlookupd/redis queue service Addr string `hcl:"addr,optional"` }
Queue is the metadata associated to the message queue for distributed operation. Cronicle uses vice to communicate with queues via channels. https://github.com/matryer/vice
type Repo ¶ added in v0.2.0
type Repo struct { // URL is the remote git repository, a local path to git repository URL string `hcl:"url,optional"` // DeployKey is the path to the rsa private key that enables pull access to a // private remote repository. DeployKey string `hcl:"key,optional"` Commit string `hcl:"branch,optional"` Branch string `hcl:"commit,optional"` }
Repo is the structure that defines a git repository
type Retry ¶
type Retry struct { //Count: Number of retry attemts to make after first attempt Count int `hcl:"count,optional"` //Seconds: Number of seconds to wait between retry attempts Seconds int `hcl:"seconds,optional"` //Minutes: Number of minutes to wait between retry attempts Minutes int `hcl:"minutes,optional"` //Hours: Number of hours to wait between retry attempts Hours int `hcl:"hours,optional"` }
Retry defines the retry count and delay in number and seconds.
type RunOptions ¶
type RunOptions struct { RunWorker bool QueueType string QueueName string Addr string LogToFile bool }
RunOptions enables the runtime configuration of the distributed message queue
type Schedule ¶
type Schedule struct { // Cron is the schedule interval. The field accepts standard cron // and other configurations listed here https://godoc.org/gopkg.in/robfig/cron.v2 // i.e. ["@hourly", "@every 1h30m", "0 30 * * * *", "TZ=Asia/Tokyo 30 04 * * * *"] Name string `hcl:"name,label"` Cron string `hcl:"cron,optional"` // Timezone Location to run cron in. i.e. "America/New_York" [IANA Time Zone database] // https://en.wikipedia.org/wiki/List_of_tz_database_time_zones Timezone string `hcl:"timezone,optional"` StartDate string `hcl:"start_date,optional"` EndDate string `hcl:"end_date,optional"` Repo *Repo `hcl:"repo,block"` Tasks []Task `hcl:"task,block"` //Now is the execution time of the given schedule that will be used to //fill variable task command ${datetime}. The cron scheduler generally provides //the value. Now time.Time //repo given at the config level, will be overridden by repo given at schedule or task level. CronicleRepo *Repo }
Schedule is the configuration structure that defines a cron job consisting of tasks.
func (*Schedule) CleanGit ¶
func (schedule *Schedule) CleanGit()
CleanGit nulls non-serlizable properties of a schedule task.Git = Git{}
func (Schedule) ExecuteTasks ¶
func (schedule Schedule) ExecuteTasks()
ExecuteTasks handels the execution of all tasks in a given schedule. The execution walks over a DAG[Directed Acyclic Graph] to determine execution order, which will default to parallel unless task.depends is specified.
func (*Schedule) PropigateTaskProperties ¶
PropigateTaskProperties pushes schedule.Name, schedule.Repo and the repo path down to the task values. It also populates task.Git.ReferenceName with task.Branch or HEAD.
type ScheduleMap ¶ added in v0.3.1
ScheduleMap is an map of key=schedul.Name: value=Schedule struct,
type TZFormatter ¶ added in v0.2.2
TZFormatter enables timezone specifc logrus formatting Example: loc, _ = time.LoadLocation("America/Los_Angeles")
log.SetFormatter(TZFormatter{Formatter: &log.TextFormatter{ FullTimestamp: true, }, loc: loc})
type Task ¶
type Task struct { Name string `hcl:"name,label"` Command []string `hcl:"command,optional"` Depends []string `hcl:"depends,optional"` Repo *Repo `hcl:"repo,block"` Retry *Retry `hcl:"retry,block"` Env []string `hcl:"env,optional"` Path string CronicleRepo *Repo CroniclePath string Git Git ScheduleName string }
Task is the configuration structure that defines a task (i.e., a command)
func (*Task) CleanGit ¶
func (task *Task) CleanGit()
CleanGit nulls non-serlizable properties of a task task.Git = Git{}
func (*Task) Exec ¶
Exec executes task.Command at task.Path and returns the exec.Result struct prior to execution, the command will replace any ${date}, ${datetime}, ${timestamp} with time t given in the bash command
func (Task) Hcl ¶
func (task Task) Hcl() HclWriteFile
Hcl returns a hcl File object from a given task
type TaskArray ¶
type TaskArray []Task
TaskArray is an array of Task structs, calling config.TaskArray() ensures that each task.ScheduleName is filled
func (TaskArray) FilterTasks ¶
FilterTasks returns a task array where only matching task.Name = taskName and schedule.Name=scheduleName if taskName = "" and scheduleName = "" then all tasks will be returned empty strings are intrepreted as no filtering requested.