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..afb2617 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,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 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) => {