Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import {store} from 'reducers/base.js'; |
| 6 | import * as sitewide from 'reducers/sitewide.js'; |
| 7 | |
| 8 | // How long should we wait until asking the server status again. |
| 9 | const SERVER_STATUS_DELAY_MS = 20 * 60 * 1000; // 20 minutes |
| 10 | |
| 11 | // CronTask is a class that supports periodically execution of tasks. |
| 12 | export 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. |
| 33 | export const getServerStatusCron = new CronTask( |
| 34 | () => store.dispatch(sitewide.getServerStatus()), |
| 35 | SERVER_STATUS_DELAY_MS); |