ホームページ php教程 php手册 Use DJJob in CodeIgniter

Use DJJob in CodeIgniter

Jun 06, 2016 pm 08:13 PM
codeigniter use web

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

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

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

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

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

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

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

CodeIgniter でカスタムミドルウェアを実装する方法 CodeIgniter でカスタムミドルウェアを実装する方法 Jul 29, 2023 am 10:53 AM

CodeIgniter でカスタムミドルウェアを実装する方法

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

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

Golang を使用して Web アプリケーションのフォーム検証を実装する方法 Golang を使用して Web アプリケーションのフォーム検証を実装する方法 Jun 24, 2023 am 09:08 AM

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

CodeIgniter フレームワークでデータベース クエリ ビルダー (Query Builder) を使用する方法 CodeIgniter フレームワークでデータベース クエリ ビルダー (Query Builder) を使用する方法 Jul 28, 2023 pm 11:13 PM

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

コックピット Web UI から管理アクセスを有効にする方法 コックピット Web UI から管理アクセスを有効にする方法 Mar 20, 2024 pm 06:56 PM

コックピット Web UI から管理アクセスを有効にする方法

PHP は Web 開発におけるフロントエンドですか、それともバックエンドですか? PHP は Web 開発におけるフロントエンドですか、それともバックエンドですか? Mar 24, 2024 pm 02:18 PM

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

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

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

PHP フレームワーク CodeIgniter を使用してバックエンド管理システムを迅速に構築する方法 PHP フレームワーク CodeIgniter を使用してバックエンド管理システムを迅速に構築する方法 Jun 27, 2023 am 09:46 AM

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

See all articles