When the laravel scheduler starts a command like:
$schedule->command('test:testcommand')->hourly();
i need to find out, inside the command, if it has been started via
artisan test:testcommand
or
artisan schedule:run
i looked into $_SERVER['argv'] but i don´t see any info that helps me to
identify this.
maybe laravel has some fancy internal functions, but i wasn´t able to find them.
The only way to do this is to communicate through arguments. So you could do as follows:
$schedule->command('test:testcommand',['--scheduler'])->hourly();
You can do this with events:
php artisan make:event OnCommandRun
Then in the handle() of your testcommand class fire it:
event(new OnCommandRun());
Then within the event handle() function do whatever you want
More on Events
Related
callapi file (command file)i am new to php and got an assignment in laravel nova.
i need to update my database every hour. I've manged to do it manually but i cant seem to make it work in laravel task scheduler. this is the appropriate files (i hope). thanks in advance for the answers.
1.i created a new command
2. i add the command logic
3. i set the command to run every minute (for testing) in kernel.php
4. i ran php artisan schedule:run.
no response!
kernel.php file
command shows on php artisan list
I need to run a scheduler task from blade view by clicking a button (Sync) and it should go for process in the background.
I have created an artisan command that is php artisan projects:get and then I schedule it once a day for running in cron job, but in some case we need to run the cron job at user's choice so when he/she is in CMS logged in, they can click a sync button to run the cron job from there, but I think its not possible, but I knew there is some work around in Laravel to process the php artisan command which I already created that is projects:get using queues or process from symphony, but I know I can do it from the command line (terminal) using putty or cPanel terminal window, but As you know client can't login into cPanel and run the command so we need to give them just a simple button to click and sync in background, right now when user click that button, its getting delay and he/she can't continue to work on other things while its fetching all the projects from the API's that I used in that command. We need to queue/background process.
php artisan projects:get
As you pointed out, you can run artisan commands from your php code as documented in the documentation
Since the artisan command will likely take some time to execute, it is a good practice to use a queue for this.
You said in the comments, that you are on xampp. On local, you need to run the pap artisan queue:work command once you started xampp. After you executed the command, the watcher will pick up jobs and will execute them. However, first you need to configure the queue. This will get you up and running. On a production server, you need to configure a supervisor to run the queue command.
You can run artisan command programmtically like this:
Route
Route::get('/run/command', 'SomeController#runCommand');
SomeController
public function function()
{
$exitCode = \Illuminate\Support\Facades\Artisan::call('projects:get');
return $exitCode;
}
Hy! I have an application where I have to send some emails at certain actions (such as user creation, etc.). Problem is they are not running in the background, instead I have to wait until the process is done, and then it redirects me to another page.
I use database driver with queues, Laravel 5.2.
My code for email, for exp, after user creation:
$this->dispatch(new WelcomeEmail($user));
Artisan::call('queue:work');
where WelcomeEmail is the job that is pushed on queue. This type of code is placed in all the places where I want an email to be send. What is wrong?
First, you do not want to use Artisan::call on 'queue' commands within your dispatcher.
You should open your terminal and execute: php artisan queue:listen --timeout=0 --tries=1 and you should let it be.
Then you can visit your page where $this->dispatch or even better dispatch method will be called. Code on that page should be:
dispatch(new WelcomeEmail($user));
On your production server, you should use supervisord to monitor your php artisan queue:listen command, to make sure that it's up and running all the time.
For further reading please visit: https://laravel.com/docs/5.2/queues
I don't know why changing .env is not enough to fix the issue but after changing this line from
'default' => env('QUEUE_CONNECTION', 'sync'),
to
'default' => env('QUEUE_CONNECTION', 'database'),
in config/queue.php file
Everything works fine.
I had a similar problem, but because was a single job I didn't want a daemon to always run, also there is the update code problem,.... So I solved running the command directly from PHP, like:
exec('nohup php /my_folder/artisan queue:work --once > /dev/null 2>&1 &');
This will launch one job and then turn off, without waiting the result. But be careful on the Laravel log file permissions, the o.s. user can change if you are running under Linux depending on context and configuration.
Hope that can help someone.
I am trying to setup an sqs based message queue in laravel.
I setup my queue.php file and messages are successfully being retrieved when I run the artisan queue:listen command. However, I am getting a [ReflectionException] telling me my command doesn't exist.
The message stored on SQS for retrieval looks like the following:
{"job":"HELLOWORLD"}
And I have created a laravel/artisan command with the same name as per the instructions at http://laravel.com/docs/5.0/queues
php artisan make:command HELLOWORLD --queued
However, when I run the command
php artisan queue:listen
when the message is retrieved from SQS I get the following error
[ReflectionException]
Class HELLOWORLD does not exist
My question is how do I get laravel to recognize this command and once that works, how do I get laravel to see other items in the JSON message contained on SQS.
So for example lets say I have another index called "message" with the data "hello from sqs" making my json look like
{"job":"HELLOWORLD","message":"Hello from SQS"}
How would I go about accessing this other field?
Thanks in advance. The documentation for Queues in Laravel is pretty sparse.
The answer is that I was doing this completely wrong. You need to use the $contoller->dispatch method as shown in the Laravel queue documentation.
If you do it this way the message will be properly formed and any variables in the class you create will still be there.
I am new with laravel. I need to send email every day, for example - at 10pm. I know how to send email, but I can't figure out, how to send this email, or lunch my email sending function every day, at specific time.
This probably isn't as much to do with Laravel as it is a task scheduler.
Check out Crontab for running scripts on a schedule
http://crontab.org
For running a job every day at 1pm Monday through Friday.
0 13 * * 1-5 $HOME/scripts/weekday_email.php
Laravel does have a 'later' method for the Mail class, but it won't send email repeatedly.
Mail::later(5, 'emails.welcome', $data, function($message)
{
$message->to('foo#example.com', 'John Smith')->subject('Welcome!');
});
http://laravel.com/docs/mail
The best way to do this in laravel is to build an artisan command, and then call that command via a cron job like so:
1 * * * * /path/to/php /path/to/app/artisan command:name
This allows you to easily test the cron by running the command directly in the terminal. Apart from that, using Laravel artisan commands means that you don't have to worry about autoloading all the classes necessary to execute the command - Laravel will handle that for you.
php artisan command:name