const LoggerKit = require('../kit/logger-kit') const log4js = require('log4js') const TimeKit = require('../kit/time-kit') class Task { context delayTime = 5 * 1000 name logger fileLogger running = true constructor(taskName, context, initFun, onTickFun) { this.name = taskName this.init = initFun this.onTick = onTickFun this.context = context this.consoleLoggerInit() this.fileLoggerInit(this.name.concat('Task.log')) } setDelayTime(delayTime) { this.delayTime = delayTime } 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...`) while (task.running) { try { await task.onTick() } catch (e) { task.fileLogger.error(e) } await TimeKit.sleep(task.delayTime) } } async Stop() { this.running = false } } module.exports = Task