Commands hang and never finish in AdonisJS

After working properly for some time, a weird issue started happening in AdonisJS whenever I would run any command, even in production.

The command would start, and seemingly do the job, but it would hang and never finish. I would have to kill the process manually with Ctrl + C.

Turns out the culprit was Redis, I added it right before the issues started happening, because it was opening a connection and command could not close it and successfully finish.

Since Redis is added as a provider in AdonisJS, and all providers are started when the application starts, the connection would be opened, but there was no way to close it after the command finishes.

The solution was fairly simple, once I dug into the documentation - I just needed to define the environment in which the Redis would run in the adonisrc.ts file.

This was before:

{
  providers: [
    () => import('@adonisjs/core/providers/app_provider'),
    () => import('@adonisjs/core/providers/hash_provider'),
    // other providers
    () => import('#providers/queue_provider'), // <-- this is where a connection to Redis is made  ]
}

And to make it work, I had to do it this way:

{
  providers: [
    () => import('@adonisjs/core/providers/app_provider'),
    () => import('@adonisjs/core/providers/hash_provider'),
    // other providers here
    {       file: () => import('#providers/queue_provider'),       environment: ['web']     },   ]
}

You can find a relevant AdonisJS documentation about services here: Service Providers.