isEligibleForAutostart returns true if the workspace should be autostarted.
(user database.User, ws database.Workspace, build database.WorkspaceBuild, job database.ProvisionerJob, templateSchedule schedule.TemplateScheduleOptions, currentTick time.Time)
| 583 | |
| 584 | // isEligibleForAutostart returns true if the workspace should be autostarted. |
| 585 | func isEligibleForAutostart(user database.User, ws database.Workspace, build database.WorkspaceBuild, job database.ProvisionerJob, templateSchedule schedule.TemplateScheduleOptions, currentTick time.Time) bool { |
| 586 | // Don't attempt to autostart workspaces for suspended users. |
| 587 | if user.Status != database.UserStatusActive { |
| 588 | return false |
| 589 | } |
| 590 | |
| 591 | // Don't attempt to autostart failed workspaces. |
| 592 | if job.JobStatus == database.ProvisionerJobStatusFailed { |
| 593 | return false |
| 594 | } |
| 595 | |
| 596 | // If the workspace is dormant we should not autostart it. |
| 597 | if ws.DormantAt.Valid { |
| 598 | return false |
| 599 | } |
| 600 | |
| 601 | // If the last transition for the workspace was not 'stop' then the workspace |
| 602 | // cannot be started. |
| 603 | if build.Transition != database.WorkspaceTransitionStop { |
| 604 | return false |
| 605 | } |
| 606 | |
| 607 | // If autostart isn't enabled, or the schedule isn't valid/populated we can't |
| 608 | // autostart the workspace. |
| 609 | if !templateSchedule.UserAutostartEnabled || !ws.AutostartSchedule.Valid || ws.AutostartSchedule.String == "" { |
| 610 | return false |
| 611 | } |
| 612 | |
| 613 | // Get the next allowed autostart time after the build's creation time, |
| 614 | // based on the workspace's schedule and the template's allowed days. |
| 615 | nextTransition, err := schedule.NextAllowedAutostart(build.CreatedAt, ws.AutostartSchedule.String, templateSchedule) |
| 616 | if err != nil { |
| 617 | return false |
| 618 | } |
| 619 | |
| 620 | // Must use '.Before' vs '.After' so equal times are considered "valid for autostart". |
| 621 | return !currentTick.Before(nextTransition) |
| 622 | } |
| 623 | |
| 624 | // isEligibleForAutostop returns true if the workspace should be autostopped. |
| 625 | func isEligibleForAutostop(user database.User, ws database.Workspace, build database.WorkspaceBuild, job database.ProvisionerJob, currentTick time.Time) bool { |