2017-11-15 13:51:18 +00:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-06-21 13:59:06 +00:00
|
|
|
import { Injectable, NgZone } from '@angular/core';
|
2017-11-15 13:51:18 +00:00
|
|
|
import { Network } from '@ionic-native/network';
|
|
|
|
import { CoreAppProvider } from './app';
|
|
|
|
import { CoreConfigProvider } from './config';
|
|
|
|
import { CoreLoggerProvider } from './logger';
|
|
|
|
import { CoreUtilsProvider } from './utils/utils';
|
2018-03-01 15:55:49 +00:00
|
|
|
import { CoreConstants } from '@core/constants';
|
|
|
|
import { SQLiteDB } from '@classes/sqlitedb';
|
2017-11-15 13:51:18 +00:00
|
|
|
|
2018-01-15 07:22:00 +00:00
|
|
|
/**
|
|
|
|
* Interface that all cron handlers must implement.
|
|
|
|
*/
|
2017-11-15 13:51:18 +00:00
|
|
|
export interface CoreCronHandler {
|
2018-01-15 07:22:00 +00:00
|
|
|
/**
|
|
|
|
* A name to identify the handler.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns handler's interval in milliseconds. Defaults to CoreCronDelegate.DEFAULT_INTERVAL.
|
|
|
|
*
|
|
|
|
* @return {number} Interval time (in milliseconds).
|
|
|
|
*/
|
|
|
|
getInterval?(): number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the process uses network or not. True if not defined.
|
|
|
|
*
|
|
|
|
* @return {boolean} Whether the process uses network or not
|
|
|
|
*/
|
|
|
|
usesNetwork?(): boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether it's a synchronization process or not. True if not defined.
|
|
|
|
*
|
|
|
|
* @return {boolean} Whether it's a synchronization process or not.
|
|
|
|
*/
|
|
|
|
isSync?(): boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the sync can be executed manually. Call isSync if not defined.
|
|
|
|
*
|
|
|
|
* @return {boolean} Whether the sync can be executed manually.
|
|
|
|
*/
|
|
|
|
canManualSync?(): boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the process.
|
|
|
|
*
|
|
|
|
* @param {string} [siteId] ID of the site affected. If not defined, all sites.
|
|
|
|
* @return {Promise<any>} Promise resolved when done. If the promise is rejected, this function will be called again often,
|
|
|
|
* it shouldn't be abused.
|
|
|
|
*/
|
|
|
|
execute?(siteId?: string): Promise<any>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the handler is running. Used internally by the provider, there's no need to set it.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2018-02-22 11:12:48 +00:00
|
|
|
running?: boolean;
|
2018-01-15 07:22:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Timeout ID for the handler scheduling. Used internally by the provider, there's no need to set it.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2018-02-22 11:12:48 +00:00
|
|
|
timeout?: number;
|
2018-01-29 09:05:20 +00:00
|
|
|
}
|
2017-11-15 13:51:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Service to handle cron processes. The registered processes will be executed every certain time.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class CoreCronDelegate {
|
|
|
|
// Constants.
|
2018-01-29 09:05:20 +00:00
|
|
|
static DEFAULT_INTERVAL = 3600000; // Default interval is 1 hour.
|
|
|
|
static MIN_INTERVAL = 300000; // Minimum interval is 5 minutes.
|
|
|
|
static DESKTOP_MIN_INTERVAL = 60000; // Minimum interval in desktop is 1 minute.
|
|
|
|
static MAX_TIME_PROCESS = 120000; // Max time a process can block the queue. Defaults to 2 minutes.
|
2017-11-15 13:51:18 +00:00
|
|
|
|
|
|
|
// Variables for database.
|
|
|
|
protected CRON_TABLE = 'cron';
|
|
|
|
protected tableSchema = {
|
|
|
|
name: this.CRON_TABLE,
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
name: 'id',
|
|
|
|
type: 'TEXT',
|
|
|
|
primaryKey: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'value',
|
|
|
|
type: 'INTEGER'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
protected logger;
|
|
|
|
protected appDB: SQLiteDB;
|
2018-01-29 09:05:20 +00:00
|
|
|
protected handlers: { [s: string]: CoreCronHandler } = {};
|
2017-11-15 13:51:18 +00:00
|
|
|
protected queuePromise = Promise.resolve();
|
|
|
|
|
|
|
|
constructor(logger: CoreLoggerProvider, private appProvider: CoreAppProvider, private configProvider: CoreConfigProvider,
|
2018-06-21 13:59:06 +00:00
|
|
|
private utils: CoreUtilsProvider, network: Network, zone: NgZone) {
|
2017-11-15 13:51:18 +00:00
|
|
|
this.logger = logger.getInstance('CoreCronDelegate');
|
|
|
|
|
|
|
|
this.appDB = this.appProvider.getDB();
|
|
|
|
this.appDB.createTableFromSchema(this.tableSchema);
|
|
|
|
|
|
|
|
// When the app is re-connected, start network handlers that were stopped.
|
|
|
|
network.onConnect().subscribe(() => {
|
2018-06-21 13:59:06 +00:00
|
|
|
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
|
|
|
zone.run(() => {
|
|
|
|
this.startNetworkHandlers();
|
|
|
|
});
|
2017-11-15 13:51:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to execute a handler. It will schedule the next execution once done.
|
|
|
|
* If the handler cannot be executed or it fails, it will be re-executed after mmCoreCronMinInterval.
|
|
|
|
*
|
|
|
|
* @param {string} name Name of the handler.
|
|
|
|
* @param {boolean} [force] Wether the execution is forced (manual sync).
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, all sites.
|
|
|
|
* @return {Promise<any>} Promise resolved if handler is executed successfully, rejected otherwise.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected checkAndExecuteHandler(name: string, force?: boolean, siteId?: string): Promise<any> {
|
|
|
|
if (!this.handlers[name] || !this.handlers[name].execute) {
|
2017-11-15 13:51:18 +00:00
|
|
|
// Invalid handler.
|
|
|
|
this.logger.debug('Cannot execute handler because is invalid: ' + name);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return Promise.reject(null);
|
|
|
|
}
|
|
|
|
|
2018-01-29 09:05:20 +00:00
|
|
|
const usesNetwork = this.handlerUsesNetwork(name),
|
|
|
|
isSync = !force && this.isHandlerSync(name);
|
|
|
|
let promise;
|
2017-11-15 13:51:18 +00:00
|
|
|
|
|
|
|
if (usesNetwork && !this.appProvider.isOnline()) {
|
|
|
|
// Offline, stop executing.
|
|
|
|
this.logger.debug('Cannot execute handler because device is offline: ' + name);
|
|
|
|
this.stopHandler(name);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return Promise.reject(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSync) {
|
|
|
|
// Check network connection.
|
2018-01-12 13:28:46 +00:00
|
|
|
promise = this.configProvider.get(CoreConstants.SETTINGS_SYNC_ONLY_ON_WIFI, false).then((syncOnlyOnWifi) => {
|
2018-10-03 14:11:09 +00:00
|
|
|
return !syncOnlyOnWifi || this.appProvider.isWifi();
|
2017-11-15 13:51:18 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
promise = Promise.resolve(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.then((execute: boolean) => {
|
|
|
|
if (!execute) {
|
|
|
|
// Cannot execute in this network connection, retry soon.
|
|
|
|
this.logger.debug('Cannot execute handler because device is using limited connection: ' + name);
|
|
|
|
this.scheduleNextExecution(name, CoreCronDelegate.MIN_INTERVAL);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return Promise.reject(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the execution to the queue.
|
|
|
|
this.queuePromise = this.queuePromise.catch(() => {
|
|
|
|
// Ignore errors in previous handlers.
|
|
|
|
}).then(() => {
|
|
|
|
return this.executeHandler(name, siteId).then(() => {
|
|
|
|
this.logger.debug(`Execution of handler '${name}' was a success.`);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return this.setHandlerLastExecutionTime(name, Date.now()).then(() => {
|
|
|
|
this.scheduleNextExecution(name);
|
|
|
|
});
|
2018-05-07 08:08:24 +00:00
|
|
|
}, (error) => {
|
2017-11-15 13:51:18 +00:00
|
|
|
// Handler call failed. Retry soon.
|
2018-05-07 08:08:24 +00:00
|
|
|
this.logger.error(`Execution of handler '${name}' failed.`, error);
|
2017-11-15 13:51:18 +00:00
|
|
|
this.scheduleNextExecution(name, CoreCronDelegate.MIN_INTERVAL);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return Promise.reject(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.queuePromise;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a handler, cancelling the execution if it takes more than MAX_TIME_PROCESS.
|
|
|
|
*
|
|
|
|
* @param {string} name Name of the handler.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, all sites.
|
|
|
|
* @return {Promise<any>} Promise resolved when the handler finishes or reaches max time, rejected if it fails.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected executeHandler(name: string, siteId?: string): Promise<any> {
|
|
|
|
return new Promise((resolve, reject): void => {
|
2017-11-15 13:51:18 +00:00
|
|
|
let cancelTimeout;
|
|
|
|
|
|
|
|
this.logger.debug('Executing handler: ' + name);
|
|
|
|
// Wrap the call in Promise.resolve to make sure it's a promise.
|
|
|
|
Promise.resolve(this.handlers[name].execute(siteId)).then(resolve).catch(reject).finally(() => {
|
|
|
|
clearTimeout(cancelTimeout);
|
|
|
|
});
|
|
|
|
|
|
|
|
cancelTimeout = setTimeout(() => {
|
|
|
|
// The handler took too long. Resolve because we don't want to retry soon.
|
|
|
|
this.logger.debug(`Resolving execution of handler '${name}' because it took too long.`);
|
|
|
|
resolve();
|
|
|
|
}, CoreCronDelegate.MAX_TIME_PROCESS);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force execution of synchronization cron tasks without waiting for the scheduled time.
|
|
|
|
* Please notice that some tasks may not be executed depending on the network connection and sync settings.
|
|
|
|
*
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, all sites.
|
|
|
|
* @return {Promise<any>} Promise resolved if all handlers are executed successfully, rejected otherwise.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
forceSyncExecution(siteId?: string): Promise<any> {
|
|
|
|
const promises = [];
|
2017-11-15 13:51:18 +00:00
|
|
|
|
2018-01-29 09:05:20 +00:00
|
|
|
for (const name in this.handlers) {
|
2017-11-15 13:51:18 +00:00
|
|
|
if (this.isHandlerManualSync(name)) {
|
|
|
|
// Now force the execution of the handler.
|
2018-11-16 09:55:32 +00:00
|
|
|
promises.push(this.forceCronHandlerExecution(name, siteId));
|
2017-11-15 13:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.utils.allPromises(promises);
|
|
|
|
}
|
|
|
|
|
2018-11-16 09:55:32 +00:00
|
|
|
/**
|
|
|
|
* Force execution of a cron tasks without waiting for the scheduled time.
|
|
|
|
* Please notice that some tasks may not be executed depending on the network connection and sync settings.
|
|
|
|
*
|
|
|
|
* @param {string} [name] If provided, the name of the handler.
|
|
|
|
* @param {string} [siteId] Site ID. If not defined, all sites.
|
|
|
|
* @return {Promise<any>} Promise resolved if handler has been executed successfully, rejected otherwise.
|
|
|
|
*/
|
|
|
|
forceCronHandlerExecution(name?: string, siteId?: string): Promise<any> {
|
|
|
|
const handler = this.handlers[name];
|
|
|
|
|
|
|
|
// Mark the handler as running (it might be running already).
|
|
|
|
handler.running = true;
|
|
|
|
|
|
|
|
// Cancel pending timeout.
|
|
|
|
clearTimeout(handler.timeout);
|
|
|
|
delete handler.timeout;
|
|
|
|
|
|
|
|
// Now force the execution of the handler.
|
|
|
|
return this.checkAndExecuteHandler(name, true, siteId);
|
|
|
|
}
|
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
/**
|
|
|
|
* Get a handler's interval.
|
|
|
|
*
|
|
|
|
* @param {string} name Handler's name.
|
|
|
|
* @return {number} Handler's interval.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected getHandlerInterval(name: string): number {
|
|
|
|
if (!this.handlers[name] || !this.handlers[name].getInterval) {
|
2017-11-15 13:51:18 +00:00
|
|
|
// Invalid, return default.
|
|
|
|
return CoreCronDelegate.DEFAULT_INTERVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow intervals lower than the minimum.
|
|
|
|
const minInterval = this.appProvider.isDesktop() ? CoreCronDelegate.DESKTOP_MIN_INTERVAL : CoreCronDelegate.MIN_INTERVAL,
|
|
|
|
handlerInterval = this.handlers[name].getInterval();
|
|
|
|
|
|
|
|
if (!handlerInterval) {
|
|
|
|
return CoreCronDelegate.DEFAULT_INTERVAL;
|
|
|
|
} else {
|
|
|
|
return Math.max(minInterval, handlerInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a handler's last execution ID.
|
|
|
|
*
|
|
|
|
* @param {string} name Handler's name.
|
|
|
|
* @return {string} Handler's last execution ID.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected getHandlerLastExecutionId(name: string): string {
|
2017-11-15 13:51:18 +00:00
|
|
|
return 'last_execution_' + name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a handler's last execution time. If not defined, return 0.
|
|
|
|
*
|
|
|
|
* @param {string} name Handler's name.
|
|
|
|
* @return {Promise<number>} Promise resolved with the handler's last execution time.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected getHandlerLastExecutionTime(name: string): Promise<number> {
|
2017-11-15 13:51:18 +00:00
|
|
|
const id = this.getHandlerLastExecutionId(name);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
|
|
|
return this.appDB.getRecord(this.CRON_TABLE, { id: id }).then((entry) => {
|
2017-11-15 13:51:18 +00:00
|
|
|
const time = parseInt(entry.value, 10);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return isNaN(time) ? 0 : time;
|
|
|
|
}).catch(() => {
|
|
|
|
return 0; // Not set, return 0.
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a handler uses network. Defaults to true.
|
|
|
|
*
|
|
|
|
* @param {string} name Handler's name.
|
|
|
|
* @return {boolean} True if handler uses network or not defined, false otherwise.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected handlerUsesNetwork(name: string): boolean {
|
|
|
|
if (!this.handlers[name] || !this.handlers[name].usesNetwork) {
|
2017-11-15 13:51:18 +00:00
|
|
|
// Invalid, return default.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.handlers[name].usesNetwork();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if there is any manual sync handler registered.
|
|
|
|
*
|
|
|
|
* @return {boolean} Whether it has at least 1 manual sync handler.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
hasManualSyncHandlers(): boolean {
|
|
|
|
for (const name in this.handlers) {
|
2017-11-15 13:51:18 +00:00
|
|
|
if (this.isHandlerManualSync(name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if there is any sync handler registered.
|
|
|
|
*
|
|
|
|
* @return {boolean} Whether it has at least 1 sync handler.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
hasSyncHandlers(): boolean {
|
|
|
|
for (const name in this.handlers) {
|
2017-11-15 13:51:18 +00:00
|
|
|
if (this.isHandlerSync(name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a handler can be manually synced. Defaults will use isSync instead.
|
|
|
|
*
|
|
|
|
* @param {string} name Handler's name.
|
|
|
|
* @return {boolean} True if handler is a sync process and can be manually executed or not defined, false otherwise.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected isHandlerManualSync(name: string): boolean {
|
|
|
|
if (!this.handlers[name] || !this.handlers[name].canManualSync) {
|
2017-11-15 13:51:18 +00:00
|
|
|
// Invalid, return default.
|
|
|
|
return this.isHandlerSync(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.handlers[name].canManualSync();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a handler is a sync process. Defaults to true.
|
|
|
|
*
|
|
|
|
* @param {string} name Handler's name.
|
|
|
|
* @return {boolean} True if handler is a sync process or not defined, false otherwise.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected isHandlerSync(name: string): boolean {
|
2017-11-15 13:51:18 +00:00
|
|
|
if (!this.handlers[name] || !this.handlers[name].isSync) {
|
|
|
|
// Invalid, return default.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.handlers[name].isSync();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a handler to be executed every certain time.
|
|
|
|
*
|
|
|
|
* @param {CoreCronHandler} handler The handler to register.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
register(handler: CoreCronHandler): void {
|
2017-11-15 13:51:18 +00:00
|
|
|
if (!handler || !handler.name) {
|
|
|
|
// Invalid handler.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof this.handlers[handler.name] != 'undefined') {
|
2018-02-22 11:12:48 +00:00
|
|
|
this.logger.debug(`The cron handler '${handler.name}' is already registered.`);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 11:12:48 +00:00
|
|
|
this.logger.debug(`Register handler '${handler.name}' in cron.`);
|
2017-11-15 13:51:18 +00:00
|
|
|
|
|
|
|
handler.running = false;
|
|
|
|
this.handlers[handler.name] = handler;
|
|
|
|
|
|
|
|
// Start the handler.
|
2018-02-22 11:12:48 +00:00
|
|
|
this.startHandler(handler.name);
|
2017-11-15 13:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedule a next execution for a handler.
|
|
|
|
*
|
|
|
|
* @param {string} name Name of the handler.
|
|
|
|
* @param {number} [time] Time to the next execution. If not supplied it will be calculated using the last execution and
|
|
|
|
* the handler's interval. This param should be used only if it's really necessary.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected scheduleNextExecution(name: string, time?: number): void {
|
2017-11-15 13:51:18 +00:00
|
|
|
if (!this.handlers[name]) {
|
|
|
|
// Invalid handler.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.handlers[name].timeout) {
|
|
|
|
// There's already a pending timeout.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let promise;
|
|
|
|
|
|
|
|
if (time) {
|
|
|
|
promise = Promise.resolve(time);
|
|
|
|
} else {
|
|
|
|
// Get last execution time to check when do we need to execute it.
|
|
|
|
promise = this.getHandlerLastExecutionTime(name).then((lastExecution) => {
|
|
|
|
const interval = this.getHandlerInterval(name),
|
|
|
|
nextExecution = lastExecution + interval;
|
|
|
|
|
|
|
|
return nextExecution - Date.now();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.then((nextExecution) => {
|
|
|
|
this.logger.debug(`Scheduling next execution of handler '${name}' in '${nextExecution}' ms`);
|
|
|
|
if (nextExecution < 0) {
|
|
|
|
nextExecution = 0; // Big negative numbers aren't executed immediately.
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handlers[name].timeout = setTimeout(() => {
|
|
|
|
delete this.handlers[name].timeout;
|
2018-05-07 08:08:24 +00:00
|
|
|
this.checkAndExecuteHandler(name).catch(() => {
|
|
|
|
// Ignore errors.
|
|
|
|
});
|
2017-11-15 13:51:18 +00:00
|
|
|
}, nextExecution);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a handler's last execution time.
|
|
|
|
*
|
|
|
|
* @param {string} name Handler's name.
|
|
|
|
* @param {number} time Time to set.
|
|
|
|
* @return {Promise} Promise resolved when the execution time is saved.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected setHandlerLastExecutionTime(name: string, time: number): Promise<any> {
|
2017-11-15 13:51:18 +00:00
|
|
|
const id = this.getHandlerLastExecutionId(name),
|
|
|
|
entry = {
|
|
|
|
id: id,
|
|
|
|
value: time
|
2018-01-29 09:05:20 +00:00
|
|
|
};
|
2017-11-15 13:51:18 +00:00
|
|
|
|
2018-03-20 14:44:26 +00:00
|
|
|
return this.appDB.insertRecord(this.CRON_TABLE, entry);
|
2017-11-15 13:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start running a handler periodically.
|
|
|
|
*
|
|
|
|
* @param {string} name Name of the handler.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected startHandler(name: string): void {
|
2017-11-15 13:51:18 +00:00
|
|
|
if (!this.handlers[name]) {
|
|
|
|
// Invalid handler.
|
|
|
|
this.logger.debug(`Cannot start handler '${name}', is invalid.`);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.handlers[name].running) {
|
|
|
|
this.logger.debug(`Handler '${name}', is already running.`);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handlers[name].running = true;
|
|
|
|
|
|
|
|
this.scheduleNextExecution(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start running periodically the handlers that use network.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
startNetworkHandlers(): void {
|
|
|
|
for (const name in this.handlers) {
|
2017-11-15 13:51:18 +00:00
|
|
|
if (this.handlerUsesNetwork(name)) {
|
|
|
|
this.startHandler(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop running a handler periodically.
|
|
|
|
*
|
|
|
|
* @param {string} name Name of the handler.
|
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
protected stopHandler(name: string): void {
|
2017-11-15 13:51:18 +00:00
|
|
|
if (!this.handlers[name]) {
|
|
|
|
// Invalid handler.
|
|
|
|
this.logger.debug(`Cannot stop handler '${name}', is invalid.`);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.handlers[name].running) {
|
|
|
|
this.logger.debug(`Cannot stop handler '${name}', it's not running.`);
|
2018-01-29 09:05:20 +00:00
|
|
|
|
2017-11-15 13:51:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handlers[name].running = false;
|
|
|
|
clearTimeout(this.handlers[name].timeout);
|
|
|
|
delete this.handlers[name].timeout;
|
|
|
|
}
|
|
|
|
}
|