Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async _fetchTask (file) {
const filePath = path.join(this.tasksPath, file)
let task
try {
task = require(filePath)
} catch (e) {
if (e instanceof ReferenceError) {
debug('Unable to import task class <%s>. Is it a valid javascript class?', file)
return
} else {
throw e
}
}
// Get instance of task class
const taskInstance = ioc.make(task)
// Every task must expose a schedule
if (!('schedule' in task)) {
throw CE.RuntimeException.undefinedTaskSchedule(file)
}
// Every task must expose a handle function
if (!('handle' in taskInstance)) {
throw CE.RuntimeException.undefinedTaskHandle(file)
}
if (!(taskInstance instanceof Task)) {
throw CE.RuntimeException.undefinedInstanceTask(file)
}
// Track currently registered tasks in memory
// Every job must expose a key
if (!Job.key) {
throw new Error(`No key found for job: ${link}`)
}
// If job concurrency is not set, default to 1
if (Job.concurrency === undefined) {
Job.concurrency = 1
}
// If job concurrecny is set to an invalid value, throw error
if (typeof Job.concurrency !== 'number') {
throw new Error(`Job concurrency value must be a number but instead it is: <${typeof Job.concurrency}>`)
}
const jobInstance = ioc.make(Job)
// Every job must expose a handle function
if (!jobInstance.handle) {
throw new Error(`No handler found for job: ${link}`)
}
// Track currently registered jobs in memory
this.registeredJobs.push(Job)
// Register job handler
this.instance.process(Job.key, Job.concurrency, (job, done) => {
jobInstance.handle(job.data)
.then(result => {
done(null, result)
})
.catch(error => {
async handle (args, { force, files, keepAlive }) {
try {
this._validateState(force)
const startTime = process.hrtime()
files = typeof (files) === 'string' ? files.split(',') : null
const allFiles = this._getSeedFiles(files)
if (!_.size(allFiles)) {
return this.viaAce ? this.info('Nothing to seed') : 'Nothing to seed'
}
for (const file of _.keys(allFiles)) {
const seedInstance = ioc.make(allFiles[file])
if (typeof (seedInstance.run) === 'function') {
await seedInstance.run()
} else {
this.warn(`${seedInstance.constructor.name} does not have a run method`)
}
}
const endTime = process.hrtime(startTime)
this.success(`Seeded database in ${prettyHrTime(endTime)}`)
} catch (error) {
console.log(error)
}
/**
* Close the connection when seeder are executed and keep alive is
* not passed
getSerializer (name) {
const serializer = Serializers[name] || this._serializers[name]
if (!serializer) {
throw GE.RuntimeException.incompleteConfig('auth', [`${name} serializer`], 'config/auth.js')
}
return ioc.make(serializer)
}
async _handleException (error, ctx) {
error.status = error.status || 500
try {
const handler = ioc.make(ioc.use(this._exceptionHandlerNamespace))
if (typeof (handler.handle) !== 'function' || typeof (handler.report) !== 'function') {
throw GE
.RuntimeException
.invoke(`${this._exceptionHandlerNamespace} class must have handle and report methods on it`)
}
handler.report(error, { request: ctx.request, auth: ctx.auth })
await handler.handle(error, ctx)
} catch (error) {
ctx.response.status(500).send(`${error.name}: ${error.message}\n${error.stack}`)
}
this._endResponse(ctx.response)
}
fs.readdirSync(path.join(Helpers.appRoot(), 'app', 'Jobs')).forEach(file => {
if (file === 'index.js' || file.substr(file.lastIndexOf('.') + 1) !== 'js') return
jobs.push(ioc.make(`App/Jobs/${file.replace('.js', '')}`))
})
getScheme (name) {
const scheme = Schemes[name] || this._schemes[name]
if (!scheme) {
throw GE.RuntimeException.incompleteConfig('auth', [`${name} scheme`], 'config/auth.js')
}
return ioc.make(scheme)
}
}
name = name.toLowerCase()
const Driver = Drivers[name] || this._drivers[name]
if (config && config.format && typeof (config.format) === 'string') {
config.format = null
}
/**
* If driver doesn't exists, let the end user know
* about it
*/
if (!Driver) {
throw GE.RuntimeException.invoke(`Logger driver ${name} does not exists.`, 500, 'E_INVALID_LOGGER_DRIVER')
}
const driverInstance = ioc.make(Driver)
driverInstance.setConfig(config)
return driverInstance
}
}
driver (name, config, viewInstance) {
if (!name) {
throw GE.InvalidArgumentException.invalidParameter('Cannot get driver instance without a name')
}
name = name.toLowerCase()
const Driver = Drivers[name] || this._drivers[name]
if (!Driver) {
throw GE.InvalidArgumentException.invalidParameter(`${name} is not a valid mail driver`)
}
const driverInstance = ioc.make(Driver)
driverInstance.setConfig(config)
return new MailSender(driverInstance, viewInstance)
}
}