Unverified Commit f4ce5ff6 authored by Nicolas Giard's avatar Nicolas Giard Committed by GitHub

refactor: remove bluebird + native scheduler (#5702)

parent 10cc2ef4
...@@ -52,7 +52,6 @@ ...@@ -52,7 +52,6 @@
"auto-load": "3.0.4", "auto-load": "3.0.4",
"aws-sdk": "2.1208.0", "aws-sdk": "2.1208.0",
"bcryptjs-then": "1.0.1", "bcryptjs-then": "1.0.1",
"bluebird": "3.7.2",
"body-parser": "1.20.0", "body-parser": "1.20.0",
"chalk": "4.1.2", "chalk": "4.1.2",
"cheerio": "1.0.0-rc.12", "cheerio": "1.0.0-rc.12",
...@@ -81,7 +80,6 @@ ...@@ -81,7 +80,6 @@
"filesize": "6.1.0", "filesize": "6.1.0",
"fs-extra": "9.0.1", "fs-extra": "9.0.1",
"getos": "3.2.1", "getos": "3.2.1",
"graphile-worker": "0.13.0",
"graphql": "16.3.0", "graphql": "16.3.0",
"graphql-list-fields": "2.0.2", "graphql-list-fields": "2.0.2",
"graphql-rate-limit-directive": "2.0.2", "graphql-rate-limit-directive": "2.0.2",
......
...@@ -78,25 +78,6 @@ defaults: ...@@ -78,25 +78,6 @@ defaults:
search: search:
maxHits: 100 maxHits: 100
maintainerEmail: security@requarks.io maintainerEmail: security@requarks.io
jobs:
- task: background
identifier: purge-uploads
pattern: '*/15 * * * *'
payload:
name: purgeUploads
data: {}
# - task: simple
# identifier: letest
# pattern: '* * * * *'
# payload:
# name: bob
# data: {}
# - task: simple
# identifier: letest2
# pattern: '* * * * *'
# payload:
# name: bob
# data: {}
groups: groups:
defaultPermissions: defaultPermissions:
- 'read:pages' - 'read:pages'
......
...@@ -4,8 +4,9 @@ const _ = require('lodash') ...@@ -4,8 +4,9 @@ const _ = require('lodash')
const jwt = require('jsonwebtoken') const jwt = require('jsonwebtoken')
const ms = require('ms') const ms = require('ms')
const { DateTime } = require('luxon') const { DateTime } = require('luxon')
const Promise = require('bluebird') const util = require('node:util')
const crypto = Promise.promisifyAll(require('crypto')) const crypto = require('node:crypto')
const randomBytes = util.promisify(crypto.randomBytes)
const pem2jwk = require('pem-jwk').pem2jwk const pem2jwk = require('pem-jwk').pem2jwk
const securityHelper = require('../helpers/security') const securityHelper = require('../helpers/security')
...@@ -366,7 +367,7 @@ module.exports = { ...@@ -366,7 +367,7 @@ module.exports = {
async regenerateCertificates () { async regenerateCertificates () {
WIKI.logger.info('Regenerating certificates...') WIKI.logger.info('Regenerating certificates...')
_.set(WIKI.config, 'sessionSecret', (await crypto.randomBytesAsync(32)).toString('hex')) _.set(WIKI.config, 'sessionSecret', (await randomBytes(32)).toString('hex'))
const certs = crypto.generateKeyPairSync('rsa', { const certs = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048, modulusLength: 2048,
publicKeyEncoding: { publicKeyEncoding: {
......
const _ = require('lodash') const _ = require('lodash')
const autoload = require('auto-load') const autoload = require('auto-load')
const path = require('path') const path = require('path')
const Promise = require('bluebird')
const Knex = require('knex') const Knex = require('knex')
const fs = require('fs') const fs = require('fs')
const Objection = require('objection') const Objection = require('objection')
const migrationSource = require('../db/migrator-source') const migrationSource = require('../db/migrator-source')
const migrateFromLegacy = require('../db/legacy') const migrateFromLegacy = require('../db/legacy')
const { setTimeout } = require('timers/promises')
/** /**
* ORM DB module * ORM DB module
...@@ -118,7 +118,7 @@ module.exports = { ...@@ -118,7 +118,7 @@ module.exports = {
WIKI.logger.error(`Database Connection Error: ${err.message}`) WIKI.logger.error(`Database Connection Error: ${err.message}`)
} }
WIKI.logger.warn(`Will retry in 3 seconds... [Attempt ${++conAttempts} of 10]`) WIKI.logger.warn(`Will retry in 3 seconds... [Attempt ${++conAttempts} of 10]`)
await new Promise(resolve => setTimeout(resolve, 3000)) await setTimeout(3000)
await initTasks.connect() await initTasks.connect()
} else { } else {
throw err throw err
......
...@@ -74,9 +74,9 @@ module.exports = { ...@@ -74,9 +74,9 @@ module.exports = {
await WIKI.db.commentProviders.initProvider() await WIKI.db.commentProviders.initProvider()
await WIKI.db.sites.reloadCache() await WIKI.db.sites.reloadCache()
await WIKI.db.storage.initTargets() await WIKI.db.storage.initTargets()
await WIKI.scheduler.start()
await WIKI.db.subscribeToNotifications() await WIKI.db.subscribeToNotifications()
await WIKI.scheduler.start()
}, },
/** /**
* Graceful shutdown * Graceful shutdown
......
...@@ -33,6 +33,9 @@ LEVELS.forEach(lvl => { ...@@ -33,6 +33,9 @@ LEVELS.forEach(lvl => {
message: msg message: msg
}) })
} else { } else {
if (msg instanceof Error) {
msg = msg.stack
}
formatted = chalk`${new Date().toISOString()} {dim [${WIKI.INSTANCE_ID}]} {${LEVELCOLORS[lvl]}.bold ${lvl}}: ${msg}` formatted = chalk`${new Date().toISOString()} {dim [${WIKI.INSTANCE_ID}]} {${LEVELCOLORS[lvl]}.bold ${lvl}}: ${msg}`
} }
......
const { DynamicThreadPool } = require('poolifier') const { DynamicThreadPool } = require('poolifier')
const { v4: uuid } = require('uuid')
const os = require('node:os') const os = require('node:os')
const path = require('node:path') const { setTimeout } = require('node:timers/promises')
module.exports = { module.exports = {
pool: null, pool: null,
runner: null,
maxWorkers: 1, maxWorkers: 1,
activeWorkers: 0,
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})...`)
...@@ -19,46 +18,55 @@ module.exports = { ...@@ -19,46 +18,55 @@ module.exports = {
}, },
async start () { async start () {
WIKI.logger.info('Starting Scheduler...') WIKI.logger.info('Starting Scheduler...')
// this.runner = await run({ WIKI.db.listener.addChannel('scheduler', payload => {
// pgPool: new Pool({ switch (payload.event) {
// ...(typeof WIKI.db.config === 'string') ? { case 'newJob': {
// connectionString: WIKI.db.config if (this.activeWorkers < this.maxWorkers) {
// } : WIKI.db.config, this.activeWorkers++
// max: this.maxWorkers + 2 this.processJob()
// }), }
// schema: WIKI.config.db.schemas.scheduler, break
// concurrency: this.maxWorkers, }
// noHandleSignals: true, }
// logger: new Logger(scope => { })
// return (level, message, meta) => { // await WIKI.db.knex('jobs').insert({
// const prefix = (scope?.workerId) ? `[${scope.workerId}] ` : '' // task: 'test',
// WIKI.logger[level](`${prefix}${message}`, meta) // payload: { foo: 'bar' }
// }
// }),
// parsedCronItems: parseCronItems(WIKI.data.jobs),
// taskList: {
// simple: async (payload, helpers) => {
// // TODO: Handle task
// },
// background: async (payload, helpers) => {
// try {
// await this.pool.execute({
// id: helpers.job.id,
// name: payload.name,
// data: payload.data
// }) // })
// } catch (err) { // WIKI.db.listener.publish('scheduler', {
// helpers.logger.warn(`Failed job: ${err.message}`) // source: WIKI.INSTANCE_ID,
// throw err // event: 'newJob'
// }
// }
// }
// }) // })
WIKI.logger.info('Scheduler: [ STARTED ]') WIKI.logger.info('Scheduler: [ STARTED ]')
}, },
async processJob () {
try {
await WIKI.db.knex.transaction(async trx => {
const jobs = await trx('jobs')
.where('id', WIKI.db.knex.raw('(SELECT id FROM jobs ORDER BY id FOR UPDATE SKIP LOCKED LIMIT 1)'))
.returning('*')
.del()
if (jobs && jobs.length === 1) {
const job = jobs[0]
WIKI.logger.info(`Processing new job ${job.id}: ${job.task}...`)
if (job.useWorker) {
await this.pool.execute({
id: job.id,
name: job.task,
data: job.payload
})
} else {
}
}
})
} catch (err) {
WIKI.logger.warn(err)
}
},
async stop () { async stop () {
WIKI.logger.info('Stopping Scheduler...') WIKI.logger.info('Stopping Scheduler...')
// await this.runner.stop() await this.pool.stop()
WIKI.logger.info('Scheduler: [ STOPPED ]') WIKI.logger.info('Scheduler: [ STOPPED ]')
} }
} }
...@@ -2,7 +2,6 @@ const fs = require('fs-extra') ...@@ -2,7 +2,6 @@ const fs = require('fs-extra')
const http = require('http') const http = require('http')
const https = require('https') const https = require('https')
const { ApolloServer } = require('apollo-server-express') const { ApolloServer } = require('apollo-server-express')
const Promise = require('bluebird')
const _ = require('lodash') const _ = require('lodash')
const io = require('socket.io') const io = require('socket.io')
const { ApolloServerPluginLandingPageGraphQLPlayground, ApolloServerPluginLandingPageProductionDefault } = require('apollo-server-core') const { ApolloServerPluginLandingPageGraphQLPlayground, ApolloServerPluginLandingPageProductionDefault } = require('apollo-server-core')
...@@ -188,11 +187,11 @@ module.exports = { ...@@ -188,11 +187,11 @@ module.exports = {
async stopServers () { async stopServers () {
this.closeConnections() this.closeConnections()
if (this.http) { if (this.http) {
await Promise.fromCallback(cb => { this.http.close(cb) }) await new Promise(resolve => this.http.close(resolve))
this.http = null this.http = null
} }
if (this.https) { if (this.https) {
await Promise.fromCallback(cb => { this.https.close(cb) }) await new Promise(resolve => this.https.close(resolve))
this.https = null this.https = null
} }
this.graph = null this.graph = null
...@@ -205,7 +204,7 @@ module.exports = { ...@@ -205,7 +204,7 @@ module.exports = {
switch (srv) { switch (srv) {
case 'http': case 'http':
if (this.http) { if (this.http) {
await Promise.fromCallback(cb => { this.http.close(cb) }) await new Promise(resolve => this.http.close(resolve))
this.http = null this.http = null
} }
this.initHTTP() this.initHTTP()
...@@ -213,7 +212,7 @@ module.exports = { ...@@ -213,7 +212,7 @@ module.exports = {
break break
case 'https': case 'https':
if (this.https) { if (this.https) {
await Promise.fromCallback(cb => { this.https.close(cb) }) await new Promise(resolve => this.https.close(resolve))
this.https = null this.https = null
} }
this.initHTTPS() this.initHTTPS()
......
...@@ -145,7 +145,9 @@ exports.up = async knex => { ...@@ -145,7 +145,9 @@ exports.up = async knex => {
.createTable('jobs', table => { .createTable('jobs', table => {
table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()')) table.uuid('id').notNullable().primary().defaultTo(knex.raw('gen_random_uuid()'))
table.string('task').notNullable() table.string('task').notNullable()
table.boolean('useWorker').notNullable().defaultTo(false)
table.jsonb('payload') table.jsonb('payload')
table.timestamp('waitUntil')
table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now()) table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now())
table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now()) table.timestamp('updatedAt').notNullable().defaultTo(knex.fn.now())
}) })
......
const _ = require('lodash') const _ = require('lodash')
const Promise = require('bluebird') const util = require('node:util')
const getos = Promise.promisify(require('getos')) const getos = util.promisify(require('getos'))
const os = require('os') const os = require('node:os')
const filesize = require('filesize') const filesize = require('filesize')
const path = require('path') const path = require('path')
const fs = require('fs-extra') const fs = require('fs-extra')
...@@ -105,12 +105,12 @@ module.exports = { ...@@ -105,12 +105,12 @@ module.exports = {
currentVersion () { currentVersion () {
return WIKI.version return WIKI.version
}, },
async dbVersion () {
return _.get(WIKI.models, 'knex.client.version', 'Unknown Version')
},
dbHost () { dbHost () {
return WIKI.config.db.host return WIKI.config.db.host
}, },
dbVersion () {
return _.get(WIKI.db, 'knex.client.version', 'Unknown Version')
},
hostname () { hostname () {
return os.hostname() return os.hostname()
}, },
......
const Promise = require('bluebird') const util = require('node:util')
const crypto = require('crypto') const crypto = require('node:crypto')
const randomBytes = util.promisify(crypto.randomBytes)
const passportJWT = require('passport-jwt') const passportJWT = require('passport-jwt')
module.exports = { module.exports = {
...@@ -17,11 +18,7 @@ module.exports = { ...@@ -17,11 +18,7 @@ module.exports = {
* @returns * @returns
*/ */
async generateToken (length) { async generateToken (length) {
return Promise.fromCallback(clb => { return (await randomBytes(length)).toString('hex')
crypto.randomBytes(length, clb)
}).then(buf => {
return buf.toString('hex')
})
}, },
extractJWT: passportJWT.ExtractJwt.fromExtractors([ extractJWT: passportJWT.ExtractJwt.fromExtractors([
......
/* global WIKI */
const Model = require('objection').Model const Model = require('objection').Model
const moment = require('moment') const moment = require('moment')
const path = require('path') const path = require('path')
const fs = require('fs-extra') const fs = require('fs-extra')
const _ = require('lodash') const _ = require('lodash')
const assetHelper = require('../helpers/asset') const assetHelper = require('../helpers/asset')
const Promise = require('bluebird')
/** /**
* Users model * Users model
...@@ -199,9 +196,8 @@ module.exports = class Asset extends Model { ...@@ -199,9 +196,8 @@ module.exports = class Asset extends Model {
} catch (err) { } catch (err) {
return false return false
} }
const sendFile = Promise.promisify(res.sendFile, {context: res})
res.type(path.extname(assetPath)) res.type(path.extname(assetPath))
await sendFile(cachePath, { dotfiles: 'deny' }) await new Promise(resolve => res.sendFile(cachePath, { dotfiles: 'deny' }, resolve))
return true return true
} }
......
const { BlobServiceClient, StorageSharedKeyCredential } = require('@azure/storage-blob') const { BlobServiceClient, StorageSharedKeyCredential } = require('@azure/storage-blob')
const stream = require('stream') const stream = require('stream')
const Promise = require('bluebird') const util = require('node:util')
const pipeline = Promise.promisify(stream.pipeline) const pipeline = util.promisify(stream.pipeline)
const pageHelper = require('../../../helpers/page.js') const pageHelper = require('../../../helpers/page.js')
const _ = require('lodash') const _ = require('lodash')
......
const fs = require('fs-extra') const fs = require('fs-extra')
const path = require('path') const path = require('node:path')
const stream = require('stream') const stream = require('node:stream')
const Promise = require('bluebird') const util = require('node:util')
const pipeline = Promise.promisify(stream.pipeline) const pipeline = util.promisify(stream.pipeline)
const klaw = require('klaw') const klaw = require('klaw')
const mime = require('mime-types').lookup const mime = require('mime-types').lookup
const _ = require('lodash') const _ = require('lodash')
......
const fs = require('fs-extra') const fs = require('fs-extra')
const path = require('path') const path = require('node:path')
const tar = require('tar-fs') const tar = require('tar-fs')
const zlib = require('zlib') const zlib = require('node:zlib')
const stream = require('stream') const stream = require('node:stream')
const _ = require('lodash') const _ = require('lodash')
const Promise = require('bluebird') const util = require('node:util')
const pipeline = Promise.promisify(stream.pipeline) const pipeline = util.promisify(stream.pipeline)
const moment = require('moment') const moment = require('moment')
const pageHelper = require('../../../helpers/page') const pageHelper = require('../../../helpers/page')
......
const { BlobServiceClient, StorageSharedKeyCredential } = require('@azure/storage-blob') const { BlobServiceClient, StorageSharedKeyCredential } = require('@azure/storage-blob')
const stream = require('stream') const stream = require('node:stream')
const Promise = require('bluebird') const util = require('node:util')
const pipeline = Promise.promisify(stream.pipeline) const pipeline = util.promisify(stream.pipeline)
const pageHelper = require('../../../helpers/page.js') const pageHelper = require('../../../helpers/page.js')
const _ = require('lodash') const _ = require('lodash')
......
const path = require('path') const path = require('node:path')
const sgit = require('simple-git/promise') const sgit = require('simple-git/promise')
const fs = require('fs-extra') const fs = require('fs-extra')
const _ = require('lodash') const _ = require('lodash')
const stream = require('stream') const stream = require('node:stream')
const Promise = require('bluebird') const util = require('node:util')
const pipeline = Promise.promisify(stream.pipeline) const pipeline = util.promisify(stream.pipeline)
const klaw = require('klaw') const klaw = require('klaw')
const os = require('os') const os = require('node:os')
const pageHelper = require('../../../helpers/page') const pageHelper = require('../../../helpers/page')
const assetHelper = require('../../../helpers/asset') const assetHelper = require('../../../helpers/asset')
......
const S3 = require('aws-sdk/clients/s3') const S3 = require('aws-sdk/clients/s3')
const stream = require('stream') const stream = require('node:stream')
const Promise = require('bluebird') const util = require('node:util')
const pipeline = Promise.promisify(stream.pipeline) const pipeline = util.promisify(stream.pipeline)
const _ = require('lodash') const _ = require('lodash')
const pageHelper = require('../../../helpers/page.js') const pageHelper = require('../../../helpers/page.js')
......
const SSH2Promise = require('ssh2-promise') const SSH2Promise = require('ssh2-promise')
const _ = require('lodash') const _ = require('lodash')
const path = require('path') const path = require('node:path')
const stream = require('stream') const stream = require('node:stream')
const Promise = require('bluebird') const util = require('node:util')
const pipeline = Promise.promisify(stream.pipeline) const pipeline = util.promisify(stream.pipeline)
const pageHelper = require('../../../helpers/page.js') const pageHelper = require('../../../helpers/page.js')
const getFilePath = (page, pathKey) => { const getFilePath = (page, pathKey) => {
......
This diff was suppressed by a .gitattributes entry.
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