Skip to content

Running the provider only when the program starts in Adonis 5 #4077

@dev-zarghami

Description

@dev-zarghami

I create a provider to connect with nats, which connects to nats every time my program runs

I defined a command that changes a value in the database using model

But I don't want my provider to be run when I execute the command

export default class UploadToStorage extends BaseCommand {
  /**
   * Command name is used to run the command
   */
  public static commandName = 'users:status:change'

  /**
   * Command description is displayed in the "help" output
   */
  public static description = 'upload all temp files to storage'

  public static settings = {
    /**
     * Set the following value to true, if you want to load the application
     * before running the command. Don't forget to call `node ace generate:manifest`
     * afterwards.
     */
    loadApp: true,

    /**
     * Set the following value to true, if you want this command to keep running until
     * you manually decide to exit the process. Don't forget to call
     * `node ace generate:manifest` afterwards.
     */
    stayAlive: false,
  }

  /**
   * Schedules AWS Rekognition requests for processing at a specified time.
   * Uses the AWS SDK to prepare and send the requests.
   */
  public async run() {
    // do somthings
  }
}
export default class NatsProvider {
  constructor(protected app: Application) {
  }

  public register() {
    this.app.container.bind('Adonis/Addons/NatsBroker', () => {
      const {validator} = this.app.container.use('Adonis/Core/Validator')
      const Logger = this.app.container.use('Adonis/Core/Logger')
      const Broker = new BrokerInit(this.app, validator, Logger)
      Broker.createConnection().then(() => {
        Broker.initRoutes()
      })
      return Broker
    })
  }

  public async boot() {
  }

  /**
   * When application is ready attach natsBroker to adonis http server
   */
  public async ready() {
    // App is ready
  }

  /**
   * When application is shutting down close all active nats connections
   */
  public async shutdown() {
    // Cleanup, since app is going down
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions