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.
Related
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
Error in running php artisan dusk.
On the screenshot, it's beginning of an error in JSON format. The context of error should be below.
Often case is related to Routes, therefore make sure that routes exist and middlewares disabled, specifically - EncryptCookies
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.
The first problem I'm running in to is that when installing I receive a mysql error stating that a table cannot be found. Of course it can't, I finished installing the dependencies much less run the migration. The error was being triggered by a Eloquent query in a view composer. After commenting out the entirety of my routes file Composer let me continue.
I proceed to uncomment out my route file - I get the error once again trying to run any artisan commands (can't migrate my database because I haven't migrated my database). Repeat the solution for step one and I've migrated my database.
Artisan serve is now serving me my layout file in the terminal and exiting. I'm at a bit of a loss to troubleshoot this. I assumed that it was possibly a plugin, trying to disable plugins one by one results in:
Script php artisan clear-compiled handling the pre-update-cmd event returned with an error
and being served up my layout file in the terminal.
It seems that the error is directly related to this function in my routes file:
View::composer('layouts.main', function($view) {
$things = Thing::where('stuff', 1)->orderBy('stuff')->get();
$view->with(compact('things'));
});
This isn't a new introduction to the application however so the underlying cause is coming from somewhere else.
As i said in the comment, if you are finding database errors in production server but not in local, then
check database credentials. if its ok then....
check the different configs in the environment.
using profilers(any) will let you know what environment you are in.
I've been searching for a while and think I have part of the information I need but just need some assistance putting it all together.
What I'm trying to achieve is to call a URL (a codeigniter controller) on a regular basis e.g. every 5 minutes which will go through my database mail queue and send the mail using amazon SES.
So far I have successfully created the controller, model, DB and SES is working just fine. The controller sends 10 emails at a time and it all works fine when I manually hit the URL.
I'm not too familiar with cron jobs, but think this is where I need to head.
My application is set up on Elastic beanstalk on AWS.
I think that I need a folder called .ebextensions in my web root, with a file called something.config in it, where I can put some 'container commands'. I also think I will need to include 'leader_only: true' in there somewhere to avoid my replicated instances doing the same jobs.
When I don't understand is what should my container command be, considering controller is 'http://myapplication/process_mail' ? From examples I've seen I couldn't see how it determines the frequency, or even the code that 'calls' the URL.
In my controller, I previously had the following code to ensure it could only be called from the command line. Is this something I can keep and have or will the container command just hit the URL like any other user?
if (!$this->input->is_cli_request()) {
echo "Access Denied";
return;
}
Thanks in advance for any help at all. I think i just need help with what should go in the config file, but then again I may have gone down completely the wrong path altogether!
UPDATE:
So far I've got as far as this:
I believe i need to run the application from the commandline like this http://ellislab.com/codeigniter/user-guide/general/cli.html
so my command would be php index.php process_mail
So what I actually need is help with running this command evey 5 minutes. This is what I have so far:
container_commands:
send_mail:
command: php index.php process_mail
leader_only: true
But what I don't understand is how I get this to run every 5 minutes, rather than just when the instance is set up. Do I need to create a cron job file on instance creation, with the php command in it instead?
Update 2:
To anyone else with the same problem, i got this sorted in the end like this:
an ebextensions file that looks like this: (.ebextensions/mail_queue.config)
container_commands:
01_send_mail:
command: "cat .ebextensions/process_mail.txt > /etc/cron.d/process_mail && chmod 644 /etc/cron.d/process_mail"
leader_only: true
a file called process_mail.txt in the same folder that looks like this:
# The newline at the end of this file is extremely important. Cron won't run without it.
*/5 * * * * root /usr/bin/php /var/app/current/index.php process_mail > /dev/null
So, every 5 minutes it runs via the cmd line the codeigniter main index file, passing in the controller name.
thanks to this: https://stackoverflow.com/a/15233848/2604392
I would set up the cron job to talk to the url, then store result in a MySQL database. Then regular PHP or any other app can connect to MySQL and access the data. That's the suggested way to connect to Twitter since a few months, so you can find info on how to do this floowing search for Twitter connectivity.
Hope this helps
By the way, while writing an email generating PHP script, I noticed that I have to slow down the pace of email sending to avoid being flagged as spammer. I added a delay of 2 seconds between emails and it did the job. My database was only 2500 so no big deal (except taking care of changing the PHP_MAXEXECUTION time variable)...