schedule
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
package schedule provides utilities for managing template and workspaceautostart and autostop schedules. This includes utilities for parsing anddeserializing cron-style expressions.
Index¶
- Constants
- Variables
- func GetMondayOfWeek(loc *time.Location, n int64) (time.Time, error)
- func GetNextApplicableMondayOfNWeeks(now time.Time, n int64) (time.Time, error)
- func TemplateRestartRequirementEpoch(loc *time.Location) time.Time
- func VerifyTemplateRestartRequirement(days uint8, weeks int64) error
- func WeeksSinceEpoch(now time.Time) (int64, error)
- type AutostopTime
- type CalculateAutostopParams
- type MockTemplateScheduleStore
- type MockUserQuietHoursScheduleStore
- type Schedule
- type TemplateRestartRequirement
- type TemplateScheduleOptions
- type TemplateScheduleStore
- type UserQuietHoursScheduleOptions
- type UserQuietHoursScheduleStore
Constants¶
const MaxTemplateRestartRequirementWeeks = 16
Variables¶
var DaysOfWeek = []time.Weekday{time.Monday,time.Tuesday,time.Wednesday,time.Thursday,time.Friday,time.Saturday,time.Sunday,}
DaysOfWeek intentionally starts on Monday as opposed to Sunday so the weekenddays are contiguous in the bitmap. This matters greatly when doing restartsevery second week or more to avoid workspaces restarting "at the start" ofthe week rather than "at the end" of the week.
Functions¶
funcGetMondayOfWeek¶added inv0.26.2
GetMondayOfWeek gets the Monday (0:00) of the n-th week since epoch.
funcGetNextApplicableMondayOfNWeeks¶added inv0.26.2
GetNextApplicableMondayOfNWeeks gets the next Monday (0:00) of the next weekdivisible by n since epoch. If the next applicable week is invalid for anyreason, the week after will be used instead (up to 2 attempts).
If the current week is divisible by n, then the provided time is returned asis.
The timezone embedded in the time object is used to determine the epoch.
funcTemplateRestartRequirementEpoch¶added inv0.26.2
funcVerifyTemplateRestartRequirement¶added inv0.26.2
VerifyTemplateRestartRequirement returns an error if the restart requirementis invalid.
Types¶
typeAutostopTime¶added inv0.26.2
type AutostopTime struct {// Deadline is the time when the workspace will be stopped. The value can be// bumped by user activity or manually by the user via the UI.Deadlinetime.Time// MaxDeadline is the maximum value for deadline.MaxDeadlinetime.Time}
funcCalculateAutostop¶added inv0.26.2
func CalculateAutostop(ctxcontext.Context, paramsCalculateAutostopParams) (AutostopTime,error)
CalculateAutostop calculates the deadline and max_deadline for a workspacebuild.
Deadline is the time when the workspace will be stopped, as long as itdoesn't see any new activity (such as SSH, app requests, etc.). When activityis detected the deadline is bumped by the workspace's TTL (this only happenswhen activity is detected and more than 20% of the TTL has passed to savedatabase queries).
MaxDeadline is the maximum value for deadline. The deadline cannot be bumpedpast this value, so it denotes the absolute deadline that the workspace buildmust be stopped by. MaxDeadline is calculated using the template's "restartrequirement" settings and the user's "quiet hours" settings to pick a timeoutside of working hours.
Deadline is a cost saving measure, while max deadline is acompliance/updating measure.
typeCalculateAutostopParams¶added inv0.26.2
type CalculateAutostopParams struct {Databasedatabase.StoreTemplateScheduleStoreTemplateScheduleStoreUserQuietHoursScheduleStoreUserQuietHoursScheduleStoreNowtime.TimeWorkspacedatabase.Workspace}
typeMockTemplateScheduleStore¶added inv0.22.0
type MockTemplateScheduleStore struct {GetFn func(ctxcontext.Context, dbdatabase.Store, templateIDuuid.UUID) (TemplateScheduleOptions,error)SetFn func(ctxcontext.Context, dbdatabase.Store, templatedatabase.Template, optionsTemplateScheduleOptions) (database.Template,error)}
typeMockUserQuietHoursScheduleStore¶added inv0.26.2
type MockUserQuietHoursScheduleStore struct {GetFn func(ctxcontext.Context, dbdatabase.Store, userIDuuid.UUID) (UserQuietHoursScheduleOptions,error)SetFn func(ctxcontext.Context, dbdatabase.Store, userIDuuid.UUID, schedulestring) (UserQuietHoursScheduleOptions,error)}
typeSchedule¶
type Schedule struct {// contains filtered or unexported fields}
Schedule represents a cron schedule.It's essentially a wrapper for robfig/cron/v3 that has additionalconvenience methods.
funcDaily¶added inv0.26.2
Daily parses a Schedule from spec scoped to a recurring daily event.Spec consists of the following space-delimited fields, in the following order:- timezone e.g. CRON_TZ=US/Central (optional)- minutes of hour e.g. 30 (required)- hour of day e.g. 9 (required)- day of month (must be *)- month (must be *)- day of week (must be *)
Example Usage:
local_sched, _ := schedule.Weekly("59 23 * * *")fmt.Println(sched.Next(time.Now().Format(time.RFC3339)))// Output: 2022-04-04T23:59:00Zus_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 * * *")fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))// Output: 2022-04-04T14:30:00Z
funcWeekly¶
Weekly parses a Schedule from spec scoped to a recurring weekly event.Spec consists of the following space-delimited fields, in the following order:- timezone e.g. CRON_TZ=US/Central (optional)- minutes of hour e.g. 30 (required)- hour of day e.g. 9 (required)- day of month (must be *)- month (must be *)- day of week e.g. 1 (required)
Example Usage:
local_sched, _ := schedule.Weekly("59 23 *")fmt.Println(sched.Next(time.Now().Format(time.RFC3339)))// Output: 2022-04-04T23:59:00Zus_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 1-5")fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))// Output: 2022-04-04T14:30:00Z
func (Schedule)Cron¶
Cron returns the cron spec for the schedule with the leading CRON_TZstripped, if present.
func (Schedule)DaysOfWeek¶
DaysOfWeek returns a humanized form of the day-of-week field.
func (Schedule)Min¶
Min returns the minimum duration of the schedule.This is calculated as follows:
- Let t(0) be a given point in time (1970-01-01T01:01:01Z00:00)
- Let t(max) be 168 hours after t(0).
- Let t(1) be the next scheduled time after t(0).
- Let t(n) be the next scheduled time after t(n-1).
- Then, the minimum duration of s d(min)= min( t(n) - t(n-1) ∀ n ∈ N, t(n) < t(max) )
typeTemplateRestartRequirement¶added inv0.26.2
type TemplateRestartRequirement struct {// DaysOfWeek is a bitmap of which days of the week the workspace must be// restarted. If fully zero, the workspace is not required to be restarted// ever.//// First bit is Monday, ..., seventh bit is Sunday, eighth bit is unused.DaysOfWeekuint8// Weeks is the amount of weeks between restarts. If 0 or 1, the workspace// is restarted weekly in accordance with DaysOfWeek. If 2, the workspace is// restarted every other week. And so forth.//// The limit for this value is 16, which is roughly 4 months.//// The "first week" starts on January 2nd, 2023, which is the first Monday// of 2023. All other weeks are counted using modulo arithmetic from that// date.Weeksint64}
typeTemplateScheduleOptions¶
type TemplateScheduleOptions struct {UserAutostartEnabledbool `json:"user_autostart_enabled"`UserAutostopEnabledbool `json:"user_autostop_enabled"`DefaultTTLtime.Duration `json:"default_ttl"`// TODO(@dean): remove MaxTTL once restart_requirement is matured and the// defaultMaxTTLtime.Duration `json:"max_ttl"`// UseRestartRequirement dictates whether the restart requirement should be// used instead of MaxTTL. This is governed by the feature flag and// licensing.// TODO(@dean): remove this when we remove max_tllUseRestartRequirementbool// RestartRequirement dictates when the workspace must be restarted. This// used to be handled by MaxTTL.RestartRequirementTemplateRestartRequirement `json:"restart_requirement"`// FailureTTL dictates the duration after which failed workspaces will be// stopped automatically.FailureTTLtime.Duration `json:"failure_ttl"`// InactivityTTL dictates the duration after which inactive workspaces will// be locked.InactivityTTLtime.Duration `json:"inactivity_ttl"`// LockedTTL dictates the duration after which locked workspaces will be// permanently deleted.LockedTTLtime.Duration `json:"locked_ttl"`}
typeTemplateScheduleStore¶
type TemplateScheduleStore interface {Get(ctxcontext.Context, dbdatabase.Store, templateIDuuid.UUID) (TemplateScheduleOptions,error)Set(ctxcontext.Context, dbdatabase.Store, templatedatabase.Template, optsTemplateScheduleOptions) (database.Template,error)}
TemplateScheduleStore provides an interface for retrieving templatescheduling options set by the template/site admin.
funcNewAGPLTemplateScheduleStore¶
func NewAGPLTemplateScheduleStore()TemplateScheduleStore
typeUserQuietHoursScheduleOptions¶added inv0.26.2
type UserQuietHoursScheduleOptions struct {// Schedule is the cron schedule to use for quiet hours windows for all// workspaces owned by the user.//// This value will be set to the parsed custom schedule of the user. If the// user doesn't have a custom schedule set, it will be set to the default// schedule (and UserSet will be false). If quiet hours schedules are not// entitled or disabled instance-wide, this value will be nil to denote that// quiet hours windows should not be used.Schedule *ScheduleUserSetbool}
typeUserQuietHoursScheduleStore¶added inv0.26.2
type UserQuietHoursScheduleStore interface {// Get retrieves the quiet hours schedule for the given user. If the user// has not set a custom schedule, the default schedule will be returned. If// quiet hours schedules are not entitled or disabled instance-wide, this// will return a nil schedule.Get(ctxcontext.Context, dbdatabase.Store, userIDuuid.UUID) (UserQuietHoursScheduleOptions,error)// Set sets the quiet hours schedule for the given user. If the given// schedule is an empty string, the user's custom schedule will be cleared// and the default schedule will be used from now on. If quiet hours// schedules are not entitled or disabled instance-wide, this will do// nothing and return a nil schedule.Set(ctxcontext.Context, dbdatabase.Store, userIDuuid.UUID, rawSchedulestring) (UserQuietHoursScheduleOptions,error)}
funcNewAGPLUserQuietHoursScheduleStore¶added inv0.26.2
func NewAGPLUserQuietHoursScheduleStore()UserQuietHoursScheduleStore