ここで作成するプロジェクトのソースファイルは GitHub で公開しています.
Webシステムで,ユーザが何らかのボタンをクリックしたときに,メールが送信されたり,データベースにレコードが登録されたりするときの処理にある程度の時間を要することがある.このような場合,ボタンを押してから次の画面に遷移するまで時間を要することになり,ユーザ目線での使い勝手が悪くなる.このような場合にキューを用いると,ボタンを押してすぐに次の画面に遷移して,バックグラウンドで処理を続けることができる.例えば,ユーザ登録でのメール送信などで実際にキューが使われている.
ここではデータベースへのレコード登録にキューを用いるシステムを開発してみよう.
プロジェクトを作成する.すでにあるプロジェクトに機能を追加する場合は以下の作業は不要です.
[vagrant@localhost Documents]$ pwd ⏎
/home/vagrant/Documents
[vagrant@localhost Documents]$ cd laravel/ ⏎
[vagrant@localhost laravel]$ ls ⏎
composer.phar
[vagrant@localhost laravel]$ php composer.phar create-project --prefer-dist laravel/laravel laravelQueue ⏎
Installing laravel/laravel (v5.8.35)
- Installing laravel/laravel (v5.8.35): Loading from cache
Created project in laravelQueue
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)
...(中略)...
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: beyondcode/laravel-dump-server
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
> @php artisan key:generate --ansi
Application key set successfully.
[vagrant@localhost laravel]$
Git の初期設定をしているのであれば,Git でコミットしておく.
[vagrant@localhost laravel]$ cd laravelQueue/ ⏎
[vagrant@localhost laravelQueue]$ ls ⏎
app bootstrap composer.lock database phpunit.xml readme.md routes storage vendor
artisan composer.json config package.json public resources server.php tests webpack.mix.js
[vagrant@localhost laravelQueue]$ git init ⏎
Initialized empty Git repository in /home/vagrant/Documents/laravel/laravelQueue/.git/
[vagrant@localhost laravelQueue]$ git add . ⏎
[vagrant@localhost laravelQueue]$ git commit -m'Initial commit' ⏎
[master (root-commit) 0b7b51f] Initial commit
89 files changed, 8128 insertions(+)
...(中略)...
[vagrant@localhost laravelQueue]$ git log ⏎
commit 0b7b51fc4669109ce990ee2fcab7f3022cdb0288
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date: Mon Feb 24 17:29:45 2020 +0900
Initial commit
[vagrant@localhost laravelQueue]$
.env ファイルを修正する.DB_CONNECTIONをsqliteに変更し,DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD の行を削除するか,先頭に # を付けてコメントアウトする.
.env (抜粋)
DB_CONNECTION=sqlite
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=homestead
# DB_USERNAME=homestead
# DB_PASSWORD=secret
プロジェクトの設定ファイル config/app.php を編集し,タイムゾーンをAsia/Tokyoに,言語をjaに変更する.
config/app.php (抜粋)
/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/
'timezone' => 'Asia/Tokyo',
/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/
'locale' => 'ja',
Sqlite用に空のデータベースファイル database/database.sqlite を作成する.
[vagrant@localhost laravelQueue]$ ls ⏎ app bootstrap composer.lock database phpunit.xml readme.md routes storage vendor artisan composer.json config package.json public resources server.php tests webpack.mix.js [vagrant@localhost laravelQueue]$ cd database/ ⏎ [vagrant@localhost database]$ ls ⏎ factories migrations seeds [vagrant@localhost database]$ touch database.sqlite ⏎ [vagrant@localhost database]$ ls ⏎ database.sqlite factories migrations seeds [vagrant@localhost database]$ cd .. ⏎ [vagrant@localhost laravelQueue]$