Delete queue job after multiple timeouts in Laravel using Beanstalk - laravel

I have a setup where sometimes a job will timeout. The problem is that it keeps hogging the queue and the other jobs won't run.
I want to delete the jobs which timeout 3 times and continue on with the queue. How can I do this? This is specific to laravel 4.2

The $job object has an attempts method that tells you how many times it tried to run:
public function fire($job, $data)
{
// Try to process. If failed:
if ($job->attempts() >= 3)
{
$job->delete();
}
}

Related

how to perform an heavy database related task in laravel that consume more than 30 seconds

I'm developing a binary multilevel marketing system in Laravel, at the registration time there we have to perform a task to entries for many types of bonus for each parent nodes of a new user. This task is time-consuming.
No one user want to see buffering and task taking more than 30 second that is not the right way.
I want to run this mechanism in the background and send a success message that your account created successfully.
You could use observers that trigger queued jobs.
After the user does an action on a model, the observers create queued jobs in the background. While the queue is being processed the user can continue working.
either implement laravel job and queues or use https://github.com/spatie/async.
you can invoke sub processes to make your task
use Spatie\Async\Pool;
$pool = Pool::create();
foreach ($things as $thing) {
$pool->add(function () use ($thing) {
// Do a thing
})->then(function ($output) {
// Handle success
})->catch(function (Throwable $exception) {
// Handle exception
});
}
$pool->wait();

Job always fails at laravel jobs Redis rate limiting

This is a follow up on
Laravel - Running Jobs in Sequence
I decided to go with redis rate limit. Code is below
jobClass {
protected $subscription;
public function __construct(Subscription$subscription) {
$this->subscription= $subscription;
}
public function handle() {
Redis::funnel('mailingJob')->limit(1)->then(function () {
// Job logic...
(new Mailer($this->subscription))->send();
}, function () {
// Could not obtain lock...
return $this->release(10);
});
}
}
And the controller code looks like.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Subscriptions;
class MailController extends Controller
{
public function sendEmail() {
Subscriptions::all()
->each(function($subscription) {
SendMailJob::dispatch($subscription);
});
}
}
Now, when i run the queue, some of them works rest(around 90%) failed with the below error in horizon.
SendMailJob has been attempted too many times or run too long. The job may have
previously timed out.
What am i missing? Please someone guide me to the right direction.
My goal is to run only one job of a type at one time.
[...] has been attempted too many times or run too long is an error that doesn't tell you why the job failed. It means some other exception has caused your job to fail every time it was attempted by the worker, and the worker has tried it the maximum number of times it was allowed to by your configuration. To understand why it's failing, check your laravel.log file for the exception that caused the job to fail.
In your case, since Mailer is contacting an external system it could be that the system you're connecting to is rate limiting you, or they could be having temporary connection problems or other service downtime. Again, there should be more detail in your log files.
The Laravel documentation has a hint about this:
When using rate limiting, the number of attempts your job will need to run successfully can be hard to determine. Therefore, it is useful to combine rate limiting with time based attempts.
The core of the issue is, the job keeps failing until it can achieve a lock and run.
So I imagine that where you are running your queue worker, you are not setting the --tries flag high enough.
Although you could just set a very high --tries, it is not really scalable.
The best solution, as suggested in the documentation, would be to increase the number of tries as well as using time based attempts
You can also increase return $this->release(10); the release time here. That should have the job wait longer before trying to reacquire a lock, so will use up fewer tries!

How to create thread in laravel 5.6?

In need run other function without stop.
How to use thread in laravel 5.6?
For example:
public function index()
{
$id = "123456";
$this->run_bot($id);
return view("index");
}
Funtion run_bot it takes about 10 minutes !!!!
I need run run_bot in a thread.
How to craete thread in laravel 5.6?
Look into Symfomy's Process Component.
As an example, you can start the process and then later wait for it to complete:
$process = new Process('ls -lsa');
$process->start();
// ... do other things
// this is optional, you don't need to wait if not necessary
$process->wait();
The solution you are looking for is how to run asynchronous jobs. This can be done with a queue service (like AWS SQS) and the Laravel queue worker.
It will allow you to send the job (really light work so it's really speed). And then, asynchronously, retrieve and execute the job.
Everything you will need to know is here :
https://laravel.com/docs/5.6/queues
Let me know if it helped you :)

Beanstalkd job switching to "ready" state

I am running using Laravel with the Beanstalkd queue driver. I have some long running jobs and I am getting an issue where after about 60 seconds or so the job will move from reserved back to ready state. the job is still running and completes without an issue. The problem is that if another job is added it will not be run next, instead the previous job that has moved back to ready will run. if the job completes before another is added it is not an issue though.
here is my code.
queue push:
Queue::push('myApp\Processors\BuildQuick', $job);
job code:
public function fire($job, $data) {
try {
//some code here that calls another class to build an amazon ec2
} catch (\Exception $ex) {
\Logging::joblog($ex->getMessage(), "ERROR");
$job->delete();
return;
}
}
$job->delete();
\Logging::joblog("Job Completed Successfully", "INFO");
}
update:
I have tested this with a sleep timer and it happens at exactly 1 minute every time. I know it is not throwing any exceptions and all my code does is sleep for 2 minutes.
I finally found what was causing this issue!
https://github.com/laravel/framework/issues/3480
this was added in laravel 4.1 I did not knwo this existed. you must change the default ttr in the queue config. Hope this helps others!

$job->release() is not putting back on queue

Very new to queues so be gentle. To my understanding, $job->release() is supposed to put the job back on the queue. I currently have the code below but it only runs the job through the queue once. I need to be able to run it through up to 5 times and if it fails again, delete it or something.
Worker:
public function fire($job, $data)
{
if ($job->attempts() < 5) {
\Log::error($job->attempts());
$job->release();
}
}
PUSH!:
Queue::push(
'ClassName',
[
'path' => $path;
]
Trying to do this locally with sync. Tried running queue:listen and queue:work, then running the push code. Only logged 1 entry. Let me know if you need more info.
Turns out $job->release() doesn't work when using the sync driver.

Resources