Laravel Envoy: abort on error - laravel

Is it possible to abort on errors or call another task when a command fails?
This doesn't work:
#task('migrate', ['on' => 'web'])
cd {{ $currentReleaseDir }};
php artisan migrate || exit 1;
#endtask
It fails with the message (I know I can run --force, it's just a way to make the command fail for testing):
**************************************
* Application In Production! *
**************************************
But then it proceeds to run the rest of the deploy script.

Yes, you can use:
#error
echo $task;
exit; /*Or Do any other processing*/
#enderror
this outputs
<?php $__container->error(function($task) {
echo $task;
exit; /*Or Do any other processing*/
}); ?>
This is listed in the compiler functions here https://github.com/laravel/envoy/blob/master/src/Compiler.php

Related

Why don't laravel tasks run with cron?

I need to send emails from laravel tasks. I have this code for the task.
$schedule->call(function () {
$date = now()->addDays(1);
echo 'step 1';
$appointment = Cita::whereDate('date', $date)->with(['patient'])->orderBy('date')->orderBy('hour')->get();
echo 'step 2';
foreach($appointment as $appt) {
$data = array("name" => $appt->patient->name, "body" => 'Remember me');
if ($appt->patient->email !== null) {
Mail::to($cita->paciente->email)->send(new Reminder($data));
}
echo 'step 3';
}
})->everyMinute();
When running the command
php artisan schedule: run
manually in the terminal, everything runs correctly and I receive the email. Using mailtrap.
Then I add this
* * * * * cd / app && php artisan schedule: run >> /app/log.log 2> & 1
in crontab.
I send the command output to the log.log file to be able to view the echo.
and only gets to paint the first echo (step 1) and the rest of the echoes are no longer added to the log.
this is the result shown in the log.
Running scheduled command: Closure
step 1
No error is displayed and it does not send me the emails.
the version of laravel I use is 7.15.0
I hope someone can help me. Thank you.

How to set a password in a Laravel Envoy script?

In Laravel 5.8 using envoy I want to set the password of a user in console command, like
envoy run Deploy --serveruser_password=mypass1112233
Having in envoy file:
#setup
$server_login_user= 'serveruser';
$user_password = isset($serveruser_password) ? $serveruser_password : "Not Defined";
#endsetup
#task( 'clone_project', ['on'=>$on] )
echo '$user_password password ::';
echo $user_password;
But $user_password output empty in both cases :
1) if serveruser_password is set in command
envoy run Deploy --serveruser_password=mypass1112233
2) or it is empty
envoy run Deploy
But I expected "Not Defined" outputted...
Why error and how correct?
Try the following.
#setup
$server_login_user = 'serveruser';
$user_password = isset($serveruser_password) ? $serveruser_password : 'Not Defined';
#endsetup
#servers(['local' => '127.0.0.1'])
#macro('deploy')
clone_project
#endmacro
#task('clone_project')
echo 'The password is: {{ $user_password }}.';
#endtask
Please make sure your macro is named "deploy" and not "Deploy." Also, in your echo statement, use curly braces to echo out your set variable. The output will be as follows.
$ envoy run deploy --serveruser_password=mypass1112233
[127.0.0.1]: The password is: mypass1112233.
$ envoy run deploy
[127.0.0.1]: The password is: Not Defined.

task sceduler in laravel 5.6 is not repeating as defined

I defined a task in laravel. first time that i run 'php artisan schedule:run' it works just fine but task is not repeating. just runs one time. i have no idea what is wrong!
$schedule->call(function (){
$users = User::all();
foreach ($users as $user){
$user->type = 'admin';
$user->save();
}
})->everyMinute();
You should run your cron automatically then follow below step:
1) go to your terminal: and run crontab -e command.
2) This will open server crontab file, paste below code inside, save it and exit.
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
Note: path means your project folder path
(i.e * * * * * php /var/www/html/yourproject/artisan schedule:run >> /dev/null 2>&1)

Bash error handling to variable

I have a Setup that doesn't work at the moment:
Lets say I have a program.sh
source path/service_x.sh; install_service_x || error_exit
source path/service_y.sh; install_service_y || error_exit
[...]
A service_y.sh with the function install_service_y
install_service_y()
{
[doing funny stuff that likes to fail ;)]
}
An error.sh
with the function error_exit inside which is doing many stuff.
Example:
error_exit()
{
echo "{$error}"
[...]
echo "Here are the error Logs. Error: {$error} appeared" | mutt -a "a_log_file" -s "failed to install service" -- error#yxz.com
}
So, I'm running the program.sh and install_service_y fails,
I want to have that error message stored to a variable $error to use it in the error_exit function of the error.sh
I really hope that you can understand my setup and whats going wrong.

CRON JOB - LARAVEL - OUTPUT

Hi I'm running CRON JOB with Laravel
Function declaration in Laravel
protected function schedule(Schedule $schedule)
{
echo "test CRON JOB\n";
$file1 = '1.log';
$file2 = '2.log';
$schedule->command('command1')->sendOutputTo($file1);
$schedule->command('command2')->sendOutputTo($file2);
}
CRON JOB - Setting
pathToArtisan schedule:run 2>&1 >> /home/log/cron_output.log
Log file output (cron_output.log)
test CRON JOB
Running scheduled command: '/opt/alt/php55/usr/bin/php' 'artisan'command1 > '1.log' 2>&1 &
Running scheduled command: '/opt/alt/php55/usr/bin/php' 'artisan' command2 > '2.log' 2>&1 &
The echo in the function schedule is displayed but the ones inside my command 1 and command 2 are not.
I tried
echo "test"; $this->info('test');
No files 1.log or 2.log where created neither /home/log/ or where the Kernel.php file is or Command folder
Any ideas ?
Thank you
You should use the built-in task output method in Laravel.
For example:
$file = 'command1_output.log';
$schedule->command('command1')
->sendOutputTo($file);
Everything is ok now.
$schedule->command('command1')
->sendOutputTo($file);
and inside your command
$this->info('test')
The files are created in the root folder of my application so I didn't see them !
Thank you
$schedule->command('my:command')
->daily()
->onSuccess(function () {
// The task succeeded...
//set flag here and store to DB
})
->onFailure(function () {
// The task failed...
//set flag here and store to DB
});
Show list in admin dashboard so that you can check logs using these flags(cron status)

Resources