core

package
v0.8.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const HashmeTagName = "hash"

Variables

View Source
var (
	// ErrSkippedExecution pass this error to `Execution.Stop` if you wish to mark
	// it as skipped.
	ErrSkippedExecution   = errors.New("skipped execution")
	ErrUnexpected         = errors.New("error unexpected, docker has returned exit code -1, maybe wrong user?")
	ErrMaxTimeRunning     = errors.New("the job has exceeded the maximum allowed time running.")
	ErrLocalImageNotFound = errors.New("couldn't find image on the host")
)
View Source
var (
	ErrEmptyScheduler = errors.New("unable to start a empty scheduler.")
	ErrEmptySchedule  = errors.New("unable to add a job with a empty schedule.")
)

Functions

This section is empty.

Types

type BareJob

type BareJob struct {
	Schedule     string `hash:"true"`
	Name         string `hash:"true"`
	Command      string `hash:"true"`
	HistoryLimit int    `default:"10"`
	// contains filtered or unexported fields
}

func (*BareJob) GetCommand

func (j *BareJob) GetCommand() string

func (*BareJob) GetCronJobID

func (j *BareJob) GetCronJobID() int

func (*BareJob) GetHistory added in v0.8.0

func (j *BareJob) GetHistory() []*Execution

GetHistory returns a copy of the job's execution history.

func (*BareJob) GetLastRun added in v0.8.0

func (j *BareJob) GetLastRun() *Execution

GetLastRun returns the last execution of the job, if any.

func (*BareJob) GetName

func (j *BareJob) GetName() string

func (*BareJob) GetSchedule

func (j *BareJob) GetSchedule() string

func (*BareJob) Hash

func (j *BareJob) Hash() (string, error)

Returns a hash of all the job attributes. Used to detect changes

func (*BareJob) Middlewares

func (c *BareJob) Middlewares() []Middleware

func (*BareJob) NotifyStart

func (j *BareJob) NotifyStart()

func (*BareJob) NotifyStop

func (j *BareJob) NotifyStop()

func (*BareJob) Running

func (j *BareJob) Running() int32

func (*BareJob) SetCronJobID

func (j *BareJob) SetCronJobID(id int)

func (*BareJob) SetLastRun added in v0.8.0

func (j *BareJob) SetLastRun(e *Execution)

SetLastRun stores the last executed run for the job.

func (*BareJob) Use

func (c *BareJob) Use(ms ...Middleware)

type Context

type Context struct {
	Scheduler *Scheduler
	Logger    Logger
	Job       Job
	Execution *Execution
	// contains filtered or unexported fields
}

func NewContext

func NewContext(s *Scheduler, j Job, e *Execution) *Context

func (*Context) Log

func (c *Context) Log(msg string)

func (*Context) Next

func (c *Context) Next() error

func (*Context) Start

func (c *Context) Start()

func (*Context) Stop

func (c *Context) Stop(err error)

func (*Context) Warn

func (c *Context) Warn(msg string)

type CronUtils

type CronUtils struct {
	Logger Logger
}

Implement the cron logger interface

func NewCronUtils

func NewCronUtils(l Logger) *CronUtils

func (*CronUtils) Error

func (c *CronUtils) Error(err error, msg string, keysAndValues ...interface{})

func (*CronUtils) Info

func (c *CronUtils) Info(msg string, keysAndValues ...interface{})

type ExecJob

type ExecJob struct {
	BareJob     `mapstructure:",squash"`
	Client      *docker.Client `json:"-"`
	Container   string         `hash:"true"`
	User        string         `default:"root" hash:"true"`
	TTY         bool           `default:"false" hash:"true"`
	Environment []string       `mapstructure:"environment"`
	// contains filtered or unexported fields
}

func NewExecJob

func NewExecJob(c *docker.Client) *ExecJob

func (*ExecJob) Middlewares

func (c *ExecJob) Middlewares() []Middleware

func (*ExecJob) Run

func (j *ExecJob) Run(ctx *Context) error

func (*ExecJob) Use

func (c *ExecJob) Use(ms ...Middleware)

type Execution

type Execution struct {
	ID        string
	Date      time.Time
	Duration  time.Duration
	IsRunning bool
	Failed    bool
	Skipped   bool
	Error     error

	OutputStream, ErrorStream *circbuf.Buffer `json:"-"`
}

Execution contains all the information relative to a Job execution.

func NewExecution

func NewExecution() (*Execution, error)

NewExecution returns a new Execution, with a random ID

func (*Execution) Start

func (e *Execution) Start()

Start starts the execution, initializes the running flags and the start date.

func (*Execution) Stop

func (e *Execution) Stop(err error)

Stop halts the execution. If a ErrSkippedExecution is given the execution is marked as skipped; if any other error is given the execution is marked as failed. Also mark the execution as IsRunning false and save the duration time

type Job

type Job interface {
	GetName() string
	GetSchedule() string
	GetCommand() string
	Middlewares() []Middleware
	Use(...Middleware)
	Run(*Context) error
	Running() int32
	NotifyStart()
	NotifyStop()
	GetCronJobID() int
	SetCronJobID(int)
	GetHistory() []*Execution
}

type LocalJob

type LocalJob struct {
	BareJob     `mapstructure:",squash"`
	Dir         string
	Environment []string `mapstructure:"environment"`
}

func NewLocalJob

func NewLocalJob() *LocalJob

func (*LocalJob) Middlewares

func (c *LocalJob) Middlewares() []Middleware

func (*LocalJob) Run

func (j *LocalJob) Run(ctx *Context) error

func (*LocalJob) Use

func (c *LocalJob) Use(ms ...Middleware)

type Logger

type Logger interface {
	Criticalf(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Noticef(format string, args ...interface{})
	Warningf(format string, args ...interface{})
}

type LogrusAdapter added in v0.8.0

type LogrusAdapter struct {
	*logrus.Logger
}

LogrusAdapter wraps a logrus.Logger to satisfy the Logger interface.

func (*LogrusAdapter) Criticalf added in v0.8.0

func (l *LogrusAdapter) Criticalf(format string, args ...interface{})

func (*LogrusAdapter) Debugf added in v0.8.0

func (l *LogrusAdapter) Debugf(format string, args ...interface{})

func (*LogrusAdapter) Errorf added in v0.8.0

func (l *LogrusAdapter) Errorf(format string, args ...interface{})

func (*LogrusAdapter) Noticef added in v0.8.0

func (l *LogrusAdapter) Noticef(format string, args ...interface{})

func (*LogrusAdapter) Warningf added in v0.8.0

func (l *LogrusAdapter) Warningf(format string, args ...interface{})

type Middleware

type Middleware interface {
	// Run is called instead of the original `Job.Run`, you MUST call to `ctx.Run`
	// inside of the middleware `Run` function otherwise you will broken the
	// Job workflow.
	Run(*Context) error
	// ContinueOnStop reports whether Run should be called even when the
	// execution has been stopped
	ContinueOnStop() bool
}

Middleware can wrap any job execution, allowing to execution code before or/and after of each `Job.Run`

type RunJob

type RunJob struct {
	BareJob `mapstructure:",squash"`
	Client  *docker.Client `json:"-"`
	User    string         `default:"root"`

	// ContainerName specifies the name of the container to be created. If
	// nil, the job name will be used. If set to an empty string, Docker
	// will assign a random name.
	ContainerName *string `gcfg:"container-name" mapstructure:"container-name"`

	TTY bool `default:"false"`

	// do not use bool values with "default:true" because if
	// user would set it to "false" explicitly, it still will be
	// changed to "true" https://github.com/netresearch/ofelia/issues/135
	// so lets use strings here as workaround
	Delete string `default:"true"`
	Pull   string `default:"true"`

	Image       string
	Network     string
	Hostname    string
	Entrypoint  *string
	Container   string
	Volume      []string
	VolumesFrom []string `gcfg:"volumes-from" mapstructure:"volumes-from,"`
	Environment []string `mapstructure:"environment"`
	// contains filtered or unexported fields
}

func NewRunJob

func NewRunJob(c *docker.Client) *RunJob

func (*RunJob) Middlewares

func (c *RunJob) Middlewares() []Middleware

func (*RunJob) Run

func (j *RunJob) Run(ctx *Context) error

func (*RunJob) Use

func (c *RunJob) Use(ms ...Middleware)

type RunServiceJob

type RunServiceJob struct {
	BareJob `mapstructure:",squash"`
	Client  *docker.Client `json:"-"`
	User    string         `default:"root"`
	TTY     bool           `default:"false"`
	// do not use bool values with "default:true" because if
	// user would set it to "false" explicitly, it still will be
	// changed to "true" https://github.com/netresearch/ofelia/issues/135
	// so lets use strings here as workaround
	Delete  string `default:"true"`
	Image   string
	Network string
}

func NewRunServiceJob

func NewRunServiceJob(c *docker.Client) *RunServiceJob

func (*RunServiceJob) Middlewares

func (c *RunServiceJob) Middlewares() []Middleware

func (*RunServiceJob) Run

func (j *RunServiceJob) Run(ctx *Context) error

func (*RunServiceJob) Use

func (c *RunServiceJob) Use(ms ...Middleware)

type Scheduler

type Scheduler struct {
	Jobs     []Job
	Removed  []Job
	Disabled []Job
	Logger   Logger
	// contains filtered or unexported fields
}

func NewScheduler

func NewScheduler(l Logger) *Scheduler

func (*Scheduler) AddJob

func (s *Scheduler) AddJob(j Job) error

func (*Scheduler) DisableJob added in v0.8.0

func (s *Scheduler) DisableJob(name string) error

DisableJob stops scheduling the job but keeps it for later enabling.

func (*Scheduler) EnableJob added in v0.8.0

func (s *Scheduler) EnableJob(name string) error

EnableJob schedules a previously disabled job.

func (*Scheduler) Entries added in v0.8.0

func (s *Scheduler) Entries() []cron.Entry

Entries returns all scheduled cron entries.

func (*Scheduler) GetDisabledJob added in v0.8.0

func (s *Scheduler) GetDisabledJob(name string) Job

GetDisabledJob returns a disabled job by name.

func (*Scheduler) GetDisabledJobs added in v0.8.0

func (s *Scheduler) GetDisabledJobs() []Job

GetDisabledJobs returns a copy of all disabled jobs.

func (*Scheduler) GetJob added in v0.8.0

func (s *Scheduler) GetJob(name string) Job

GetJob returns an active job by name.

func (*Scheduler) GetRemovedJobs added in v0.8.0

func (s *Scheduler) GetRemovedJobs() []Job

GetRemovedJobs returns a copy of all jobs that were removed from the scheduler.

func (*Scheduler) IsRunning

func (s *Scheduler) IsRunning() bool

IsRunning returns true if the scheduler is active.

func (*Scheduler) Middlewares

func (c *Scheduler) Middlewares() []Middleware

func (*Scheduler) RemoveJob

func (s *Scheduler) RemoveJob(j Job) error

func (*Scheduler) RunJob added in v0.8.0

func (s *Scheduler) RunJob(name string) error

RunJob manually executes a job by name.

func (*Scheduler) Start

func (s *Scheduler) Start() error

func (*Scheduler) Stop

func (s *Scheduler) Stop() error

func (*Scheduler) Use

func (c *Scheduler) Use(ms ...Middleware)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL