Deployer PHP - Custom command task always failed - php

in my Symfony 5.1 project I am trying to declare my own task which I would like to use when deploying. I created it like this:
task('update_database', function () {
if($_ENV['DATABASE_URL'])
{
cd('{{release_path}}');
writeln('<info>Updating database</info>');
run('{{bin/php}} {{bin/console}} d:d:c --if-not-exists');
run('{{bin/php}} {{bin/console}} doctrine:schema:update --force');
}
})->desc('Make database updates');
The "cd" and the "writeln" work. But every time I try to run a command it always fails
I tried with just the command php bin/console instead of using {{bin/php}} {{bin/console}}.
I even tried a silly php bin/console cache:clear command to see, but same error.
Yet I am in the right directory.. I followed this tutorial https://www.codepicky.com/php-automatic-deploy/
And I do the exact same thing though
UPDATE :
I connected to my server, I went to the last release of my project, and when I tried commands, absolutely nothing was happening. No mistake, but they didn't do anything. Even if I try the command to create an entity, it is "ignored".
The -vvv flag didn't give anything either
Here is what I have in the console:

Related

Laravel run package artisan command in controller

I need to run this command when user change something in translation file
php artisan export:messages-flat
I need to add it in may controller
so I'm using this code
\Artisan::call('export:messages-flat');
but it return error saying that
The command "export:messages-flat" does not exist.
but when I
php artisan list
it's in the list
I also try to run other command
\Artisan::call('cache:clear');
and it works
this is the package I'm using link
kindly help me, sorry for may poor english
The package only allows you to run the commands from the CLI. You can see from the source code, they only register the Artisan commands if the application is running from the console.
As an alternative, you can call ExportLocalization::export()->toFlat() according to their documentation.

Laravel - Catch php artisan commands

I've did some changes in my config/app to use multiple databases selected by front-end,
now I have to tell in \Request()->header('database') which database I want access.
It's work perfectly, the problem is: when I try to do any artisan commands my logic dies, because isn't informed the database.
So I need to inform the database in artisan commands, like that:
php artisan migrate --database=sandiego_school
php artisan migrate:rollback --database=newyork_school
How can I observer all commands to get the argument?
In this case I guess you should create your own commands that overrides the commands you want to call, then in the handle method of the command you could specify the connexion you want to work on:
\DB::setDefaultConnection($connexion);
or also you can simply add header to request:
request()->headers->set('database', $dbname)

Ubuntu Screen command does not recognize Lumen Queue command

I am in a bit of a situation where I need to manually restart queues on linux/ubuntu based box and the proper approach I was told to follow is to use the screen command line application.
I am on Ubuntu 14.X and when I do an -ls command I can actually see the sessions running on the server. Below is a command I would run if I was outside screen session:
mycommandline$ php artisan queue:restart
Now the issue is that if from the -ls output I grab the session's port to include in the command I run I get the error below:
Cannot exec 'php artisan queue': No such file or directory
I also tried typing screen "php artisan queue:restart" and I keep getting the same issue.
I am not farmiliar at all with the screen program and any suggestions to point me to the right direction would be much appreciated.
Okay digging through google I came up with the command below. Given that I already had a session that was running I needed to make sure that I send my commands on that same session just to be sure. Below is what I ended up doing which does confirm that something is happenning within the screen session:
luyanda#devbox:~$ screen -d -r 19167 -p0 -x stuff "free -g; php artisan queue:restart"
Attaching from inside of screen?
As I am not seeing any visible issues I am convinced that this is what I needed to do given that my log file is updating as well it either means that the queues never stopped working or I managed to restart them.
What I am still interested to know is how I can actually see the output of the commands I send to my session else if I can rather pipe the output somewhere else.
Any thoughts?

Laravel Artisan - reload .env variables or restart framework

I'm trying to write a command as a quickstart to build a project. The command asks for input for database details and then changes the .env file. The problem is that the command needs to do some Database queries afterwards, but the .env variables are not reloaded.
My question would be, is there a way to reload or override .env variables runtime. And if not, is there a way to call another Artisan command freshly, so framework bootstraps again?
In my command I tried doing $this->call('build:project') in my actual command, but even in the second call the variables are not reloaded.
Is there a way to achieve this without making the user manually call multiple commands?
Thanks!
i had the same problem with reloaded .env variables, and i fixed it with this command line which allow you to clear the configuration :
php artisan config:clear
Hope that helped. Regards.
Laravel uses config files which take data from .env file. So, what you can do is to override configuration values at runtime:
config(['database.default' => 'mysql']);
Try to clear cache it helped me (couldn't ssh into the server)
Is there a {app route}/bootstrap/cache/config.php file on the production server? Delete it.
This helped me
As OP I'm trying to bootstrap a Laravel project build by running console command and asking for the database credentials in the middle of the process.
This is a tricky problem and nothing I read was able to fix it : reset config, cache, Dotenv reload, etc... It seems that once the console command / operation is initialized the initial database connection is kept all over until the end.
The working solution I found is to bypass, after database modification are done, this cached state by using native shell exec command and passing the php artisan command as parameter :
passthru('php artisan migrate');
So, the order will be :
php artisan project:build (or whatever is your console command)
prompt the user for database credentials
replace values in .env file (your search & replace algorythm)
run php artisan config:cache
passthru('php artisan migrate'); ro run your migrations
shell_exec would do the same but in silent mode while passthru will return the output generated by console.
https://www.php.net/manual/en/function.passthru.php
Done successfully on Laravel 8.52

Laravel migration cancelled

when I trying to migrate something I get this error:
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command?
Command Cancelled!
Im running a centOS 6.5 server without Plesk 12
Is there anyway to figure out what the error is or how to solve it?
Thanks
A bit late for answer but just for info purpose you can use php artisan migrate --force to avoid it OR in laravel 5.2 they are using a key env in config/app.php configure it to avoid this prompt
reference https://github.com/laravel/laravel/blob/master/config/app.php
Just use --force flag in production:
php artisan migrate --force
https://laravel.com/docs/6.x/migrations#running-migrations
Forcing Migrations To Run In Production
Some migration operations are destructive, which means they may cause
you to lose data. In order to protect you from running these commands
against your production database, you will be prompted for
confirmation before the commands are executed. To force the commands
to run without a prompt, use the --force flag.
Change your environment first from production to local . /bootstrap/start.php somewhere line no 26
$env = $app->detectEnvironment(array(
// find your machine name and replace mine
'local' => array('hassanjamal.local'),
));
I've just bumped into this problem as well. If you don't want to reconfigure your app (which is probably a good idea) hit 'Y' and then enter instead of just enter. It will go on even in production setting.
Simple,
$env = $app->detectEnvironment(function()
{
return 'development';
});
Good luck!

Categories