I have a API where a user create integrations.
Those integrations may have recurrence (repeat every 6 months, every 2 days, etc).
In the UI when the user selects when to repeat is generated a cron (ex: * * * * *).
The recurrence is managed by the laravel schedule (https://laravel.com/docs/5.6/scheduling)
That cron is saved in the configurations of that integration of the user
EX: then in the Kernel.php :
$schedule->job(new IntegrateJob, 'integrationId')->cron('* * * * *');
Easy... I works!!!
But if the user deletes the integrations (soft delete), then I dont want to run that job in the selected cron.
I want to delete that entry in the laravel scheduler.
But that info is not saved anywhere, not in the database, not in cache, not in the redis server that manages the jobs...
If I cant find them I cant delete them... Any Solutions? Thank U
I think the proper way to do this would be changing your design so the event runs for all the users and then determines which user(s) to run the event for (if any). Also, the way Laravel handles scheduled tasks in different than the way Linux handles Cron jobs.
As an example:
If you have the following code in the app/Console/Kernel.php file and run php artisan schedule:run, the job is ran.
$schedule->job(new IntegrateJob, 'integrationId')->cron('* * * * *');
If you take out the above code and run php artisan schedule:run after a minute, the job will not be ran because Laravel does not persist the job.
Related
I have a setting section in my laravel application. In this section I have a setting for defining a sync time. So the user can set their preferred time for sync data from laravel application to a third party CRM(Salesforce).
For now, I am storing a sync time into Database table. Now I want to run a cron JOB at that time for a particular user.
I have already created a cron job script and the cron job script is working fine. I am able to test cron job manually I just need to automate it with user setting(Preferred time).
My Cron JOB URL https://my-domain.org/cronjob?user_id=101
I tried to use laravel scheduling but this not fulfillig my requirement.
Is there any other better solution available at laravel?
There is some information missing but I try to assume a few things to hopefully propose a working solution using the Laravel scheduler.
Assuming you store the time at which the CRON runs in the database as 01:00 for 1 o'clock in the night you could do the following:
protected function schedule(Schedule $schedule)
{
User::query()->whereNotNull('cron_time')->each(function (User $user) use ($schedule) {
$schedule->command('app:import', ['user_id' => $user->id])
->withoutOverlapping() // prevents running the same command (+ parameters) twice at the same time, should only be a problem if the command can run > 24 hours, but still a safe thing to do
->dailyAt($user->cron_time);
});
}
I dreamed up that you stored the information on the User model
That you used a flag in the database called cron_time which contains the 01:00 or is null if the cron is disabled
The cron needs to run daily at that user supplied time
The import command is called app:import and is a console command that accept a user_id argument with the user id to run the import for
This might help you adapt the above to how it's structured in you own app.
If you want to use queued jobs to execute the imports you could also create the app:import CLI command to just dispatch that job instead of actually running the import. Depending on the amount of imports it might be a good idea to prevent long running scheduler commands and have the ability to retry and have a timeout.
Another option could also be to have a daily scheduled command that dispatches jobs to the queue scheduled to run at a specific time (which does not work with the SES driver). For more about that see: https://laravel.com/docs/5.8/queues#delayed-dispatching.
I am going to create Task Shedule in laravel 5.6 in my app. I am working with windows 7 os and my localhost is WAMP. in laravel documentation there is add Cron entries to your server as
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
but I have not any idea about how to add Cron entries with My wamp localhost. my laravel file is in the desktop name as schoolproject then how can add Cron entries with my wamp?
For people who run a local WAMP server and their computer isn't always awake
I spent the past couple months figuring out what would work best.
First, set up your Kernel class and console commands as instructed in the Laravel docs.
In Windows, open Task Scheduler, and create a new task:
In the Triggers section, you can set it like this:
The action should be to run the PHP executable within your WAMP folder (using artisan schedule:run) as the argument:
But then here is an important difference from other tutorials I've seen:
For any tasks that are critical, you'll want to treat them separately.
For example, let's assume you have a task that backs up your database, and you'd like to run this every single day at noon, so your Kernel class has this command in its schedule.
But then you realize a flaw in this approach: if you were out at lunch a few days in a row and left your computer asleep, that noon task would have been missed, and now your backups would be quite stale.
So, remove that daily database backups command from the schedule in Kernel, and instead, then create a second task within Windows Task Scheduler, and set the trigger like:
Then in its Settings tab, you can choose "Run task as soon as possible after a scheduled start is missed":
Therefore, even if your computer is off or asleep at this task's scheduled time, Windows will know to run the task immediately upon waking.
I'm writing a CMS with the Laravel as a backend and Vue.js as a frontend. So far, I had no problems with the application, however, when I tried to create Scheduler to manage tasks from the frontend, I'm not able to run these tasks with provided Laravel cron task:
* * * * * php /var/www/html/artisan schedule:run >> /dev/null 2>&1
Well, actually I can, if I redirect the output of the CRON to the file and not /dev/null, Laravel is telling me that the job is executed successfully, however, nothing is happening.
To test out that created Scheduler class is able to call the jobs at an assigned times, I've written small node.js notification application, which simply sends the system notification.
In case if I'm calling the php artisan schedule:run myself, I have no problem with receiving this notification, and yet again, I have a new entry in the log file telling me that job executed successfully.
But when CRON executes the same artisan command, the only thing I get is entry in the log file but no notification.
I'm running Apache server under the same user which has this cron entry and the node.js script is placed under in the home path of this same user. So from here I've no idea why this might happen.
P.S. This is the interface which basically shows how the crontab is edited and what I'm adding to it to make this thing work (but for some reason it is just spitting messages to log without actually doing anything):
And this is how the task looks like:
Laravel provides an easy way for task scheduling. Laravel’s ‘Command Scheduler’ allows you to easily define the schedule of the commands within Laravel itself. When using the scheduler, only one Cron entry is needed on the server.
Currently i am using PHP 5.6.32 and Laravel Framework Lumen (5.4.6) (Laravel Components 5.4.*).
I am able to schedule my task as per the specific time like below
$schedule->call('App\Http\Controllers\SubjectController#frickcall', ["request" => $request])
->cron("14:23");
But after scheduling above call how could i remove it from scheduler list?
Just remove that line of code from your schedule.
Schedules are not jobs placed in a work queue in Laravel and Lumen, you are supposed to setup a cron job to run the scheduler every minute with a crontab entry like this:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Then the schedule will be evaluated every minute to determine if anything needs to be run.
If you were meant to be using work queues look at this.
I'm trying to make Laravel automatically handle the emailing queue but can't make the task scheduler working. The problem is like:
I already got jobs successfully in the database table, and in Kernel.php:
$schedule->command('queue:work')->everyMinute();
on the remote server I've run this command under the project folder:
* * * * * php artisan schedule:run >> /dev/null 2>&1
But the scheduler still refuse to work, as job still remains in the table. If I manually run
artisan queue:work
the email is sent then.
What am I getting wrong here? Many thanks!
Firstly I would suggest you to not use laravel's command scheduler.
Pros and cons of using laravel's task scheduler:
pros
Your cron task gets embedded to your code. So if you change your server you don't need to remember which all cron tasks you had.
cons
Let's say you have several other cron tasks. Task T1 runs every minute but task T2 runs every day while task T3 runs every Tuesday. Now to just check this you will be running a daemon which will check if every minute of you have any task in queue schedule. Also your queue should respect each jobs and their respective timings.
Instead what you can do is create separate commands for every task. And run cron jobs for them.
But even if you wanted to do what you were doing already or want to know why your cron task was not running, then here is what you were forgetting "running the artisan command in your project directory".
* * * * * cd path_to_your_laravel_project & php artisan schedule:run