Unverified Commit 011be49a authored by Nicolas Giard's avatar Nicolas Giard Committed by GitHub

refactor: scheduler - simple tasks handling (#5703)

parent f4ce5ff6
const { DynamicThreadPool } = require('poolifier') const { DynamicThreadPool } = require('poolifier')
const os = require('node:os') const os = require('node:os')
const { setTimeout } = require('node:timers/promises') const { setTimeout } = require('node:timers/promises')
const autoload = require('auto-load')
const path = require('node:path')
module.exports = { module.exports = {
pool: null, pool: null,
maxWorkers: 1, maxWorkers: 1,
activeWorkers: 0, activeWorkers: 0,
tasks: null,
async init () { async init () {
this.maxWorkers = WIKI.config.scheduler.workers === 'auto' ? os.cpus().length : WIKI.config.scheduler.workers this.maxWorkers = WIKI.config.scheduler.workers === 'auto' ? os.cpus().length : WIKI.config.scheduler.workers
WIKI.logger.info(`Initializing Worker Pool (Limit: ${this.maxWorkers})...`) WIKI.logger.info(`Initializing Worker Pool (Limit: ${this.maxWorkers})...`)
...@@ -14,6 +17,7 @@ module.exports = { ...@@ -14,6 +17,7 @@ module.exports = {
exitHandler: () => WIKI.logger.debug('A worker has gone offline.'), exitHandler: () => WIKI.logger.debug('A worker has gone offline.'),
onlineHandler: () => WIKI.logger.debug('New worker is online.') onlineHandler: () => WIKI.logger.debug('New worker is online.')
}) })
this.tasks = autoload(path.join(WIKI.SERVERPATH, 'tasks/simple'))
return this return this
}, },
async start () { async start () {
...@@ -56,7 +60,7 @@ module.exports = { ...@@ -56,7 +60,7 @@ module.exports = {
data: job.payload data: job.payload
}) })
} else { } else {
await this.tasks[job.task](job.payload)
} }
} }
}) })
...@@ -66,7 +70,7 @@ module.exports = { ...@@ -66,7 +70,7 @@ module.exports = {
}, },
async stop () { async stop () {
WIKI.logger.info('Stopping Scheduler...') WIKI.logger.info('Stopping Scheduler...')
await this.pool.stop() await this.pool.destroy()
WIKI.logger.info('Scheduler: [ STOPPED ]') WIKI.logger.info('Scheduler: [ STOPPED ]')
} }
} }
...@@ -669,12 +669,12 @@ exports.up = async knex => { ...@@ -669,12 +669,12 @@ exports.up = async knex => {
await knex('jobSchedule').insert([ await knex('jobSchedule').insert([
{ {
task: 'update-locales', task: 'updateLocales',
cron: '0 0 * * *', cron: '0 0 * * *',
type: 'system' type: 'system'
}, },
{ {
task: 'check-version', task: 'checkVersion',
cron: '0 0 * * *', cron: '0 0 * * *',
type: 'system' type: 'system'
} }
......
module.exports = async (payload) => {
WIKI.logger.info('Checking for latest version...')
try {
// TODO: Fetch latest version
WIKI.logger.info('Checked for latest version: [ COMPLETED ]')
} catch (err) {
WIKI.logger.error('Checking for latest version: [ FAILED ]')
WIKI.logger.error(err.message)
}
}
module.exports = async (payload) => {
WIKI.logger.info('Fetching latest localization data...')
try {
// TODO: Fetch locale updates
WIKI.logger.info('Fetched latest localization data: [ COMPLETED ]')
} catch (err) {
WIKI.logger.error('Fetching latest localization data: [ FAILED ]')
WIKI.logger.error(err.message)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment