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.
Related
For different reasons I need to read the PID of the queues of a supervisor in Laravel Horizon. The problem is that I can't find the information in the library.
I'm able to retrieve all supervisor like this:
public function index(SupervisorRepository $supervisors){
$supervisors = collect($supervisors->all())->sortBy('name');
return $supervisors;
}
or all workload (queue) like this:
public function index(WorkloadRepository $workloadRepository){
$workload = collect($workloadRepository->get())->sortBy('name')->values()->toArray();
return $workload;
}
but I don't see how I can get the PID of queue by the repository, last but not least I can't run shell command on my server so I must get it by either accessing Redis or Laravel Horizon.
Turn out you can't get it from horizon, you need to get it by questioning your os
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 :)
I tried php artisan queue:work, its runs great, the website no freezing, but how to run programatically via Controller? i run Artisan:call('queue:work') but its freezing (waiting the queue to finish) and end up gateway timeout, but the queue run successfully though.
Any suggestion?
Queues allow you to defer the processing of a time-consuming task, such as sending an email, until a later time.
So executing the queue worker from controller actually negate the purpose of the queues. Explain your exact use case in question to provide more details.
Try this in your controller function
use Symphony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
try {
$process = new Process(your artisan command,
null,
your environment,
[],
timeout(ex: 60000),[]);
$process->run();
} catch (ProcessTimedOutException $e) {
// you can show some flash message
// OR
return Response::json(['message' => 'some message'], 'desire response code');
}
In your case, you need to do some font-end hack, run this controller function as ajax and make some loading. And stop the loading after your desire time is pass.
This is not a best approach, you should not run queue process from controller, you should let your server to do this process by using supervisor, etc…
https://laravel.com/docs/5.1/queues#supervisor-configuration
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!
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();
}
}