Posted on September 05, 2016 at 05:03 PM
Queues and task scheduler in laravel works like a charm. But it can fail in a certain case.
In this post, I am going to explain the case where it can fail and how to overcome this.
So let’s go step by step
1. We have setup a simple queue that sends an email to the admin regarding daily activity on the site and log Info before and after sending the mail.
2. If we have Log settings for single log file everything will work fine but if we have the daily file selected then it will break and will not be logged also. So you will have no idea what is going wrong.
So what is the problem here ?
When we had single log file then we had one log file : “laravel.log” which had full permission [777]. so we were able to log and everything was working as expected.
But when we have daily file selected then each day the laravel app will create a new file whenever it needs to log something.
Now this file’s owner is the web server (daemon/www-root) because laravel is run by the user daemon/www-root.
When the queue is getting processed the user in action is “cli“, and it does not have permission to write to this file. so it throws an exception and the processing stops. If you had a catch block also, you will try to log this error and the same exception will be raised as the user is still “cli“.
And this case can occur in the case of Task Scheduler[Cron Jobs] too, because while processing the scheduled task the user will be “cli“.
So, to overcome this, change monolog settings so that for different users/processes different log files will be created.
Add this code to bootstrap/app.php
/**
* Configure Monolog.
*/
$app->configureMonologUsing( function( Monolog\Logger $monolog) {
$processUser = posix_getpwuid( posix_geteuid() );
$processName= $processUser[ 'name' ];
$filename = storage_path( 'logs/laravel-' . php_sapi_name() . '-' . $processName . '.log' );
$handler = new Monolog\Handler\RotatingFileHandler( $filename );
$monolog->pushHandler( $handler );
});
Just before returning the app.
Now you will have log files for cli too, and everything started from the command line will be logged in that file.
It will look like
For Http App logs : laravel-apache2handler-daemon-2016-09-04.log
For Command line processes : laravel-cli-username-2016-09-04.log
This also keeps log files clean.
First of all, we will have different files for different days making it much easier to track errors.
Second HTTP app logs will be in a different file and the Queue/Cron logs will be in a different file.
Note : After changing the settings please don’t forget to run
# To make sure new config is loaded correctly
php artisan config:clear
# If you are using supervisor daemon queue worker, so you will need to broadcast the queue restart signal.
php artisan queue:restart
For More on Queue visit : Laravel Queues With Supervisor
Hope this helps. Happy Coding 🙂
Queues and task scheduler in laravel works like a charm. But it can fail in a certain case. In this post, I am going to explain the case where it c...
Some services run on different ports and to access them in the browser we have to mention the port number also. For ex : SonarQube : 9000 [http://1...
Sometimes you need to strike off a row in ExtJS grids, this can be easily done by CSS code. The CSS code will look like this, .strike-through-row {...
When you are working on a laravel project there is some basic setup that needs to be done for each install of the project. Like Permissions for ...
In this blog, we are going to take a look into Laravel Queues. Queues allow you to defer the processing of a time-consuming task, such as sending a...
PARTIALS Let’s say we have a structure like the following : data = { students : [{ name : ‘ABC’, roll : 1 }, { name : ‘DEF&...
PHP 8 has been officially released in November 2020! This new major update brings many optimizations and powerful features to the language. This...
ProxySQL is a high-performance SQL proxy. ProxySQL runs as a daemon watched by a monitoring process. The process monitors the daemon and restarts i...
Time is important. We should not waste it doing trivial things. Automate things wherever possible.