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`).
<?php Class Mail_job{ private $_CI; private $config; private $to; private $subject; private $body; public function __construct($to, $subject, $body){ $this->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, 感谢原作者分享。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











CodeIgniter ミドルウェア: アプリケーションの応答性とページのレンダリングを高速化します。

Golang を使用して Web アプリケーションのフォーム検証を実装する方法

CodeIgniter フレームワークでデータベース クエリ ビルダー (Query Builder) を使用する方法

PHP は Web 開発におけるフロントエンドですか、それともバックエンドですか?

CodeIgniter ミドルウェア: 安全なファイルのアップロードおよびダウンロード機能を提供します。

PHP フレームワーク CodeIgniter を使用してバックエンド管理システムを迅速に構築する方法
