Laravel Queue Job doesn't respect timeout - php

I have this process setting.
php artisan queue:work beanstalkd --sleep=3 --tries=1 --timeout=0 --queue=medium,messages
I also have a job with the setting
public $timeout = 100000000;
But the job is stopped much before that with message has been attempted too many times or run too long. The job may have previously timed out Which was run for the first time.
I also have this in php.ini
max_execution_time = 0
What am I missing here?

Console
Things I want to execute from the command line (As you mentioned with your job creates more jobs). But the thing is, you could extract the business logic of it to a command.
Example: A console command with fires a command to fetch images from Imgur. The Class FetchImages contains the actual business logic of fetching images.
Command
Class which contains the actual logic. You should also be able to call this command from your application with app()->make(Command::class)->handle().
Example: Command mentioned in Example 1. Contains logic which does the actual API calls to Imgur and process returned data.
Jobs
Laravel 5.0 so jobs weren't a thing back then. But as I see it, Jobs are like commands but they are queued and can be dispatched. (As you may have seen in those examples, those commands implement your mentioned Interfaces SelfHandling and ShouldBeQueued).
I see changes in Commands and Jobs are quite difficult to understand.
EDIT:
From the Laravel Docs:
The app/Commands directory has been renamed to app/Jobs. However, you are not required to move all of your commands to the new location, and you may continue using the make:command and handler:command Artisan commands to generate your classes.
Likewise, the app/Handlers directory has been renamed to app/Listeners and now only contains event listeners. However, you are not required to move or rename your existing command and event handlers, and you may continue to use the handler:event command to generate event handlers.
By providing backwards compatibility for the Laravel 5.0 folder structure, you may upgrade your applications to Laravel 5.1 and slowly upgrade your events and commands to their new locations when it is convenient for you or your team.

Related

How can I run background tasks in Gitlab CICD?

How can I run a service-based command after the build process in gitlab-ci.yml?
For example, i'd like to run:
php artisan queue:listen --timeout=0 &
The issue is the build runs perpetually and does not finish as it waits for the results of this command (even though this command never finishes).
Is there anyway I can run it as a background task? I tried nohup with no luck.
As mentioned here:
Process started with Runner, even if you add nohup and & at the end, is marked with process group ID.
When the job is finished, the Runner is sending a kill signal to the whole process group.
So any process started directly from CI job will be terminated at job end.
Using a systenter code hereemd service (as in this same page) remains an option, if you control the target server.
With VonC's help - this is the approach I took.
I use Alpine Linux so slightly different to the link he provided, but same approach.
I created a file in /etc/init.d and gave it chmod +x permissions.
With the following contents:
#!/sbin/openrc-run
command="php /var/www/artisan queue:listen"
command_args="--timeout=0"
command_background=true
pidfile="/run/${RC_SVCNAME}.pid"
I then ran it with rc-service laravel-queue start within the gitlab-ci configuration file.

laravel command php artisan make:model run all commands in Commands folder

Why php artisan run all commands in the folder Commands when I create a model for example ?
Did I made something wrong ?
Laravel will initialize all the commands (User defined and Default commands) whenever you run any command in laravel application.
Which means don't perform any operation in __construct method of the commands.
For Example:
If you are performing any Databse operation in __construct method of custom command it will perform Databse operation no matter what command to ran.
So Move all the logics and operations to handle method of command.
I have faced the same issue back in the day You can see the github issue to clarify.
Artisan is the command line interface included with Laravel. Artisan exists at the root of your application as the artisan script and provides a number of helpful commands that can assist you while you build your application.
See more at : https://laravel.com/docs/8.x/artisan

Is it better to do this conversion job using Queue or Schedule?

I need to convert/resize uploaded images using Laravel so they are available during the next seconds or minute or so...
I was wondering, to have less pressure on the server, is it better to do this using Laravel Schedules or Queues
If Queues is the way to go, how to dispatch? (delayed?)
If you are expecting multiple users uploading multiple images concurrently, in that case, I would stick with Queue for this task. Here's a link to Laravel 7.x Queue, they have really good documentation.
Step 1: php artisan queue:table & php artisan migrate. This will create 2 tables: jobs and failed_jobs.
Step 2: Create a Job php artisan make:job ResizeImage. This will create a new file under jobs folder. It's the implements ShouldQueue that's gonna do the magic for you.
Step 3: To make a dispatch call. ResizeImage::dispatch($params);. To delay, add ->delay(now()->addMinutes(10));
Step 4: In your .env file, change QUEUE_CONNECTION=sync to QUEUE_CONNECTION=database. This config will be used by config/queue.php file.
Step 5: Clear config and cache. php artisan config:clear php artisan cache:clear
Things to understand:
When you dispatch a job, a new row will be added to jobs table. Whatever you add to the handle() method in your jobs file will be added to the payload column of the table.
To trigger the queue, you can either add a supervisor to your server or a simple php artisan queue:work will run the jobs.
Please go through the docs, they have really good + deep + better explanation with examples. Cheers!

Laravel - Execute queued job automatically [duplicate]

This question already has answers here:
How to keep Laravel Queue system running on server
(20 answers)
Closed 4 years ago.
I have jobs to send several emails.
In my controller I call the job:
dispatch(new SendStartPatEmail($data));
And record is saved in table jobs.
But to execute the job I have to run php artisan queued:work manually.
How can I do this automatically?
There are lots of different ways, all depending on the environment that you're using. Laravel tends to recommend using Supervisor to monitor your queue workers and keep them running.
Alternatively, you may wish to have your jobs execute immediately, instead of adding them to a queue. You can do this by setting your queue driver to sync, either in your config:
config/queue.php
'default' => env('QUEUE_DRIVER', 'sync'),
or in your .env file (assuming your config is set up as above)
.env
QUEUE_DRIVER=sync
Already answered here
Yes, if you use Linux you can use for example supervisor which will
run php artisan queue:listen (you need to add this command to
supervisor configuration file) and it will make sure all the time this
command is running.
php artisan queue:work is a simple command that listens to a queue and executes some jobs.
What's the whole concept?
You can run this simple command on the background and all jobs in queue will be executed.
But running a process (queue:work) on the background is not always safe.
Why? because there is always the chance that the process may be terminated or stuck because of a memory leak.
In this case laravel recommends the use of a Supervisor .The supervisor is another process working like a service.It is responsible to check whether the process that php artisan queue:work creates, works normally or it should be restarted.
This way php artisan queue:work runs on the background but there is a mechanism (supervisor) which can restart the process in case something goes wrong
There is the dispatch_now( ... ) method for specifying those jobs you want run synchronously.
I don't care for underscores, so I usually create a helper method dispatchNow( ... ) that calls the underscore version.. 😛

Laravel queued jobs don't appear in new relic if worker runs as daemon

I noticed that the queued jobs do not appear in new relic as transactions of any kind.
After digging for a bit I found that if I run my artisan queue workers "straight" they do appear just fine but if I run them as daemons (That's what I have set for my artisan queue:work commands in supervisord config) they do not.
Why does it work that way? Is there anything that can be done about it?
I wanna keep them with --daemon set to avoid framework bootstrapping for every single job. However being able to see what's going on in new relic is important as well.
Scheduled commands and regular http requests seem to be tracked just fine.
I'm running Laravel 5.2 on several forge servers with both php 5.6 and 7.0.
Thank you
New Relic added out-of-the-box instrumentation support for Laravel Queues as an experimental feature in version 6.6.0. Check if your agent version is at least 6.6.0 and then add this property to your newrelic.ini:
newrelic.feature_flag=laravel_queue
For more info, check out the release notes:
https://docs.newrelic.com/docs/release-notes/agent-release-notes/php-release-notes/php-agent-660169

Categories