Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run commands with the --domain option from another command #18

Closed
andrecolza opened this issue Jan 21, 2020 · 7 comments
Closed

Run commands with the --domain option from another command #18

andrecolza opened this issue Jan 21, 2020 · 7 comments

Comments

@andrecolza
Copy link

Hi, i'm trying to create the command domains:migrate in order to run the artisan migrate command on all domains.

My command handle function is as follows:

public function handle()
{
    $domains = config('domain.domains');

    foreach($domains as $domain) {
        $this->call('migrate', ['--domain' => $domain]);
    }

    return true;
}

When i run the command, i expect it to migrate to all 3 of my domains. But it fails and returns "nothing to migrate":

$ php artisan domains:migrate
Nothing to migrate.
Nothing to migrate.
Nothing to migrate.

How can i create a command to execute migration on all my domains?
Thanks

@andrecolza andrecolza changed the title Domains commands from another command Run commands with the --domain option from another command Jan 21, 2020
@gecche
Copy link
Owner

gecche commented Jan 21, 2020

Hi,

sorry but as pointed out in the docs and in some previous issues, the --domain option does not work from within another command or in general from within an already instantiated Laravel Container.

Note that this is not a fault of the package, but it is due to how Laravel works and from a general viewpoint it is right.
Technically it is not easy to explain but basically if it would work, you would be able to change the environment from within another environment.
Even the standard Laravel command option --env does not work in the same way.

The only way to do what you want is to create an external script running an artisan command for each domain.
This is planned to be added in a future release of the package as a general script (see #14 ) but it is not so easy.

I hope this helps and I close the issue.

Cheers

Giacomo

@gecche gecche closed this as completed Jan 21, 2020
@andrecolza
Copy link
Author

Yes, your explanation helped me a lot, i managed to do what i needed.

Thank you so much!

@centralcybersecurity
Copy link

Yes, your explanation helped me a lot, i managed to do what i needed.

Thank you so much!

Would you mind sharing the script/code that helped you to do it?

Thanks

@andrecolza
Copy link
Author

andrecolza commented Jul 20, 2021

Hi @newlaravelcoder , instead of create a command as in my first post, I run all the original commands with the parameter --domain.
For example in my App\Console\Kernel I use this code:

protected function schedule(Schedule $schedule)
{
    $currentDomain = app()->domain();

    $schedule->command('my:command --domain=' . $currentDomain)->dailyAt('1:00');
}

I use php deployer to deploy my app, and this was the hardest thing, since I cannot run a simple command on all domains when deploying, so I created a custom recipe to run my commands:

desc('Flush cache');
task('artisan:domains:cache:clear', function () {
    foreach(get('domain_list') as $domain) {
        run('{{bin/php}} {{release_path}}/artisan cache:clear --domain=' . $domain);
    }
});

To make it work I added the entire domain list (as is it in config\domain) into my deploy.php file, this let me to read it during deploy with get('domain_list').

I hope I have been helpful,
Andrea

@centralcybersecurity
Copy link

Hi, thank you for sharing your code.

protected function schedule(Schedule $schedule)
{
    $currentDomain = app()->domain();

    $schedule->command('my:command --domain=' . $currentDomain)->dailyAt('1:00');
}

I use php deployer to deploy my app, and this was the hardest thing, since I cannot run a simple command on all domains when deploying, so I created a custom recipe to run my commands:

desc('Flush cache');
task('artisan:domains:cache:clear', function () {
    foreach(get('domain_list') as $domain) {
        run('{{bin/php}} {{release_path}}/artisan cache:clear --domain=' . $domain);
    }
});

Can you tell me where this code is to go... should I add this in laravel kernel file or it is a separate php file or a bash file to run in the terminal?

To make it work I added the entire domain list (as is it in config/domain) into my deploy.php file, this let me to read it during deploy with get('domain_list').

How do I run the command - through web route or terminal? should I run like - php artisan migrate --domain

I am very new to laravel.. if you could tell me where these files to be created/updated and what command to type in terminal, that will be really helpful. Thank you again for sharing your code.

@andrecolza
Copy link
Author

I used the first piece of code I posted in my App\Console\Kernel (where you schedule your cron).
If you don't use php deployer to deploy your app you don't need the second piece of code.

Anyway you have to use the --domain parameter wherever you want to run a command on all domains.

@centralcybersecurity
Copy link

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants