Laravel scheduled task without overlapping, run on demand - php

I have a scheduled task with Laravel defined as below to run every 10 minutes. I also need the same job to be run on-demand without it overlapping if it is already running or preventing the scheduled job starting to run if the on-demand job is running.
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$job = new \App\Jobs\ImportJob();
$job->handle();
})->name('Import')->everyTenMinutes()->withoutOverlapping();
}
Is there a nice, simple way of achieving this with the schedular API or should the Job take care of its own mutex flag?

Related

Laravel Job Overlapping

I am using the laravel middleware withoutOverlapping in laravel but it does not seem to work when jobs are dispatched at the same time (and take < 1 second)
Example
class TestWaitJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public Charge $charge;
public function __construct(Charge $charge)
{
$this->charge = $charge;
}
/**
* #return array
*/
public function middleware(): array
{
return [
(new WithoutOverlapping($this->charge->id))
->releaseAfter(30)
];
}
/**
* #return void
*/
public function handle()
{
$charge = $this->charge->current_state_key_name;
return;
}
}
This is an absolutely simple job. It does nothing and it is what I have used to test.
If I go to my application and dispatch two copies of the job at the same time (using tinker)
TestWaitJob::dispatch(Charge::Find(1)); TestWaitJob::dispatch(Charge::find(1));1;
Both jobs are processed by horizon at the same time.
If I add a super simple sleep(1) line to the handle method of the job I get the expected behaviour which is
Job begins processing and acquires lock
The next job cannot acquire lock so is released back to the queue.
So with this sleep line, I have a job processed immediately (in 1 second) and the next job completes 31 seconds later which matches up exactly with the releaseAfter(30 seconds)
I have been looking at this for hours and everytime I introduce a delay the jobs process as expected but with no delay they process at the same time. The application is financial in nature so I cannot afford to potentially process jobs at the same time
Any help/advice would be greatly appreciated. (ps I am using Redis as cache)

how to change laravel jobs time

I'm developing the app for the customer and he wants to start some jobs in special time
I must run it in jobs, that's right?
for example, he wants to publish a post have 2 status published or waiting
and in send page, he can set time for publish post
how I can develop this in jobs?
ScanJob::dispatch($property->Name, $property->Owner, $Scan->id)->delay(Carbon::now()->addHour(Carbon::now()->diffInHours($Time)));
it's my first try
get diff time in hours and add it from delay
There are basically two ways via which you can solve your problem:
Create a Laravel Artisan command(you can use other methods also that Laravel provides, but I found Artisan to be fun and more flexible, helps avoid the rework) and schedule it accordingly.
Create a Queued Job and dispatch it for some later time, but it has some limitation like, the Amazon SQS queue service has a maximum delay time of 15 minutes.
Now, what is to be done:
In my opinion, you should use Solution 1 as it is more flexible and gives you more control.
Queues are used for 2 things. First, ideally, the task you want to perform should be done in the next 30-45 minutes. Second, the task is time intensive and you don't want to block the thread because of that.
Now the FUN part.
Note: You need not worry, Laravel will perform the majority of the steps for you. I am mentioning each and every step for the sake of not skipping the knowledge.
Step 1: Run the following command to create an Artisan Console Command(Remember to be in your project's root path.):
php artisan make:command PublishSomething
The command will now be available for further development at app/Console/Commands.
Step 2: You will see a handle method inside the Class like following, this is where all of your logic will exist.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PublishSomething extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'something:publish';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Publishes something amazing!';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
}
}
Step 3: Let's add some logic inside our handle method
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->info('Publishing something cool!');
// you can add your own custom logic here.
}
Step 4: After you have added your logic, now we need to test it, you can do so like:
php artisan something:publish
Step 5: Our function is running all fine. Now we will schedule the command. Inside app/Console you will find a file Console.php, this class is responsible for all task scheduling registration, in our case.
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Notice the schedule function here, this is where we will add the schedule logic.
Step 6: Now we will schedule our command to run every 5 minutes. You can change the time period very easily, Laravel provides some pre-made frequency options, and you have your own custom schedule also.
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('something:publish')->everyFiveMinutes(); // our schedule
}
Step 7: Now, Laravel's task scheduler itself is dependent on Cron. So to start the schedule, we will add the following file to our crontab.
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
That's it! We are done. You have created your own custom command and scheduled it for every 5 minutes.
You can learn more about Laravel Artisan Command & Laravel Task Scheduling.
Hope it helps!

Laravel - Scheduling a Large Task

Trying to run a function in Laravel that's quite large and fetches a lot of data from Google Places API and stores parts in my database as new entries in a table. The problem is it auto-discovers new entries for me near my current entries, and that creates more jobs.
When I just access the command via GET it times out eventually. I've tried running it as a scheduled command with Redis but to be frank I can't seem to figure out how it works. I've created a job, I tried to queue it with dispatch, but then it tries to run it immediately right now and it times out eventually again.
How do I run this large task without it pausing my entire server?
Thanks
Zach
I is really simple and you do not need to stop server. It is just CRON job. Use Laravel Schedule.
When using the scheduler, you only need to add the following Cron entry to your server.
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Class example:
namespace App\Console;
use DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
DB::table('recent_users')->delete();
})->daily();
}
}
More: https://laravel.com/docs/5.6/scheduling
Nice video about laravel scheduling: https://www.youtube.com/watch?v=mp-XZm7INl8

Laravel: Run cron job tasks simultaneously

I have several tasks that I've scheduled to run at various times. Here are those tasks:
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\PostGetter::class
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('post_getter 0 5')
->cron('*/15 * * * *');
$schedule->command('post_getter 5 10')
->cron('*/15 * * * *');
$schedule->command('post_getter 10 20')
->everyThirtyMinutes();
$schedule->command('post_getter 20 30')
->hourly();
}
}
In order to log when each of these tasks is run, I've added the following piece of code in the PostGetter class to log when the task has begun running:
Log::info('Post getter for {arg1} and {arg2} has started.');
Where arg1 and arg2 are the two arguments in the scheduler (e.g. 0 5 or 5 10).
I've noticed in my log file that these scripts don't seem to run at the same time. For example, when the first task is run (post_getter 0 5), the second task (post_getter 5 10) only seems to run after the first task is done, and so on.
How can I make it so that all of the tasks listed in the Kernel class are run at the same time and don't have to wait for the previous task to finish?
Cronjobs / scheduled processes in Laravel are run sequentially.
There is an article about parallel processing of scheduled tasks in Laravel 4.3. Currently there is no parallel processing in Laravel 5.
I solved this by adding custom cron jobs instead of running it through the Laravel scheduler.
From Laravel version 5.7, If you would like to run commands in the background so that they may all run simultaneously, you may use the runInBackground method:
$schedule->command('analytics:report')
->daily()
->runInBackground();

Scheduling tasks after polling the database php

I am building web app in PHP which is used to schedule tasks in the future. In the background I am supposed to Poll the database every 2 min to see if there is any upcoming task. How do I implement polling. Javascript solution is not helpful as the user can be on any of the pages on the webapp, but the system should keep pooling the DB.
Cron is one way but still I have to poll the database to create a cron job. What is the best way to implement it?
Thanks
Create a cron job that is executed once every X minutes and make that job check the DB. If there is a new upcoming task, just make the job launch it.
Create an 'execute:tasks' artisan command, so you can poll your database whenever you need to execute your tasks. You should be able to run it this way:
php artisan execute:tasks
Your command will call some controller action (or a class method) to poll the database and find if there are tasks available to be executed.
Then you just need to create a cron job that will execute that artisan command every 2 minutes:
*/2 * * * * php artisan execute:tasks
This is an example of Artisan command:
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ExecuteTasksCommand extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'execute:tasks';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Find and execute available tasks.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return void
*/
public function fire()
{
(new ExecuteTasksClass)->findAndExecute();
}
}
You just have to name this file something like app/commands/ExecuteTasksCommand.php
And add this to app\start\artisan.php:
Artisan::add(new ExecuteTasksCommand);

Categories