schedule

package
v0.27.3 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2023 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

package schedule provides utilities for managing template and workspace autostart and autostop schedules. This includes utilities for parsing and deserializing cron-style expressions.

Index

Constants

View Source
const MaxTemplateRestartRequirementWeeks = 16

Variables

DaysOfWeek intentionally starts on Monday as opposed to Sunday so the weekend days are contiguous in the bitmap. This matters greatly when doing restarts every second week or more to avoid workspaces restarting "at the start" of the week rather than "at the end" of the week.

Functions

func GetMondayOfWeek added in v0.26.2

func GetMondayOfWeek(loc *time.Location, n int64) (time.Time, error)

GetMondayOfWeek gets the Monday (0:00) of the n-th week since epoch.

func GetNextApplicableMondayOfNWeeks added in v0.26.2

func GetNextApplicableMondayOfNWeeks(now time.Time, n int64) (time.Time, error)

GetNextApplicableMondayOfNWeeks gets the next Monday (0:00) of the next week divisible by n since epoch. If the next applicable week is invalid for any reason, 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 as is.

The timezone embedded in the time object is used to determine the epoch.

func TemplateRestartRequirementEpoch added in v0.26.2

func TemplateRestartRequirementEpoch(loc *time.Location) time.Time

func VerifyTemplateRestartRequirement added in v0.26.2

func VerifyTemplateRestartRequirement(days uint8, weeks int64) error

VerifyTemplateRestartRequirement returns an error if the restart requirement is invalid.

func WeeksSinceEpoch added in v0.26.2

func WeeksSinceEpoch(now time.Time) (int64, error)

WeeksSinceEpoch gets the weeks since the epoch for a given time. This is a 0-indexed number of weeks since the epoch (Monday).

The timezone embedded in the time object is used to determine the epoch.

Types

type AutostopTime added in v0.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.
	Deadline time.Time
	// MaxDeadline is the maximum value for deadline.
	MaxDeadline time.Time
}

func CalculateAutostop added in v0.26.2

func CalculateAutostop(ctx context.Context, params CalculateAutostopParams) (AutostopTime, error)

CalculateAutostop calculates the deadline and max_deadline for a workspace build.

Deadline is the time when the workspace will be stopped, as long as it doesn't see any new activity (such as SSH, app requests, etc.). When activity is detected the deadline is bumped by the workspace's TTL (this only happens when activity is detected and more than 20% of the TTL has passed to save database queries).

MaxDeadline is the maximum value for deadline. The deadline cannot be bumped past this value, so it denotes the absolute deadline that the workspace build must be stopped by. MaxDeadline is calculated using the template's "restart requirement" settings and the user's "quiet hours" settings to pick a time outside of working hours.

Deadline is a cost saving measure, while max deadline is a compliance/updating measure.

type CalculateAutostopParams added in v0.26.2

type CalculateAutostopParams struct {
	Database                    database.Store
	TemplateScheduleStore       TemplateScheduleStore
	UserQuietHoursScheduleStore UserQuietHoursScheduleStore

	Now       time.Time
	Workspace database.Workspace
}

type MockTemplateScheduleStore added in v0.22.0

type MockTemplateScheduleStore struct {
	GetFn func(ctx context.Context, db database.Store, templateID uuid.UUID) (TemplateScheduleOptions, error)
	SetFn func(ctx context.Context, db database.Store, template database.Template, options TemplateScheduleOptions) (database.Template, error)
}

func (MockTemplateScheduleStore) Get added in v0.26.2

func (MockTemplateScheduleStore) Set added in v0.26.2

type MockUserQuietHoursScheduleStore added in v0.26.2

type MockUserQuietHoursScheduleStore struct {
	GetFn func(ctx context.Context, db database.Store, userID uuid.UUID) (UserQuietHoursScheduleOptions, error)
	SetFn func(ctx context.Context, db database.Store, userID uuid.UUID, schedule string) (UserQuietHoursScheduleOptions, error)
}

func (MockUserQuietHoursScheduleStore) Get added in v0.26.2

func (MockUserQuietHoursScheduleStore) Set added in v0.26.2

type Schedule

type Schedule struct {
	// contains filtered or unexported fields
}

Schedule represents a cron schedule. It's essentially a wrapper for robfig/cron/v3 that has additional convenience methods.

func Daily added in v0.26.2

func Daily(raw string) (*Schedule, error)

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:00Z

us_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 * * *")
fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))
// Output: 2022-04-04T14:30:00Z

func Weekly

func Weekly(raw string) (*Schedule, error)

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:00Z

us_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

func (s Schedule) Cron() string

Cron returns the cron spec for the schedule with the leading CRON_TZ stripped, if present.

func (Schedule) DaysOfWeek

func (s Schedule) DaysOfWeek() string

DaysOfWeek returns a humanized form of the day-of-week field.

func (Schedule) Location

func (s Schedule) Location() *time.Location

Location returns the IANA location for the schedule.

func (Schedule) Min

func (s Schedule) Min() time.Duration

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) )

func (Schedule) Next

func (s Schedule) Next(t time.Time) time.Time

Next returns the next time in the schedule relative to t.

func (Schedule) String

func (s Schedule) String() string

String serializes the schedule to its original human-friendly format. The leading CRON_TZ is maintained.

func (Schedule) Time

func (s Schedule) Time() string

Time returns a humanized form of the minute and hour fields.

type TemplateRestartRequirement added in v0.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.
	DaysOfWeek uint8
	// 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.
	Weeks int64
}

func (TemplateRestartRequirement) DaysMap added in v0.26.2

func (r TemplateRestartRequirement) DaysMap() map[time.Weekday]bool

DaysMap returns a map of the days of the week that the workspace must be restarted.

type TemplateScheduleOptions

type TemplateScheduleOptions struct {
	UserAutostartEnabled bool          `json:"user_autostart_enabled"`
	UserAutostopEnabled  bool          `json:"user_autostop_enabled"`
	DefaultTTL           time.Duration `json:"default_ttl"`
	// TODO(@dean): remove MaxTTL once restart_requirement is matured and the
	// default
	MaxTTL time.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_tll
	UseRestartRequirement bool
	// RestartRequirement dictates when the workspace must be restarted. This
	// used to be handled by MaxTTL.
	RestartRequirement TemplateRestartRequirement `json:"restart_requirement"`
	// FailureTTL dictates the duration after which failed workspaces will be
	// stopped automatically.
	FailureTTL time.Duration `json:"failure_ttl"`
	// InactivityTTL dictates the duration after which inactive workspaces will
	// be locked.
	InactivityTTL time.Duration `json:"inactivity_ttl"`
	// LockedTTL dictates the duration after which locked workspaces will be
	// permanently deleted.
	LockedTTL time.Duration `json:"locked_ttl"`
}

type TemplateScheduleStore

type TemplateScheduleStore interface {
	Get(ctx context.Context, db database.Store, templateID uuid.UUID) (TemplateScheduleOptions, error)
	Set(ctx context.Context, db database.Store, template database.Template, opts TemplateScheduleOptions) (database.Template, error)
}

TemplateScheduleStore provides an interface for retrieving template scheduling options set by the template/site admin.

func NewAGPLTemplateScheduleStore

func NewAGPLTemplateScheduleStore() TemplateScheduleStore

type UserQuietHoursScheduleOptions added in v0.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 *Schedule
	UserSet  bool
}

type UserQuietHoursScheduleStore added in v0.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(ctx context.Context, db database.Store, userID uuid.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(ctx context.Context, db database.Store, userID uuid.UUID, rawSchedule string) (UserQuietHoursScheduleOptions, error)
}

func NewAGPLUserQuietHoursScheduleStore added in v0.26.2

func NewAGPLUserQuietHoursScheduleStore() UserQuietHoursScheduleStore

Jump to

Keyboard shortcuts

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