adonis-resque icon indicating copy to clipboard operation
adonis-resque copied to clipboard

Add ability to auto-register jobs via user-configurable directories

Open iamtommcc opened this issue 6 months ago • 6 comments

Fantastic library @shiny! Good ergonomics and excellent documentation.

Small suggestion: currently it appears that the apps/jobs directory is hard-coded for job registration.

This is an okay default, but a bit inflexible. It's problematic for anyone building their app in a domain module-based structure e.g. app/contacts/jobs, app/reminders/jobs. This approach is increasingly common in larger scale Laravel/Adonis/Rails apps.

As a workaround, people could extend the ResqueProvider and replacing the register() function (which is probably what I'll have to do).

It'd be awesome if there was a setting that lets you define a custom glob to search for jobs by, similar to what Adonis core lets you do for test directories.

iamtommcc avatar Jun 23 '25 07:06 iamtommcc

Great suggestion! Would love to see someone open a pull request. If no one gets to it, I’ll try to take a look and implement it when I have some time.

shiny avatar Jun 23 '25 08:06 shiny

I might be able to help here if you can guide me regarding how to achieve this!

I am guessing something like

import path from 'path';

const JOBS_DIR = path.join(process.cwd(), 'app/jobs')
for await (const file of JOBS_DIR) {
   let jobs = await import(file);
   await jobs.default();
}

AlphaRomeoMike avatar Jun 28 '25 17:06 AlphaRomeoMike

I might be able to help here if you can guide me regarding how to achieve this!

I am guessing something like

import path from 'path';

const JOBS_DIR = path.join(process.cwd(), 'app/jobs')
for await (const file of JOBS_DIR) {
   let jobs = await import(file);
   await jobs.default();
}

You can take a look at the importAllJobs function in jobs.ts. It looks something like this:

const jobs: Record<string, unknown> = await fsImportAll(app.makePath('app/jobs'), {
  ignoreMissingRoot: true
})

Both fsImportAll and app.makePath are utility methods provided by Adonis.

In addition, we should use Adonis's built-in configure mechanism to make the jobs directory configurable.

shiny avatar Jun 29 '25 07:06 shiny

#13 resolves his issue

There are still a few issues.

  • If we go with automatic glob patterns, it will inevitably require introducing an extra package.
  • Also, node ace make:job isn’t able to automatically create the Job file.

shiny avatar Jul 16 '25 07:07 shiny

@shiny what are your suggestion here? Should we introduce fast-glob or restrict the user to add a relative path to the directory?

AlphaRomeoMike avatar Jul 16 '25 07:07 AlphaRomeoMike

@shiny what are your suggestion here? Should we introduce fast-glob or restrict the user to add a relative path to the directory?

I think your previous code is suitable for implementing the feature mentioned in the issue (except that we don’t need to check whether the filename includes "job").

If we want to support node ace make:job, we should check whether it's possible to create the stubs in jobsPath. If the path contains glob patterns, it might be best to simply disable the make:job command.

shiny avatar Jul 16 '25 08:07 shiny