本記事では、CakePHP4でQueuePluginのインストール手順と、サンプルプログラムを解説しています。
CakePHP4で利用できるQueueプラグインはいくつかありますが、
本記事では、「dereuromark/cakephp-queue」を利用した方法となります。
cakephp-queueのインストール
1 |
composer require dereuromark/cakephp-queue |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[hogehoge@svxxx]$ composer require dereuromark/cakephp-queue Warning from https://repo.packagist.org: You are using an outdated version of Composer. Composer 2.0 is about to be released and the older 1.x releases will self-update directly to it once it is released. To avoid surprises update now to the latest 1.x version which will prompt you before self-updating to 2.x. Using version ^5.1 for dereuromark/cakephp-queue ./composer.json has been updated Loading composer repositories with package information Warning from https://repo.packagist.org: You are using an outdated version of Composer. Composer 2.0 is about to be released and the older 1.x releases will self-update directly to it once it is released. To avoid surprises update now to the latest 1.x version which will prompt you before self-updating to 2.x. Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing dereuromark/cakephp-queue (5.1.0): Downloading (100%) dereuromark/cakephp-queue suggests installing friendsofcake/search (For admin backend and filtering of current jobs.) dereuromark/cakephp-queue suggests installing dereuromark/cakephp-tools (For the QueueEmailTask (if you don't write your own task here). Also for admin backend.) dereuromark/cakephp-queue suggests installing dereuromark/cakephp-ide-helper (For maximum IDE support, especially around createJob() usage.) Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested. Writing lock file Generating autoload files > Cake\Composer\Installer\PluginInstaller::postAutoloadDump |
src/Application.phpの編集
src/Application.phpに、下記のプラグイン追加を記述します。
1 |
$this->addPlugin('Queue'); |
migrationsの実行
1 |
[hogehoge@svxxx]$ bin/cake migrations migrate -p Queue |
下記のエラーが発生する場合には、Timezone定義を修正します。
1 |
Notice: date_default_timezone_set(): Timezone ID '+09:00' is invalid in /home/hogehoge/public_html/tools/config/bootstrap.php on line 107 |
1 2 3 |
'defaultTimezone' => env('APP_DEFAULT_TIMEZONE', '+09:00'), → 'defaultTimezone' => env('APP_DEFAULT_TIMEZONE', 'Japan'), |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
using migration paths - /home/hogehoge/public_html/tools/vendor/dereuromark/cakephp-queue/config/Migrations using seed paths - /home/hogehoge/public_html/tools/vendor/dereuromark/cakephp-queue/config/Seeds using environment default using adapter mysql using database hogehoge_db ordering by creation time == 20150425180802 Init: migrating == 20150425180802 Init: migrated 0.0033s == 20150511062806 Fixmissing: migrating == 20150511062806 Fixmissing: migrated 0.0079s == 20150911132343 ImprovementsForMysql: migrating == 20150911132343 ImprovementsForMysql: migrated 0.0057s == 20161319000000 IncreaseDataSize: migrating == 20161319000000 IncreaseDataSize: migrated 0.0005s == 20161319000001 Priority: migrating == 20161319000001 Priority: migrated 0.0054s == 20161319000002 Rename: migrating == 20161319000002 Rename: migrated 0.0047s == 20161319000003 Processes: migrating == 20161319000003 Processes: migrated 0.0038s == 20171013131845 AlterQueuedJobs: migrating == 20171013131845 AlterQueuedJobs: migrated 0.0007s == 20171013133145 Utf8mb4Fix: migrating == 20171013133145 Utf8mb4Fix: migrated 0.0112s == 20171019083500 ColumnLength: migrating == 20171019083500 ColumnLength: migrated 0.0058s == 20171019083501 MigrationQueueNull: migrating == 20171019083501 MigrationQueueNull: migrated 0.0116s == 20171019083502 MigrationQueueStatus: migrating == 20171019083502 MigrationQueueStatus: migrated 0.0098s == 20171019083503 MigrationQueueProcesses: migrating == 20171019083503 MigrationQueueProcesses: migrated 0.0061s == 20171019083505 MigrationQueueProcessesIndex: migrating == 20171019083505 MigrationQueueProcessesIndex: migrated 0.0052s == 20171019083506 MigrationQueueProcessesKey: migrating == 20171019083506 MigrationQueueProcessesKey: migrated 0.0078s All Done. Took 0.0930s Dumps the current schema of the database to be used while baking a diff using migration paths - /home/hogehoge/public_html/tools/vendor/dereuromark/cakephp-queue/config/Migrations using seed paths - /home/hogehoge/public_html/tools/vendor/dereuromark/cakephp-queue/config/Seeds Writing dump file `/home/hogehoge/public_html/tools/vendor/dereuromark/cakephp-queue/config/Migrations/schema-dump-default.lock`... Dump file `/home/hogehoge/public_html/tools/vendor/dereuromark/cakephp-queue/config/Migrations/schema-dump-default.lock` was successfully written [hogehoge@svxxx]$ |
サンプルプログラム
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace App\Shell\Task ; use Queue\Shell\Task; use Queue\Shell\Task\QueueTask; class QueueSampleTask extends QueueTask { /** * @var int */ public $timeout = 20; /** * @var int */ public $retries = 1; /** * @param array $data The array passed to QueuedJobsTable::createJob() * @param int $jobId The id of the QueuedJob entity * @return void */ public function run(array $data, int $jobId): void { $this->out ( 'call run !!' ); return; } } |
runworker実行
1 |
bin/cake queue runworker |
1 2 3 4 5 6 7 8 9 10 11 |
[hogehoge@svxxx]$ ./bin/cake queue runworker [2020-10-25 23:00:00] Looking for Job ... Running Job of type "Sample" call run !! Job Finished. --------------------------------------------------------------- [2020-10-25 23:00:10] Looking for Job ... nothing to do, sleeping. --------------------------------------------------------------- [2020-10-25 23:00:20] Looking for Job ... nothing to do, sleeping. |