adonisjs / core

AdonisJS is a TypeScript-first web framework for building web apps and API servers. It comes with support for testing, modern tooling, an ecosystem of official packages, and more.
https://adonisjs.com
MIT License
16.75k stars 637 forks source link

Adonis mail + node ace commands don't work #4564

Closed QU35T-code closed 4 months ago

QU35T-code commented 4 months ago

Package version

@adonisjs/mail@9.2.1

Describe the bug

Hey,

I think I found a bug with using @adonisjs/mail with node ace commands. Here's a classic example to send an e-mail with HTTP that works :

routes.tsx

router.get('/test', [EventsController, 'test'])

events_controller.tsx

@inject()
export default class EventsController {
  constructor(
    private action: FetchEventsAction
  ) {}

async test({}: HttpContext) {
    await this.action.sendMail()
    return 'hello'
  }

fetch_events_action.ts

sendMail() {
    return mail.send((message) => {
      message.to('test@example.com').subject('job:fetch report').html('Hello World')
    })
  }

image

Now, if I want to do exactly the same thing using a node ace command and not an HTTP request, I get an error.

commands/job.ts

export default class Job extends BaseCommand {
  static commandName = 'job:fetch'
  static description = ''

  static options: CommandOptions = {}

  @inject()
  async run(
    events: FetchEventsAction,
  ) {
    await events.sendMail()
  }
}

fetch_events_action.ts

sendMail() {
    return mail.send((message) => {
      message.to('test@example.com').subject('job:fetch report').html('Hello World')
    })
  }

I got this error :

qu35t@local:~/Documents/Project$ node ace job:fetch

[ error ] Cannot read properties of undefined (reading 'send')
          at FetchEventsAction.sendMail (file:///home/qu35t/Documents/Project/app/events/actions/fetch_events_action.ts:37:21)
          at Job.run (file:///home/qu35t/Documents/Project/commands/job.ts:22:22)
          at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
          at async Job.exec (file:///home/qu35t/Documents/Project/node_modules/@adonisjs/core/build/modules/ace/commands.js:51:28)
          at async #execMain (file:///home/qu35t/Documents/Project/node_modules/@adonisjs/ace/build/index.js:1481:7)
          at async AceProcess.handle (file:///home/qu35t/Documents/Project/node_modules/@adonisjs/core/build/src/ignitor/ace.js:66:9)

Reproduction repo

No response

Julien-R44 commented 4 months ago

Check this : https://docs.adonisjs.com/guides/concepts/application-lifecycle#during-the-console-environment

You need to set options.startApp

QU35T-code commented 4 months ago

Check this : https://docs.adonisjs.com/guides/concepts/application-lifecycle#during-the-console-environment

You need to set options.startApp

Oh, I missed this, thx @Julien-R44. Now I receive my email but the command doesn't stop in my terminal (as the application is running). My goal is to have a crontab that will run the node ace command at regular intervals, which seems problematic if the application doesn't stop... Do you have any ideas on how to solve this ?

qu35t@local:~/Documents/Archive-ctf$ node ace job:fetch

[.............................]
Julien-R44 commented 4 months ago

If your application doesn't stop, then that means there's something being not stopped and that keeps the process alive: a connection to a Database, a setInterval, a setTimeout ....

Please use Github discussions for this as it is not an issue