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 Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

如何在CodeIgniter中實現自訂中間件引言:在現代的Web開發中,中間件在應用程式中起著至關重要的作用。它們可以用來執行在請求到達控制器之前或之後執行一些共享的處理邏輯。 CodeIgniter作為一個流行的PHP框架,也支持中間件的使用。本文將介紹如何在CodeIgniter中實作自訂中間件,並提供一個簡單的程式碼範例。中間件概述:中間件是一種在請求

CodeIgniter中間件:加速應用程式的反應速度和頁面渲染概述:隨著網頁應用程式的複雜性和互動性不斷增長,開發人員需要使用更有效率和可擴展的解決方案來提高應用程式的效能和反應速度。 CodeIgniter(CI)是一種基於PHP的輕量級框架,提供了許多有用的功能,其中之一就是中間件。中間件是在請求到達控制器之前或之後執行的一系列任務。這篇文章將介紹如何使用

表單驗證是Web應用程式開發中非常重要的環節,它能夠在提交表單資料之前對資料進行有效性檢查,避免應用程式出現安全漏洞和資料錯誤。使用Golang可以輕鬆實現網頁應用程式的表單驗證,本文將介紹如何使用Golang來實作網頁應用程式的表單驗證。一、表單驗證的基本要素在介紹如何實作表單驗證之前,我們需要知道表單驗證的基本要素是什麼。表單元素:表單元素是指

Web標準是一組由W3C和其他相關組織制定的規範和指南,它包括HTML、CSS、JavaScript、DOM、Web可訪問性和性能優化等方面的標準化,透過遵循這些標準,可以提高頁面的兼容性、可訪問性、可維護性和效能。 Web標準的目標是使Web內容能夠在不同的平台、瀏覽器和裝置上一致地展示和交互,提供更好的使用者體驗和開發效率。

在CodeIgniter框架中使用資料庫查詢建構器(QueryBuilder)的方法引言:CodeIgniter是一個輕量級的PHP框架,它提供了許多功能強大的工具和函式庫,方便開發人員進行Web應用程式開發。其中一個令人印象深刻的功能是資料庫查詢建構器(QueryBuilder),它提供了一種簡潔而強大的方法來建立和執行資料庫查詢語句。本文將介紹如何在Co

Cockpit是一個面向Linux伺服器的基於Web的圖形介面。它主要是為了使新用戶/專家用戶更容易管理Linux伺服器。在本文中,我們將討論Cockpit存取模式以及如何從CockpitWebUI切換Cockpit的管理存取。內容主題:駕駛艙進入模式查找當前駕駛艙訪問模式從CockpitWebUI啟用Cockpit的管理訪問從CockpitWebUI禁用Cockpit的管理訪問結論駕駛艙進入模式駕駛艙有兩種訪問模式:受限訪問:這是駕駛艙的默認訪問模式。在這種存取模式下,您無法從駕駛艙Web用戶

隨著行動互聯網的發展,即時通訊變得越來越重要,越來越普及。對許多企業而言,即時聊天更像是一種通訊服務,提供便利的溝通方式,可以快速有效地解決業務方面的問題。基於此,本文將介紹如何使用PHP框架CodeIgniter開發一個即時聊天應用程式。了解CodeIgniter框架CodeIgniter是一個輕量級的PHP框架,提供了一系列的簡單的工具和函式庫,幫助開發者快速

CodeIgniter中間件:提供安全的檔案上傳和下載功能引言:在網路應用程式開發過程中,檔案上傳和下載是非常常見的功能。然而,對於安全性的考慮,處理文件上傳和下載通常需要額外的安全措施。 CodeIgniter是一個流行的PHP框架,提供了豐富的工具和函式庫來支援開發者建立安全可靠的網路應用程式。本文將介紹如何使用CodeIgniter中介軟體來實現安全的文件
