blob: bd675076106bbc73a2264c2d9c58250b71fc1a34 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import {store} from 'reducers/base.js';
6import * as sitewide from 'reducers/sitewide.js';
7
8// How long should we wait until asking the server status again.
9const SERVER_STATUS_DELAY_MS = 20 * 60 * 1000; // 20 minutes
10
11// CronTask is a class that supports periodically execution of tasks.
12export class CronTask {
13 constructor(task, delay) {
14 this.task = task;
15 this.delay = delay;
16 this.started = false;
17 }
18
19 start() {
20 if (this.started) return;
21 this.started = true;
22 this._execute();
23 }
24
25 _execute() {
26 this.task();
27 setTimeout(this._execute.bind(this), this.delay);
28 }
29}
30
31// getServerStatusCron requests status information from the server every 20
32// minutes.
33export const getServerStatusCron = new CronTask(
34 () => store.dispatch(sitewide.getServerStatus()),
35 SERVER_STATUS_DELAY_MS);