cron
v1.1.1
Documentation
pkg.go.dev ↗Package cron implements the database.Model interface for the Cron entity. The Cron entity allows for build's to be submitted on a defined schedule.
Functions
InitEvent
func InitEvent(dis event.Dispatcher) queue.InitFunc
LoadRelations
func LoadRelations(loaders *database.Loaders, cc ...*Cron) error
LoadRelations loads all of the available relations for the given Cron models using the given loaders available.
Model
func Model(cc []*Cron) func(int) database.Model
Model is called along with database.ModelSlice to convert the given slice of Cron models to a slice of database.Model interfaces.
SelectBuild
func SelectBuild(col string, opts ...query.Option) query.Query
SelectBuild returns a SELECT query that will select the given column from the cron_builds table and apply the given query options.
Types
Batcher
type Batcher struct {
// contains filtered or unexported fields
}
Batcher provides a way of retrieving batches of cron jobs that are ready to be executed.
NewBatcher
func NewBatcher(db *sqlx.DB, hasher *crypto.Hasher, limit int64, errh func(error)) *Batcher
NewBatcher returns a new Batcher using the given Store to retrieve cron jobs from, and setting the size of each batch to the given limit.
Batcher.Batch
func (b *Batcher) Batch() []*Cron
Batcher.Err
func (b *Batcher) Err() error
Err returns the current error, if any, that occurred when loading a batch.
Batcher.Invoke
func (b *Batcher) Invoke(ctx context.Context, producers map[string]*curlyq.Producer) int
Invoke will submit a build for each job in the current batch.
Batcher.Load
func (b *Batcher) Load() bool
Load will load in the next batch of cron jobs to be executed (WHERE NOW() >= next_run). This will return false if it reaches the end of the batches in the table. If the end of the table is reached, or if an error happens then false is returned.
Build
type Build struct {
ID int64 `db:"id"`
CronID int64 `db:"cron_id"`
BuildID int64 `db:"build_id"`
CreatedAt time.Time `db:"created_at"`
Cron *Cron `db:"-"`
Build *build.Build `db:"-"`
}
Build is the type that represents a build trigged via a cron job.
Build.Bind
func (b *Build) Bind(mm ...database.Model)
Bind implements the database.Binder interface. This will only bind the model if they are pointers to either Cron or build.Build.
Build.Endpoint
func (b *Build) Endpoint(uri ...string) string
Endpoint implements the database.Model interface.
Build.IsZero
func (b *Build) IsZero() bool
IsZero implements the database.Model interface.
Build.JSON
func (b *Build) JSON(addr string) map[string]interface{}
JSON implements the database.Model interface. This will return the JSON of the bound build.Build model.
Build.Primary
func (b *Build) Primary() (string, int64)
Primary implements the database.Model interface.
Build.SetPrimary
func (b *Build) SetPrimary(_ int64)
SetPrimary implements the database.Model interface.
Build.Values
func (b *Build) Values() map[string]interface{}
Values implements the database.Model interface.
BuildStore
type BuildStore struct {
database.Store
// Cron is the bound Cron model. If not nil this will bind the Cron model to
// any Build models that are created. If not nil this will append a WHERE
// clause on the cron_id column for all SELECT queries performed.
Cron *Cron
// Builds is the bound build.Build model. If not nil this will bind the
// build.Build model to any Build models that are created. If not nil this
// will append a WHERE clause to the build_id column for all SELECT queries
// performed.
Build *build.Build
}
BuildStore is the type for creating Build models in the database.
NewBuildStore
func NewBuildStore(db *sqlx.DB, mm ...database.Model) *BuildStore
NewBuildStore returns a new BuildStore for querying the cron_builds table. Each of the given models if bound to the returned BuildStore.
BuildStore.Bind
func (s *BuildStore) Bind(mm ...database.Model)
Bind implements the database.Binder interface. This will only bind the model if they are pointers to either Cron or build.Build.
Cron
type Cron struct {
ID int64 `db:"id"`
UserID int64 `db:"user_id"`
AuthorID int64 `db:"author_id"`
NamespaceID sql.NullInt64 `db:"namespace_id"`
Name string `db:"name"`
Schedule Schedule `db:"schedule"`
Manifest manifest.Manifest `db:"manifest"`
PrevRun sql.NullTime `db:"prev_run"`
NextRun time.Time `db:"next_run"`
CreatedAt time.Time `db:"created_at"`
Author *user.User `db:"-"`
User *user.User `db:"-"`
Namespace *namespace.Namespace `db:"-"`
}
Cron is the type that represents a cron job that has been created by the user.
FromContext
func FromContext(ctx context.Context) (*Cron, bool)
FromContext returns the Cron model from the given context, if any.
Cron.Bind
func (c *Cron) Bind(mm ...database.Model)
Bind implements the database.Binder interface. This will only bind the model if they are pointers to either user.User or namespace.Namespace.
Cron.Endpoint
func (c *Cron) Endpoint(uri ...string) string
Endpoint returns the endpoint to the current Variable database, with the given URI parts appended to it.
Cron.IsZero
func (c *Cron) IsZero() bool
IsZero implements the database.Model interface.
Cron.JSON
func (c *Cron) JSON(addr string) map[string]interface{}
JSON implements the database.Model interface. This will return a map with the current Cron values under each key. If any of the User, or Namespace bound models exist on the Cron, then the JSON representation of these models will be returned in the map, under the user, and namespace keys respectively.
Cron.Primary
func (c *Cron) Primary() (string, int64)
Primary implements the database.Model interface.
Cron.SetPrimary
func (c *Cron) SetPrimary(id int64)
SetPrimary implements the database.Model interface.
Cron.Values
func (c *Cron) Values() map[string]interface{}
Values implements the database.Model interface. This will return a map with the following values, user_id, namespace_id, name, schedule, manifest, and next_run.
Event
type Event struct {
Cron *Cron
Action string
// contains filtered or unexported fields
}
Event.Name
func (ev *Event) Name() string
Event.Perform
func (ev *Event) Perform() error
Form
type Form struct {
Crons *Store `schema:"-"`
Cron *Cron `schema:"-"`
Name string `schema:"name" json:"name"`
Schedule Schedule `schema:"schedule" json:"schedule"`
Manifest manifest.Manifest `schema:"manifest" json:"manifest"`
}
Form is the type that represents input data for creating and editing a cron job.
Form.Fields
func (f Form) Fields() map[string]string
Fields returns a map of fields for the current Form. This map will contain the Namespace, Name, Schedule, and Manifest fields of the current form.
Form.Validate
func (f Form) Validate() error
Validate will bind a Namespace to the Form's Store, if the Namespace field is present. The presence of the Name field is then checked, followed by a uniqueness check of that field. The present oft he Manifest field is then checked, followed by a validation of that manifest.
Schedule
type Schedule uint
Schedule represents the schedule of the Cron. This will either be Daily, Weekly, or Monthly. Below is how the Cron schedules are handled,
Daily - This will trigger a Cron on the start of the next day Weekly - This will trigger a Cron on the start of the next week Monthly - This will trigger a Cron on the start of the next month
Daily (3 declarations)
const ( Daily Schedule = iota // daily Weekly // weekly Monthly // monthly )
Schedule.Next
func (s Schedule) Next() time.Time
Next returns the next time a schedule will occur. If Daily the start of the next day is returned. If Weekly the start of the next week is returned. If Monthly the start of the next month is returned.
Schedule.Scan
func (s *Schedule) Scan(val interface{}) error
Scan scans the given interface value into a byte slice and will attempt to turn it into the correct Schedule value. If it success then it set's it on the current Schedule, otherwise an error is returned.
Schedule.String
func (i Schedule) String() string
Schedule.UnmarshalJSON
func (s *Schedule) UnmarshalJSON(b []byte) error
UnmarshalJSON attempts to unmarshal the given byte slice as a JSON string, and checks to see if it is a valid schedule of either daily, weekly, or monthly.
Schedule.UnmarshalText
func (s *Schedule) UnmarshalText(b []byte) error
UnmarshalText takes the given byte slice, and attempts to map it to a known Schedule. If it is a known Schedule, then that the current Schedule is set to that, otherwise webutil.UnmarshalError is returned.
Schedule.Value
func (s Schedule) Value() (driver.Value, error)
Value returns the string value of the current Schedule so it can be inserted into the database.
Store
type Store struct {
database.Store
// User is the bound user.User model. If not nil this will bind the
// user.User model to any Cron models that are created. If not nil this
// will append a WHERE clause on the user_id column for all SELECT queries
// performed.
User *user.User
// Namespace is the bound namespace.Namespace model. If not nil this will
// bind the namespace.Namespace model to any Variable models that are
// created. If not nil this will append a WHERE clause on the namespace_id
// column for all SELECT queries performed.
Namespace *namespace.Namespace
}
Store is the type for creating, modifying, and deleting Cron models in the database.
NewStore
func NewStore(db *sqlx.DB, mm ...database.Model) *Store
NewStore returns a new Store for querying the cron table. Each of the given models is bound to the returned Store.
Store.All
func (s *Store) All(opts ...query.Option) ([]*Cron, error)
All returns a slice of Variable models, applying each query.Option that is given. The namespace.WhereCollaborator option is applied to the *user.User bound database, and the database.Where option is applied to the *namespace.Namespace bound database.
Store.Bind
func (s *Store) Bind(mm ...database.Model)
Bind implements the database.Binder interface. This will only bind the model if they are pointers to either user.User or namespace.Namespace.
Store.Create
func (s *Store) Create(authorId int64, name string, sched Schedule, m manifest.Manifest) (*Cron, error)
Create will create a new Cron with the given name, schedule, and manifest.
Store.Delete
func (s *Store) Delete(id int64) error
Delete a cron with the given id.
Store.Get
func (s *Store) Get(opts ...query.Option) (*Cron, error)
All returns a single Cron model, applying each query.Option that is given. The namespace.WhereCollaborator option is applied to the *user.User bound database, and the database.Where option is applied to the *namespace.Namespace bound database.
Store.Index
func (s *Store) Index(vals url.Values, opts ...query.Option) ([]*Cron, database.Paginator, error)
Index returns the paginated results from the cron table depending on the values that are present in url.Values. Detailed below are the values that are used from the given url.Values,
name - This applies the database.Search query.Option using the value of key
Store.Invoke
func (s *Store) Invoke(c *Cron) (*build.Build, error)
Invoke will create a new build for the given Cron if the NextRun time is after the current time. This will add a tag to the created build detailing the name of the Cron, and it's schedule.
Store.Load
func (s *Store) Load(key string, vals []interface{}, load database.LoaderFunc) error
Load loads in a slice of Cron models where the given key is in the list of given vals. Each database is loaded individually via a call to the given load callback. This method calls Store.All under the hood, so any bound models will impact the models being loaded.
Store.New
func (s *Store) New() *Cron
New returns a new Cron binding any non-nil models to it from the current Store.
Store.Paginate
func (s *Store) Paginate(page, limit int64, opts ...query.Option) (database.Paginator, error)
Paginate returns the database.Paginator for the cron table for the given page. This applies the namespace.WhereCollaborator option to the *user.User bound database, and the database.Where option to the *namespace.Namespace bound database.
Store.Update
func (s *Store) Update(id int64, name string, sched Schedule, m manifest.Manifest) error
Update will update the name, schedule, and manifest for the cron with the given id.