Skip to content

Commit

Permalink
default automatic updates to be every 4 hours (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalsaleh committed May 28, 2020
1 parent d0b4625 commit 7f96012
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion kotsadm/migrations/tables/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ spec:
type: text
- name: update_checker_spec
type: text
default: '@daily'
default: '@default'
2 changes: 1 addition & 1 deletion kotsadm/pkg/handlers/update_checker_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func UpdateCheckerSpec(w http.ResponseWriter, r *http.Request) {

// validate cron spec
cronSpec := updateCheckerSpecRequest.UpdateCheckerSpec
if cronSpec != "@never" {
if cronSpec != "@never" && cronSpec != "@default" {
_, err := cron.ParseStandard(cronSpec)
if err != nil {
logger.Error(err)
Expand Down
15 changes: 13 additions & 2 deletions kotsadm/pkg/updatechecker/updatechecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"sync"
"time"

"github.com/pkg/errors"
"github.com/replicatedhq/kots/kotsadm/pkg/app"
Expand Down Expand Up @@ -66,11 +67,21 @@ func Configure(appID string) error {
mtx.Lock()
defer mtx.Unlock()

if a.UpdateCheckerSpec == "@never" || a.UpdateCheckerSpec == "" {
cronSpec := a.UpdateCheckerSpec

if cronSpec == "@never" || cronSpec == "" {
Stop(a.ID)
return nil
}

if cronSpec == "@default" {
// check for updates every 4 hours
t := time.Now()
m := t.Minute()
h := t.Hour() % 4
cronSpec = fmt.Sprintf("%d %d/4 * * *", m, h)
}

job, ok := jobs[a.ID]
if ok {
// job already exists, remove entries
Expand All @@ -85,7 +96,7 @@ func Configure(appID string) error {
))
}

_, err = job.AddFunc(a.UpdateCheckerSpec, func() {
_, err = job.AddFunc(cronSpec, func() {
logger.Debug("checking updates for app", zap.String("slug", a.Slug))

availableUpdates, err := CheckForUpdates(a)
Expand Down
23 changes: 19 additions & 4 deletions kotsadm/web/src/components/modals/UpdateCheckerModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ const SCHEDULES = [
value: "@weekly",
label: "Weekly",
},
{
value: "@default",
label: "Default",
},
{
value: "custom",
label: "Custom",
}
},
];

export default class UpdateCheckerModal extends React.Component {
Expand Down Expand Up @@ -97,7 +101,7 @@ export default class UpdateCheckerModal extends React.Component {

handleScheduleChange = selectedSchedule => {
let updateCheckerSpec;
if (selectedSchedule.value != "custom") {
if (selectedSchedule.value !== "custom") {
updateCheckerSpec = selectedSchedule.value;
} else {
updateCheckerSpec = "0 2 * * WED,SAT"; // arbitrary choice
Expand Down Expand Up @@ -160,9 +164,20 @@ export default class UpdateCheckerModal extends React.Component {
className="Input u-marginBottom--5"
placeholder="0 0 * * MON"
value={updateCheckerSpec}
onChange={(e) => this.setState({ updateCheckerSpec: e.target.value })}
onChange={(e) => {
const schedule = find(SCHEDULES, { value: e.target.value });
const selectedSchedule = schedule ? schedule : find(SCHEDULES, { value: "custom" });
this.setState({ updateCheckerSpec: e.target.value, selectedSchedule });
}}
/>
{humanReadableCron && <span className="u-fontSize--small u-fontWeight--medium u-color--dustyGray">{humanReadableCron}</span>}
{selectedSchedule.value === "@default" ?
<span className="u-fontSize--small u-fontWeight--medium u-color--dustyGray">Every 4 hours</span>
:
humanReadableCron ?
<span className="u-fontSize--small u-fontWeight--medium u-color--dustyGray">{humanReadableCron}</span>
:
null
}
</div>
</div>
{submitUpdateCheckerSpecErr && <span className="u-color--chestnut u-fontSize--small u-fontWeight--bold u-marginTop--15">Error: {submitUpdateCheckerSpecErr}</span>}
Expand Down

0 comments on commit 7f96012

Please sign in to comment.