Get executed scheduled tasks in laravel 5.1 - php

In Kernel.php I have several tasks that runs once or twice a day.
When I call php artisan schedule:run in the console I can see a response with "No scheduled commands are ready to run" or "Running scheduled command: xxxx".
I want to retrieve this messages to store them while running function schedule(Schedule $schedule){} in Kernel.php
The last think I tried is using ob_start(); and ob_get_contents(); but theese only returns my own echo();.
Adding ->getSummaryForDisplay() to the register command line doesn't display if a command was executed or not.

You can get the output as described here:
$schedule->command('emails:send')
->daily()
->sendOutputTo($filePath);
to save the output to a file. Or:
$schedule->command('emails:send')
->daily()
->appendOutputTo($filePath);
to append it to a file.
You can also get the output by email but I believe you'll still have to use a file as well.

Related

Laravel 8, how to run a job (script) in the background?

I am trying to run a time consuming script in the background with Laravel 8, but cant quite get it to work. I try to follow the docs from here https://laravel.com/docs/8.x/queues in combination with the tutorial found here: https://learn2torials.com/a/how-to-create-background-job-in-laravel
As per docs, I should run the following commands to get strateted with Queue/jobs in Laravel
php artisan queue:table
php artisan migrate
Then we should create our Job with the following command
php artisan make:job TestJob
In App\Jobs\ is our newly created job-file: TestJob.php
Again following the docs, I should put my time consuming script/code in the handle() method of TestJob.php. I have written the following code in handle() for test purposes:
public function handle()
{
//Do some time-consuming stuff
sleep(30);
}
Next, according to the docs, we should dispatch our job with the following line of code TestJob::dispatch(), anywhere in our app, so for test purposes, I put this line directly into our routes file, like this:
Route::get('/', function () {
//Run this job in the background and continue
\App\Jobs\TestJob::dispatch();
//After job is started/Queued return view
return view('welcome');
});
That should be it, as I understand from the docs, but it is not working as I expected. The code in handle() gets executed, but the return view('welcome'); is executed AFTER the the job is completed.
I was expecting the script to be executed and while running in the background the next line of code will be executed. How can I make it run in the background so the user do not have to wait for the script to finish?
I have googled a lot and according to the tutorial linked to earlier, I should have the following line: QUEUE_DRIVER=database​ in my .env file. I have sat this, and also sat it in Config\queue.php with the following line: 'default' => env('QUEUE_CONNECTION', 'database'),, but still same result
I also found the following solution for Laravel 5 here on SO (link), where there is suggested that we also should run the following code to get it to work: php artisan queue:listen, but its the same result again
Any help would be much appreciated!
By default the .env file has QUEUE_CONNECTION=sync.
Meaning, the sync connection uses the main thread for the execution of tasks. Hence, it has to first complete before moving on to the next line of code.
To make tasks run in the background so that your main application thread won't block and you can serve your client requests more quickly, try using a different connection i.e database.
To do this, simply change QUEUE_CONNECTION=database in your .env file.
You may run php artisan queue:listen on your local computer set-up to process tasks as they come in.
NOTE: On the production server, it may be more convenient to set-up something more robust to automatically restart your processes if they fail. Supervisor Configuration

Laravel dispatch job run async

I have a function that posts some content and pushes a job onto a queue and returns response to user even before the queue complete the job.
for that I changed the .env QUEUE_DRIVER to database, And records is saved in table jobs, but to execute this jobs I have to call the command php artisan queue:work, and that is my question: how do I call this command in the code or what should I do whenever there is jobs in the table?
The command
php artisan:queue work
Should be runnig always it will check if there is new jobs he will dispatch them
But it should be always running you can't execute it from the code
Also you can run
php artisan queue:work --tries=5
This for example will try 5 times then it will stop
Plus you can install supervisor it will always start the queue:work if it faild

Laravel cron on Digitalocean

I have set up Laravel scheduler to run my custom commands at specific time. Now I wanted to set up cron on Digitalocean server to trigger schedule:run each minute to check if something is scheduled at the given time.
After initial SSH-ing to server, I have run crontab -e and added the following line to it:
* * * * * php /var/www/Laravel artisan schedule:run >> laravel_cron.log
but the problem I'm facing is that I don't see anything written in laravel_cron.log, but it does get created, so now I have no idea whether my commands will actually be ran.
To test it out, I have tried entering php /var/www/Laravel artisan but I get no output in command line.
If I change the route to say xyz/www/Laravel it is saying that it can't find it, so I guess the route is set up fine. Also when I manually go to the Laravel folder and run php artisan without the route in the middle, I get the standard output.
I believe the command php /path/ artisan schedule:run does not return any output.
If you want to log the output of a task, you can use sendOutputTo or emailOutputTo
e.g.
$schedule->command('foo')
->daily()
->sendOutputTo($filePath)
->emailOutputTo('foo#example.com');
More examples can be found here

Schedule laravel 5.3 tasks to run every minute in windows

I'am developing a project in laravel 5.3 where in a particular command that I created prints "Hello".
I want this command to be called after every 1 minute.
I know in linux you can use cron to schedule the tasks but I'am using windows so I used the windows taks to schedule a .bat file to run after every 1 minute,but it doesnt work.
My .bat file looks like this
cd c:\Users\User\Desktop\alerts
C:\wamp64\bin\php\php7.0.10\php.exe artisan schedule:run 1>> NUL 2>&1
The schedule function in the Kernel.php looks like this:
protected function schedule(Schedule $schedule)
{
$schedule->command('custome:command')->everyMinute();
}
And the actual task that will be called looks like this
public function handle()
{
echo 'Hello';
}
It doesn't give any output. It should automatically print "Hello" after a minute right? But it doesn't. Where is the output shown?
I am going to simplify the process for you.
The content of your .bat file should rather be
"C:\wamp64\bin\php\php7.0.10\php.exe" "c:\Users\User\Desktop\alerts\artisan" "schedule:run"
NB: According to the directories from your question.
Create a task and call this .bat file as an "Action" in the Task Scheduler on your windows.
public function handle(){ logger("Hello"); }
The logger function will output your message to the default laravel log file "laravel.log" for u to see at the bottom.
Hope it helps others too.

Laravel 5 How to run a php file via scheduler

First time ever setting up a cron/scheduler. How do I run a php file with the scheduler? This is what I see in Laravel's documentation...
Entered the following command via Putty/SSH...
php /path/to/artisan schedule:run >> /dev/null 2>&1
Now... In the Kernel.php file... do I simply add the path to the php file that I want to run in the statement below?
$schedule->exec('node /home/forge/script.js')->daily();
After a few days... I have it working.
I set up the cron job in my cpanel. I set it up to run every minute so I could get good feedback on what I was doing wrong. I created a text file to record the errors.
php /home/accountname/artisan schedule:run >> /home/accountname/cron-output.txt 2>&1
I thought I had to call the php file where the method was located. However, to call a method, you must put the full path to the method like so...
$schedule->call('App\Http\Controllers\ParseDataFeed#parseFeed')
->dailyAt('15:00')
->sendOutputTo('cron-output.txt');
I hope this helps someone.

Categories