const LoggerKit = require('../kit/logger-kit') const log4js = require('log4js') class Task { context delayTime name logger fileLogger constructor(taskName, delayTime, initFun, onTickFun) { this.name = taskName this.delayTime = delayTime this.init = initFun this.onTick = onTickFun this.consoleLoggerInit() this.fileLoggerInit(this.name.concat('Task.log')) } consoleLoggerInit() { this.logger = LoggerKit.getLogger(this.name) } fileLoggerInit(logFilePath) { const logFileCategoryName = this.name.concat('FileLog') const appender = { type: 'file', filename: logFilePath } const category = { appenders: [logFileCategoryName], level: log4js.levels.TRACE } LoggerKit.pushAppender(logFileCategoryName, appender) this.fileLogger = LoggerKit.pushCategory(logFileCategoryName, category) this.fileLogger.info('File logger has init.') } async Start() { const task = this this.logger.info(`Init context or others.`) await this.init() this.logger.info(`Dida dida dida, on tick, on tick...`) this.interval = setInterval(async () => { try { await task.onTick() } catch (e) { task.logger.error(e) } }, this.delayTime) } async Stop() { clearInterval(this.interval) } } module.exports = Task