Does task scheduling in Lumen work just like in Laravel? - laravel

You can see task scheduling explained in the latest docs for Laravel, but Lumen's docs do not mention this. However, it looks like Lumen's Console Kernel file has a schedule method just like Laravel. Does scheduling work the same in both, or what are the caveats with scheduling in Lumen?

Yes, yes it does! :) It works exactly the same.
crontab -e
And then add
* * * * * php /var/www/domain.com/public_html/artisan schedule:run 1>> /dev/null 2>&1
Come to think of it, the php artisan schedule:run kinda gives it away :p

Related

Laravel not executing scheduled command

I'm using laravel telescope. I used the following code to schedule the prune command in kernel.php (it has 48 hours but I already tried with less hours):
$schedule->command('telescope:prune --hours=48')->daily();
I tested it in my local environment and it prunes the data correctly. But once in my production server it doesn't seem to be working.
Telescope is runing there and capturing data. I thought it could be a telescope command issue, but I ran
php artisan telescope:prune
and it works, it says "25404 entries pruned", I also verified this in the DB. Also, my cron task is runing every minute, and I have some scheduled calls like this:
$schedule->call('*some irrelevant stuff*')->cron('0 */3 * * *');
$schedule->call('*some irrelevant stuff*')->dailyAt('00:00');
And they are working as expected. I also tried calling the scheduller manually inside a function:
Artisan::call('schedule:run');
and the schedule calls work, but "$schedule->command" does not.
So, I'm guessing that the problem is that somehow "$schedule->command" is not working. Do you have any idea or suggestion to fix this?
You should set Laravel Task Scheduler to your's servers crontab file, adding the following entry:
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

Laravel scheduled job did not resume after maintance mode

I did a maintanace on my laravel app by php artisan down. Then again after 15 minutes I did php artisan up.
But the scheduled jobs have not still resumed. They are not being executed as it was before.
Schedueled jobs are not being executed even when I manually give the command. ex: php artisan my:command
I think by making logs of your cronjobs will help you.
First Method
remove this part from you cronjob
>> /dev/null
Now add log file path in you cronjob
* * * * * php /var/www/html/project/artisan schedule:run >> /var/www/html/project/mylog.log 2>&1
create a log file and this file 777 (writable) permission.
so you can check what actually happening
Second Method
Or you can use method appendOutputTo() in your kernal.php
$schedule->command('inspire')->everyMinute()->appendOutputTo($filePath);
$filePath is a path of a log file you can give a public path or storage path

How to check cronjob is working for laravel app in digital ocean server?

This is scheduling code in laravel.
Console .php
$schedule->command('mail:customer')->dailyAt('9:00 am');
$schedule->command('renew:booking')->dailyAt('9:00 am');
And I 'd like to run this on an Ubuntu server. I am just using crontab for email sending in laravel application. For this, I had used
* * * * * * php /var/www_testing/artisan schedule:run 1>> /dev/null 2>&1
This command in digital ocean server. But I am not sure if this is working or not. So I just need to check this is working. Is there exact and fast way for testing?
yes use facility of logs provided by laravel.
you can put something like this in your Job or Command whatever you have used.
public function handle()
{
Log::info('Cron Job Started');
// your logic
Log::info('Cron Job Ended');
}
This will put a log into Laravel.log file and if the cron is running perfectly you will get the log here..
Also you have syntax error here try this.
* * * * * php /var/www_testing/artisan schedule:run 1>> /dev/null 2>&1

Cron job of my custom command in Laravel 4.2

I know there is Laravel 5.* now but for many reasons I'm using Laravel 4.2
I have a custom command, and in the fire() method, I import a file and seed the information in my database, so my class is some like this
class SeedDataFile extends Command {
protected $name = 'import:file';
public function fire(){
$command = 'mongoimport --db things --collection users --type csv --file file.csv --headerline';
$result = exec($command, $output, $return);
}
}
I want the file that is seeding data, i.e. every 12 hours (considering the file every 12 hours is changing with new data), but as an user I don't want to type the command in my terminal:
php artisan import:file
..every 12 hours (I just want to upload the new file to my project).
So the cron job is where I do all the work, but I don't want to do this:
crontab -e
and edit the file
I want to setup the schedule of my command in one class o somewhere in the code, and automatically the custom command is running everyday until a determined date.
Is this possible? Or I have to configure the crontab file?
You could take a look at this package, description says that it does exactly what you want
https://github.com/Indatus/dispatcher
As is Laravel 4.2 does not offer this type of functionality. In fact you'll need to set up 1 cron job for this functionality no matter the version you're using.
Newer versions of Laravel do indeed provide this functionality, but they still require you to set up 1 cron job (scheduler on Windows) at highest frequency available for Laravel's command php artisan schedule:run.
If you want to have scheduling of your commands in your code (rather than cron file) you could replicate what newer versions of Laravel do.
1. you would create a "master" command, let's call it TaskSchedulerCommand and let's say it's signature is php artisan schedule.
2. you would create a cronjob at highest frequency on the TaskSchedulerCommand like this: * * * * * php /path/to/artisan schedule`
3. you would write all the scheduling logic of other commands within the TaskSchedulerCommmand

How to setup Laravel 5's task scheduling in Heroku?

I'm trying to follow the Laravel Documentation on how to Run Cron Jobs,
and I want to add this
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
But I don't know how to add it to Heroku.
I created this Scheduler which runs once a minute and is part of your own app.
https://gist.github.com/robbydooo/65bf341ea0f4081150b945bfb1d38d3c
It creates a new Dyno type called Scheduler which you start one of.
Make sure you run jobs on your queue to avoid this scheduler over running once per minute.
To use this scheduler with Laravel 5.4+ add this file to /app/Console/Commands/RunScheduler.php
Register this file in app/Console/Kernel.php
protected $commands = [
…
Commands\RunScheduler::class
…
]
Add this line to your Procfile:
scheduler: php -d memory_limit=512M artisan schedule:cron
Heroku has a cron scheduler addon that you can use for scheduled tasks.
You can install it like this:
$ heroku addons:create scheduler:standard
Have a look at this article for more information.
It's been a while since this question was asked but recently I've had a similar issue and was able to overcome it using this blog post:
https://dev.to/autoidle/run-laravel-scheduled-jobs-on-heroku-2ah6
You can basically create a console command using this artisan generator:
php artisan make:command SchedulerDaemon
then you can edit app/Console/Commands/SchedulerDaemon.php and edit some lines as described below:
...
class SchedulerDaemon extends Command
{
...
// Change $signature value
protected $signature = 'schedule:daemon {--sleep=60}';
...
// Change $description value
protected $description = 'Triggers scheduler every minute or --sleep seconds interval';
...
public function handle()
{
// Change handle() function body to this:
while (true) {
$this->info('Calling scheduler');
$this->call('schedule:run');
sleep($this->option('sleep'));
}
}
...
}
Then, add this line to Procfile:
scheduler: php artisan schedule:daemon
And remember to enable scheduler process on heroku dashboard or run:
heroku ps:scale scheduler=1
I hope it becomes helpful to others in the future!
Cron To Go is an add-on that allows you to run Laravel's task scheduler every minute (* * * * * in cron). Simply install the add-on, add a job with * * * * * as the schedule and php artisan schedule:run as the command, sit back and relax! If your Laravel logs don't show up for the scheduler, there's a quick fix for log routing described here.
A free alternative can be to use the free worker dyno and configure it as follows in the Procfile
web: vendor/bin/heroku-php-apache2 public/
worker: bash -c "while [ true ]; do (php artisan schedule:run &); sleep 60; done"
This command creates a while loop for run schedule:run every minute.
Then you must exec heroku ps:scale web=1 worker=1 to enable worker dyno.

Resources