Core: Running ace commands in a controller

Created on 14 Feb 2020  路  9Comments  路  Source: adonisjs/core

In laravel you can call artisan commands via

      Artisan::call('email:send', [
       'user' => 1, '--queue' => 'default'
      ]);

So how can we achieve this in adonis controller.
I have tried this

  const { Command } = use('@adonisjs/ace');
  class ConfigurationController {

    //here run ace commands
    async runConfigurations(){
       Command.run("adonis migration:run"); //this doenst work
    }
 }

How can we run ace commands from the controller.

Question

All 9 comments

Hey @Geowan! 馃憢

You need to require ace to run the command.

const ace = require('@adonisjs/ace')

class ConfigurationController {
  runConfigurations () {
    ace.run('migration:run')
  }
}

Also, this is more a question than an issue with the framework itself.
Since we want to keep this space clean, please use the forum or the Discord Server next time for this kind of request.

Hi @RomainLanz

After trying this am getting an error ace.run is not a function.

Oops, it should be ace.call().

HI after using this as

  const ace = require('@adonisjs/ace')

  class ConfigurationController {
    runConfigurations () {
      ace.call('migration:run')
    }
}

This throws an error migration:run is not a registered command.
When i check on the terminal via node ace there is migration:run command

Further checking via

       console.log("commands have ", ace.commands)

Doesn't contain the migration commands. How can i load the migration commands

A quick check on start/app.js found there is @adonisjs/lucid/providers/MigrationsProvider in the aceProviders which i believe is the one responsible for the ace migrations commands. But even with it included still it doesnt load migration commands on the controller but migration commands are available on the terminal (console) commands.

@Geowan is this issue was solved?

@maxsimych no it wasn't resolved. I ended up writing custom functions to create the tables with knex create table commands which is duplication of what ace migrations does,

I am not looking to allow to run ace commands from the HTTP server process as of now. It requires some proper thinking and time and will look into it later sometime.

@thetutlage Can I ask if there is a way to run a command from inside a Task (with adonis-scheduler)?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dezashibi picture dezashibi  路  4Comments

codingphasedotcom picture codingphasedotcom  路  3Comments

seanc picture seanc  路  4Comments

ghost picture ghost  路  3Comments

themodernpk picture themodernpk  路  3Comments