Use DJJob in CodeIgniter
In a web application, time consuming processes, like sending emails, you need make it asynchronous. In this blog, we will introduce how to make a job running in the background using DJJob and crontab or CLI. In the Rails world, you have ma
In a web application, time consuming processes, like sending emails, you need make it asynchronous. In this blog, we will introduce how to make a job running in the background using DJJob and crontab or CLI.
In the Rails world, you have many choices: Delayed Job, Resque, Sidekiq. Even in Rails 4.2, it introduced Active Job.
If you are using PHP, it may be inconvenient.But you also have some choice, for example DJJob, through the homepage, it’s a:
DJJob allows PHP web applications to process long-running tasks asynchronously. It is a PHP port of delayed_job (developed at Shopify), which has been used in production at SeatGeek since April 2010.
Like delayed_job, DJJob uses a jobs table for persisting and tracking pending, in-progress, and failed jobs.
Here we will write a simple exapmle, to use DJJob to send email in background.
At first your need to install DJJob by copy the PHP files and create tables.Here we will skip these steps. Let’s start by writing business code directly.
A job class is needed, it’s a normaly PHP class, you don’t need to extend any classes, or keep to any rules(all you need is to write a method named `perform`).
to = $to; $this->subject = $subject; $this->body = $body; } public function perform() { $this->_CI = &get_instance(); $this->_CI->config->load('mail'); $this->config = $this->_CI->config->item('mail_server'); $config['protocol'] = 'smtp'; $config['smtp_host'] = $this->config['smtp_host']; $config['smtp_port'] = $this->config['smtp_port']; $config['smtp_user'] = $this->config['smtp_user']; $config['smtp_pass'] = $this->config['smtp_pass']; $config['charset'] = 'utf-8'; $config['wordwrap'] = TRUE; $config['newline'] = "\r\n"; $config['crlf'] = "\r\n"; $config['mailtype'] = 'html'; $this->_CI->email->clear(); $this->_CI->email->initialize($config); $this->_CI->email->from($from_email, 'no-reply'); $this->_CI->email->to($to); $this->_CI->email->subject($subject); $this->_CI->email->message($body); return $this->_CI->email->send(); } }
Notcie:
job class’s __construct?should as simply as possible, it’s will affect the size of serialized class.
To use this job class, you will insert these code in your source:
DJJob::configure(['driver'=> 'mysql','host'=> $db->hostname, 'dbname'=> $db->database, 'user'=> $db->username, 'password'=> $db->password]); $mj = new Mail_job($to, $subject, $body, $mailtype); DJJob::enqueue($mj, "email");
These code first do the database configuration, and then serialize the Mail_job?instance into database use DJJob::enqueue($mj, “email”); ?and the queue name is email(the sencond parameter).
Now we have saved a job into database, next let’s learn how to execute the job.
Our job executer will run in an console but not from a web browser, CodeIgniter support this use case by running Controller from CLI, for more infomation you can checkout the offical document?here.
We will create a Controller too, let’s name it to Queue_job?:
class Queue_job extends CI_Controller { function __construct(){ parent::__construct(); // this controller can only be called from the command line if (!$this->input->is_cli_request()) show_error('Direct access is not allowed'); } function send_mail(){ $db = $this->db; DJJob::configure(['driver'=> 'mysql','host'=> $db->hostname, 'dbname'=> $db->database, 'user'=> $db->username, 'password'=> $db->password]); $worker = new \DJWorker(['queue' => 'email']); $worker->start(); } }
The main method is send_mail, this will first configurate the database an run a DJWorker. The DJWorker?will pickup jobs from database and run them, ?mark the job status, and lastly remove them from database if the job completed?successfully.
If you have finished the Controller, then you can call the Controller from CLI like this:
$ cd /path/to/project; $ php index.php Queue_job send_mail
or make a cron job like:
0 10 * * * /path/to/php /path/to/project/index.php Queue_job send_mail
Conclusion
Though it’s not as easy as in Rails, but it’s realy simple to run jobs that kicked off from web browser by end-user and running in background asynchronous.
Notice:
If you job class with complex types, you will pay attention to the include path somewhere if you got an error.
Reference:
1. How to Run Cron Jobs in CodeIgniter
2. Writing Cron Jobs and Command Line Scripts in CodeIgniter
原文地址:Use DJJob in CodeIgniter, 感谢原作者分享。