Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(Task): Load Tasks In Sub-directories #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 26 additions & 2 deletions src/Scheduler/index.js
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ class Scheduler {
let taskFiles

try {
taskFiles = fs.readdirSync(this.tasksPath)
taskFiles = this._getFiles(this.tasksPath)
} catch (e) {
// If the directory isn't found, log a message and exit gracefully
if (e.code === 'ENOENT') {
@@ -112,13 +112,37 @@ class Scheduler {
}

taskFiles = taskFiles.filter(file => path.extname(file) === '.js')

for (let taskFile of taskFiles) {
await this._fetchTask(taskFile)
}

debug('scheduler running %d tasks', this.registeredTasks.length)
}

/**
* Get all files in the `Task` directory.
*
* @param {String} directory
* @param {String} relativeDir
* @private
*/
_getFiles(directory, relativeDir = "") {
let result = [];

for (const file of fs.readdirSync(directory)) {
const fullPath = path.join(directory, file);
const relativePath = path.join(relativeDir, file)

if (fs.statSync(fullPath).isDirectory()) {
result.push(...this._getFiles(fullPath, relativePath));
continue;
}

result.push(relativePath);
}

return result;
}
}

module.exports = Scheduler
14 changes: 14 additions & 0 deletions test/projects/good/app/Tasks/a/Good.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const Task = use('Task')

class Good extends Task {
static get schedule () {
return '*/1 * * * * *'
}

async handle () {
}
}

module.exports = Good
14 changes: 14 additions & 0 deletions test/projects/good/app/Tasks/a/b/Working.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const Task = use('Task')

class Working extends Task {
static get schedule () {
return '*/1 * * * * *'
}

async handle () {
}
}

module.exports = Working
2 changes: 1 addition & 1 deletion test/scheduler.spec.js
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ test.group('Scheduler', (group) => {
const scheduler = new Scheduler(Helpers)
await scheduler.run()
assert.isArray(scheduler.registeredTasks)
assert.equal(scheduler.registeredTasks.length, 1)
assert.equal(scheduler.registeredTasks.length, 3)
})

test('Should ignore invalid task file types', async (assert) => {