hi i created a laravel queue job to send mails
public function handle() {
foreach($this->emails as $value) {
$to = $value->email;
$subject = $this->data['subject'];
$this->data['t_firstname'] = $value->firstname;
$this->data['t_lastname'] = $value->lastname;
if (view()->exists('mail.requirement_to_tutor')) {
$view = view('mail.requirement_to_tutor',$this->data);
$html = $view->render();
}
file_put_contents('test.txt', 'test database');
$body = $html;
$headers = "From: " . $this->data['from'] . "\r\nReply-To: " . $this->data['from'] . "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset: utf8\r\n";
mail($to, $subject, $body, $headers);
}
}
and also i am pushing datas from repo
$obj = (new SendStudentRequirement($TutorsbyCity,$data));
$this->dispatch($obj);
but it doesnot run as background , the function is waiting untill the queue finish , help me out please
By default the sync driver is used. You should change this to another driver that is listed in
config/queue.php
Look for the following line in your
.env
file and adjust to a different driver:Laravel - Docs - Queues
You are using a default configuration of
"sync"
, that means that all queue jobs will run synchronously instead of "fire and forget" way. To change this default behavior you can follow these steps:1. Select a diferent Queue Connection
Open the .env configuration file and add QUEUE_DRIVER parameter with one of the supported values: "database", "beanstalkd", "sqs" or "redis". In this case we are going to use a database connection as an example mode:
QUEUE_DRIVER=database
2. Edit connection driver
Open the /config/queue.php file and configure your driver connection, for example:
Then run the next commands to create the new
jobs
table:So, you already have the queue as "fire and forget" way; you can try and see how the
jobs
table was created with the information of the queue.3. Configure a process manager for a long-running
To execute the pending queues in the future we can use a process manager as supervisor. You can install supervisor for Ubuntu and Debian running the following command:
sudo apt-get install supervisor
Then open the supervisor file:
sudo nano /etc/supervisor/supervisord.conf
and add a line like the following to the end of the file:Replace the values of:
QUEUE_NAME
andMY_PROJECT
. Note that the--sleep
parameter is the time (seconds) to verify for new queues in the database. You can see more details of the configuration file in the official documentation.Finally execute these commands to enable the program:
sudo supervisorctl reread && sudo supervisorctl update
You can check the status of the queues in the configured log file: /var/www/MY_PROJECT/storage/logs/worker.log