schedule

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const MaxTemplateAutostopRequirementWeeks = 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

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

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 TemplateAutostopRequirementEpoch added in v2.1.5

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

func VerifyTemplateAutostopRequirement added in v2.1.5

func VerifyTemplateAutostopRequirement(days uint8, weeks int64) error

VerifyTemplateAutostopRequirement returns an error if the autostop requirement is invalid.

func WeeksSinceEpoch

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

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

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 "autostop 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

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

	Now       time.Time
	Workspace database.Workspace
}

type MockTemplateScheduleStore

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

func (MockTemplateScheduleStore) Set

type MockUserQuietHoursScheduleStore

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

func (MockUserQuietHoursScheduleStore) Set

type TemplateAutostopRequirement added in v2.1.5

type TemplateAutostopRequirement 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 (TemplateAutostopRequirement) DaysMap added in v2.1.5

func (r TemplateAutostopRequirement) 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 autostop_requirement is matured and the
	// default
	MaxTTL time.Duration `json:"max_ttl"`
	// UseAutostopRequirement dictates whether the autostop 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
	UseAutostopRequirement bool
	// AutostopRequirement dictates when the workspace must be restarted. This
	// used to be handled by MaxTTL.
	AutostopRequirement TemplateAutostopRequirement `json:"autostop_requirement"`
	// FailureTTL dictates the duration after which failed workspaces will be
	// stopped automatically.
	FailureTTL time.Duration `json:"failure_ttl"`
	// TimeTilDormant dictates the duration after which inactive workspaces will
	// go dormant.
	TimeTilDormant time.Duration `json:"time_til_dormant"`
	// TimeTilDormantAutoDelete dictates the duration after which dormant workspaces will be
	// permanently deleted.
	TimeTilDormantAutoDelete time.Duration `json:"time_til_dormant_autodelete"`
	// UpdateWorkspaceLastUsedAt updates the template's workspaces'
	// last_used_at field. This is useful for preventing updates to the
	// templates inactivity_ttl immediately triggering a dormant action against
	// workspaces whose last_used_at field violates the new template
	// inactivity_ttl threshold.
	UpdateWorkspaceLastUsedAt bool `json:"update_workspace_last_used_at"`
	// UpdateWorkspaceDormantAt updates the template's workspaces'
	// dormant_at field. This is useful for preventing updates to the
	// templates locked_ttl immediately triggering a delete action against
	// workspaces whose dormant_at field violates the new template time_til_dormant_autodelete
	// threshold.
	UpdateWorkspaceDormantAt bool `json:"update_workspace_dormant_at"`
}

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

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 *cron.Schedule
	UserSet  bool
}

type UserQuietHoursScheduleStore

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

func NewAGPLUserQuietHoursScheduleStore() UserQuietHoursScheduleStore

Directories

Path Synopsis
package schedule provides utilities for managing template and workspace autostart and autostop schedules.
package schedule provides utilities for managing template and workspace autostart and autostop schedules.

Jump to

Keyboard shortcuts

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