From 13012d0e23fac837e8e891c9ed982238ee1c593d Mon Sep 17 00:00:00 2001 From: Tochukwu Nkemdilim Date: Mon, 24 Aug 2020 14:20:54 +0100 Subject: [PATCH 1/2] Fix(Task): Load Tasks In Sub-directories --- package-lock.json | 6 ++++-- src/Scheduler/index.js | 21 +++++++++++++++++++-- test/projects/good/app/Tasks/a/Good.js | 14 ++++++++++++++ test/projects/good/app/Tasks/a/b/Working.js | 14 ++++++++++++++ test/scheduler.spec.js | 2 +- 5 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 test/projects/good/app/Tasks/a/Good.js create mode 100644 test/projects/good/app/Tasks/a/b/Working.js diff --git a/package-lock.json b/package-lock.json index a3f03b0..b9f292f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "adonis-scheduler", - "version": "3.0.1", + "version": "3.0.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2047,6 +2047,7 @@ "version": "0.1.4", "bundled": true, "dev": true, + "optional": true, "requires": { "kind-of": "^3.0.2", "longest": "^1.0.1", @@ -3229,7 +3230,8 @@ "longest": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "loose-envify": { "version": "1.3.1", diff --git a/src/Scheduler/index.js b/src/Scheduler/index.js index c86c0ec..97d12a9 100644 --- a/src/Scheduler/index.js +++ b/src/Scheduler/index.js @@ -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,30 @@ 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) } + + _getFiles(directory, relativePath = "") { + let result = []; + + for (const file of fs.readdirSync(directory)) { + const fullPath = path.join(directory, file); + const relativeFile = path.join(relativePath, file) + + if (fs.statSync(fullPath).isDirectory()) { + result.push(...this._getFiles(fullPath, relativeFile)); + continue; + } + + result.push(relativeFile); + } + + return result; + } } module.exports = Scheduler diff --git a/test/projects/good/app/Tasks/a/Good.js b/test/projects/good/app/Tasks/a/Good.js new file mode 100644 index 0000000..f079d32 --- /dev/null +++ b/test/projects/good/app/Tasks/a/Good.js @@ -0,0 +1,14 @@ +'use strict' + +const Task = use('Task') + +class Good extends Task { + static get schedule () { + return '*/1 * * * * *' + } + + async handle () { + } +} + +module.exports = Good diff --git a/test/projects/good/app/Tasks/a/b/Working.js b/test/projects/good/app/Tasks/a/b/Working.js new file mode 100644 index 0000000..4955963 --- /dev/null +++ b/test/projects/good/app/Tasks/a/b/Working.js @@ -0,0 +1,14 @@ +'use strict' + +const Task = use('Task') + +class Working extends Task { + static get schedule () { + return '*/1 * * * * *' + } + + async handle () { + } +} + +module.exports = Working diff --git a/test/scheduler.spec.js b/test/scheduler.spec.js index c03f3d4..98043d0 100755 --- a/test/scheduler.spec.js +++ b/test/scheduler.spec.js @@ -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) => { From 067554d96e934929c84cfd76e1c58283f1a0ba55 Mon Sep 17 00:00:00 2001 From: Tochukwu Nkemdilim Date: Mon, 24 Aug 2020 14:25:20 +0100 Subject: [PATCH 2/2] Chore(Doc): Update Doc --- src/Scheduler/index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Scheduler/index.js b/src/Scheduler/index.js index 97d12a9..afb2617 100644 --- a/src/Scheduler/index.js +++ b/src/Scheduler/index.js @@ -119,19 +119,26 @@ class Scheduler { debug('scheduler running %d tasks', this.registeredTasks.length) } - _getFiles(directory, relativePath = "") { + /** + * 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 relativeFile = path.join(relativePath, file) + const relativePath = path.join(relativeDir, file) if (fs.statSync(fullPath).isDirectory()) { - result.push(...this._getFiles(fullPath, relativeFile)); + result.push(...this._getFiles(fullPath, relativePath)); continue; } - result.push(relativeFile); + result.push(relativePath); } return result;