Eggjs 集群环境下扩展定时任务类型 agent.js

// agent.js
'use strict'const _func = async agent => {
	class ClusterStrategy extends agent.TimerScheduleStrategy {
		async handler() {
			try {
				const scheduleName = this.key.replace(`${this.agent.baseDir}\\app\\schedule\\`, '');
				const result = await agent.redis.set(redis_key, scheduleName, 'EX', 50, 'NX');
				if (result === 'OK') {
					this.sendone();
				} else {
					this._scheduleNext();
				}
			} catch (err) {
				agent.loggers.exceptionLogger.info(`key: ${this.key}, err: ${err}`);
			}
		}
	}
	agent.schedule.use('schedule_name', ClusterStrategy);
}

class AppBootHook {
	constructor(app) {
		this.app = app;
	}
	async configWillLoad() {
		await _func(this.app);
	}
}

module.exports = AppBootHook;

你可能感兴趣的:(杂论,node.js)