cron

package
v0.0.0-...-0183129 Latest Latest
Warning

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

Go to latest
Published: May 25, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CRON = core.EXTERN

CRON is a crude logging component, which really is just the EXTERN catch-all from core/log.go.

View Source
var SysCronBroadcaster = NewCronBroadcaster()

SysCronBroadcaster is a default, shared broadcaster.

Functions

func AddHooks

func AddHooks(ctx *core.Context, cronner Cronner, state core.State) error

func ParseSchedule

func ParseSchedule(schedule string) (string, map[string]string, error)

Types

type APIProcessEventRequest

type APIProcessEventRequest struct {
	URI      string      `json:"uri"`
	Location string      `json:"location"`
	Event    interface{} `json:"event"`
}

APIProcessEventRequest helps to serialize a request for the simple ProcessEvent HTTP API.

type CroltJob

type CroltJob struct {
	Account  string            `json:"account"`
	Id       string            `json:"id"`
	URL      string            `json:"url"`
	Schedule string            `json:"schedule"`
	Method   string            `json:"method"`
	Header   map[string]string `json:"header"`
	Body     string            `json:"requestBody"`
}

CroltJob is the structure of a Crolt job.

We could use the actual Crolt type. Instead, we pretend that we don't have access to it.

type CroltSimple

type CroltSimple struct {
	// CroltURL points to the cron service.
	CroltURL string

	// RulesURL points to a rules endpoint that speaks the
	// primitive API.
	RulesURL string
}

func (*CroltSimple) Persistent

func (c *CroltSimple) Persistent() bool

func (*CroltSimple) Rem

func (c *CroltSimple) Rem(ctx *core.Context, id string) (bool, error)

Rem removes the job with the given id from crolt.

The boolean return value isn't particularly meaningful (yet?).

func (*CroltSimple) Schedule

func (c *CroltSimple) Schedule(ctx *core.Context, work *ScheduledWork) error

Schedule makes the Crolt request to create a new job.

func (*CroltSimple) ScheduleEvent

func (c *CroltSimple) ScheduleEvent(ctx *core.Context, se *ScheduledEvent) error

ScheduleEvent packages up a ProcessEvent call for the simple rules HTTP API.

type Cron

type Cron struct {
	sync.Mutex
	Timeline

	// PauseDuration determines how long a 'pause' command pauses the processing loop.
	// (We have the suffix "Duration" to distinguish from the method.)
	PauseDuration time.Duration

	// Name is opaque; just used for logging.
	Name string

	// The approximate maximum number pending jobs.
	Limit int
	// contains filtered or unexported fields
}

Cron implements a little in-memory cron system.

Not persistent; not fair.

func NewCron

func NewCron(broadcaster *CronBroadcaster, pause time.Duration, name string, limit int) (*Cron, error)

NewCron creates a new Cron instanced.

If the given broadcaster in nil, SysCronBroadcaster is used. Name is opaque; just for logging.

func (*Cron) Add

func (c *Cron) Add(ctx *core.Context, id string, schedule string, f func(t time.Time) error) error

Add creates a new cron job.

Use the ID to Rem() that job later if you want. F is the work to be performed. The first argument is the scheduled time for that invocation, and the second argument is true if the job is a one-shot job.

The schedule syntax can have three forms:

1. A cron schedule string (supposedly in syntax at https://en.wikipedia.org/wiki/Cron).

2. "!TIME", where TIME is according to RFC3339.

3. "+DURATION", where DURATION is a Go Duration (http://golang.org/pkg/time/#ParseDuration). Examples: "5s" means "5 seconds", "2m" means "2 minutes", and "1h" means "1 hour".

func (*Cron) Kill

func (c *Cron) Kill(ctx *core.Context) error

Kill the instance's loop forever.

No going back.

func (*Cron) Pause

func (c *Cron) Pause(ctx *core.Context) error

Pause the instance for c.PauseDuration.

func (*Cron) PendingCount

func (c *Cron) PendingCount() int

PendingCount returns the number of known cron jobs.

func (*Cron) Rem

func (c *Cron) Rem(ctx *core.Context, id string) (bool, error)

Rem deletes the job with the given ID.

Returns true if the job was found and false if not.

func (*Cron) Resume

func (c *Cron) Resume(ctx *core.Context) error

Resume restarts the instance's processing loop.

func (*Cron) Start

func (c *Cron) Start(ctx *core.Context)

Start starts up a goroutine that operates the cron system.

This method returns after the goroutine is started.

You need to call this method for the cron system to run any jobs.

func (*Cron) Suspend

func (c *Cron) Suspend(ctx *core.Context) error

Suspend stops the instance's processing loop.

Restart processing with Resume().

type CronBroadcaster

type CronBroadcaster struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

CronBroadcaster can broadcast a signal to multiple Cron instances.

The basis for the implementation was suggested by Bill. All channel readers hear when the channel is closed. Since we want to send more than one signal, we create another channel when the current one is closed.

func NewCronBroadcaster

func NewCronBroadcaster() *CronBroadcaster

func (*CronBroadcaster) Get

func (b *CronBroadcaster) Get() (*chan struct{}, bool)

Get returns the broadcaster's current channel.

Cron instances call this method after each channel close that is detected.

func (*CronBroadcaster) Resume

func (b *CronBroadcaster) Resume() bool

Resume broadcasts that command to all Cron instances with this broadcaster.

func (*CronBroadcaster) Suspend

func (b *CronBroadcaster) Suspend() bool

Suspend broadcasts that command to all Cron instances with this broadcaster.

type CronJob

type CronJob struct {
	// Id should be same as the fact Id that created this job.
	Id string

	// Schedule is the cron schedule string.
	Schedule string

	// Expression is the parsed schedule.
	Expression *cronexpr.Expression

	// When this job should run.
	// If the job is a one-shot job, Expression will be nil.
	Next time.Time

	// Fn is the work to be performed.
	//
	// Argument is the scheduled time.
	Fn func(time.Time) error

	// Err holds the error returned by the last invocation of Fn.
	Err error
}

CronJob packages up the basics for a job. In particular, we need a Go function that defines the work to be performed.

func (*CronJob) Once

func (job *CronJob) Once() bool

Once is a little check to see if a job should only be executed once.

The test is if job.Expression is nil.

type Cronner

type Cronner interface {
	// ScheduleEvent schedules a rule engine event.
	//
	// We have this special, API-specific method due to our desire
	// to have cron functionality that does not require an HTTP
	// API.
	ScheduleEvent(ctx *core.Context, work *ScheduledEvent) error

	// Schedule is a generic scheduler for HTTP request work.
	Schedule(ctx *core.Context, work *ScheduledWork) error

	// Rem removes the job with the given id.
	//
	// The boolean return value might indicate whether the job was
	// found.  Probably isn't meaningful.
	Rem(ctx *core.Context, id string) (bool, error)

	// Persistent reports whether the Cronner's state is
	// persistent or ephemeral.
	//
	// A persistent Cronner will not get updates when Locations
	// are loaded (since such a Cronner would have gotten updates
	// when the API calls occurred).
	Persistent() bool
}

Cronner is a generic cron service.

We'll probably add some other methods later.

For example, we might want a method that enables us to remove all jobs for a given location. We might also want special methods for other engine APIs.

type InternalCron

type InternalCron struct {
	Cron *Cron
}

InternalCron just wraps an internal cron so it can be a Cronner.

func (*InternalCron) Persistent

func (c *InternalCron) Persistent() bool

func (*InternalCron) Rem

func (c *InternalCron) Rem(ctx *core.Context, id string) (bool, error)

func (*InternalCron) Schedule

func (c *InternalCron) Schedule(ctx *core.Context, sw *ScheduledWork) error

func (*InternalCron) ScheduleEvent

func (c *InternalCron) ScheduleEvent(ctx *core.Context, se *ScheduledEvent) error

type ScheduledEvent

type ScheduledEvent struct {
	// Id is an optional transaction id.
	Id string

	// Event is a JSON representation of an event.
	Event string

	// Schedule is the schedule for the event in our extended cron
	// syntax.
	Schedule string
}

ScheduledEvent is a scheduled rule engine event.

type ScheduledWork

type ScheduledWork struct {
	Work
	WorkControl
}

ScheduledWork associates Work with its WorkControl.

type Timeline

type Timeline []*CronJob

Timeline is the time-order list of pending CronJobs.

func (Timeline) Len

func (tl Timeline) Len() int

func (Timeline) Less

func (tl Timeline) Less(i, j int) bool

func (Timeline) Search

func (tl Timeline) Search(t time.Time) int

func (Timeline) Swap

func (tl Timeline) Swap(i, j int)

type Work

type Work struct {
	// URL is the target for the request.
	URL string `json:"url"`

	// Method is the HTTP method for the request.
	Method string `json:"method"`

	// Headers are added to the request headers.
	//
	// ToDo: Values should be []string, not string.
	Headers map[string]string `json:"headers"`

	// Body is the optional body for the HTTP request.
	Body string `json:"body"`
}

Work represents a basic HTTP request that an internal or external cron can issue as a cron job.

We'll likely need to add to this struct (or another) to support additional HTTP request properties such as timeouts.

type WorkControl

type WorkControl struct {
	// Schedule is a string in our extended cron syntax.
	//
	// See elsewhere (?) for that syntax.
	//
	// Basically a cron schedule, "!timestamp", or "+duration".
	Schedule string `json:"schedule"`

	// Tag is an optional label that a cron system might use to
	// facilitate access to a batch of jobs.  For example, the
	// location's name, which could enable the deletion of all
	// jobs in that location.
	Tag string `json:"tag"`

	// Id is a an optional id for the job.
	//
	// ToDo: Elaborate.
	Id string `json:"id"`
}

WorkControl includes the schedule and perhaps an id for the job.

Jump to

Keyboard shortcuts

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