X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fschedulers%2Fabstract-scheduler.ts;h=f3d51a22e09f9df869dee6990a18e4f65f4981cd;hb=4d526901349b35bfac9811acc45854c65639cee3;hp=473544ddfff1d78ba23ed5861f4597c9be779b11;hpb=60650c77c8a2a98e92d869b237ae4900f369a8fc;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/schedulers/abstract-scheduler.ts b/server/lib/schedulers/abstract-scheduler.ts index 473544ddf..f3d51a22e 100644 --- a/server/lib/schedulers/abstract-scheduler.ts +++ b/server/lib/schedulers/abstract-scheduler.ts @@ -1,16 +1,35 @@ -import { SCHEDULER_INTERVAL } from '../../initializers' +import Bluebird from 'bluebird' +import { logger } from '../../helpers/logger' export abstract class AbstractScheduler { + protected abstract schedulerIntervalMs: number + private interval: NodeJS.Timer + private isRunning = false enable () { - this.interval = setInterval(() => this.execute(), SCHEDULER_INTERVAL) + if (!this.schedulerIntervalMs) throw new Error('Interval is not correctly set.') + + this.interval = setInterval(() => this.execute(), this.schedulerIntervalMs) } disable () { clearInterval(this.interval) } - protected abstract execute () + async execute () { + if (this.isRunning === true) return + this.isRunning = true + + try { + await this.internalExecute() + } catch (err) { + logger.error('Cannot execute %s scheduler.', this.constructor.name, { err }) + } finally { + this.isRunning = false + } + } + + protected abstract internalExecute (): Promise | Bluebird }