Skip to content

Commit 82ae0fc

Browse files
committed
mail送信、batch処理(command)、job/workerのサンプルコードを追加
1 parent a0fbd1b commit 82ae0fc

16 files changed

+224
-107
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// "../compose.production.yaml",
88
"../compose.development.api.yaml",
99
"../compose.development.infra.yaml",
10-
// "../compose.development.batch.yaml",
11-
// "../compose.development.worker.yaml",
10+
"../compose.development.batch.yaml",
11+
"../compose.development.worker.yaml",
1212
"./compose.yaml"
1313
],
1414
"service": "workspace",

compose.development.api.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ services:
7272
# condition: service_healthy
7373
networks:
7474
- common
75-
# healthcheck:
76-
# NOTE: cgi-fcgi リクエストを送ると、zend_mm_heap corrupted が発生する重大な問題があるため、コメントアウト中
75+
healthcheck:
76+
# NOTE: cgi-fcgi リクエストを送ると、zend_mm_heap corrupted が発生する重大な問題がある
7777
# 参考: https://github.com/php/php-src/issues/15395
7878
# PHP8.2以降の最新パッチバージョンでは修正されているが、PHP8.1系はまだ対応されていないっぽい。
7979
# 参考: https://www.php.net/ChangeLog-8.php#PHP_8_1
80-
# test: [ "CMD-SHELL", "cgi-fcgi -bind -connect /run/php-fpm/www.sock 2>&1 || exit 1" ]
81-
# interval: 30s
82-
# timeout: 5s
83-
# start_period: 30s
84-
# retries: 3
80+
test: [ "CMD-SHELL", "cgi-fcgi -bind -connect /run/php-fpm/www.sock 2>&1 || exit 1" ]
81+
interval: 30s
82+
timeout: 5s
83+
start_period: 30s
84+
retries: 3

compose.development.batch.yaml

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
2-
example-batch-1:
3-
container_name: ${COMPOSE_PROJECT_NAME}_example-batch-1
2+
example-emails-batch:
3+
container_name: ${COMPOSE_PROJECT_NAME}_example-emails-batch
44
build:
55
target: development
66
dockerfile: ./docker/app/php-fpm/Dockerfile
@@ -20,31 +20,6 @@ services:
2020
depends_on:
2121
db:
2222
condition: service_healthy
23-
command: [ "php", "artisan", "example-batch-1" ]
24-
networks:
25-
- common
26-
27-
example-batch-2:
28-
container_name: ${COMPOSE_PROJECT_NAME}_example-batch-2
29-
build:
30-
target: development
31-
dockerfile: ./docker/app/php-fpm/Dockerfile
32-
additional_contexts:
33-
php-fpm-ctx: ./docker/app/php-fpm
34-
args:
35-
PHP_VERSION: ${PHP_VERSION}
36-
env_file:
37-
- ./src/.env
38-
environment:
39-
CONTAINER_ROLE: batch
40-
volumes:
41-
- type: bind
42-
source: ./src
43-
target: /var/www/src
44-
consistency: cached
45-
depends_on:
46-
db:
47-
condition: service_healthy
48-
command: [ "php", "artisan", "example-batch-2" ]
23+
command: [ "php", "artisan", "mail:send" ]
4924
networks:
5025
- common

compose.development.infra.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ services:
5252
profiles:
5353
- nosql
5454

55-
queue:
56-
container_name: ${COMPOSE_PROJECT_NAME}_queue
55+
elasticmq:
56+
container_name: ${COMPOSE_PROJECT_NAME}_elasticmq
5757
image: softwaremill/elasticmq:latest
5858
ports:
5959
- "${HOST_ADDR}9324:9324"

compose.development.worker.yaml

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
2-
example-worker-1:
3-
container_name: ${COMPOSE_PROJECT_NAME}_example-worker-1
2+
example-emails-worker:
3+
container_name: ${COMPOSE_PROJECT_NAME}_example-emails-worker
44
build:
55
target: development
66
dockerfile: ./docker/app/php-fpm/Dockerfile
@@ -12,40 +12,7 @@ services:
1212
- ./src/.env
1313
environment:
1414
CONTAINER_ROLE: worker
15-
SQS_FIFO_QUEUE: "example-worker-1.fifo"
16-
volumes:
17-
- type: bind
18-
source: ./src
19-
target: /var/www/src
20-
consistency: cached
21-
depends_on:
22-
db:
23-
condition: service_healthy
24-
elasticmq:
25-
condition: service_healthy
26-
networks:
27-
- common
28-
healthcheck:
29-
test: [ "CMD-SHELL", "pgrep -f 'php artisan queue:work' || exit 1" ]
30-
interval: 60s
31-
timeout: 5s
32-
start_period: 30s
33-
retries: 3
34-
35-
example-worker-2:
36-
container_name: ${COMPOSE_PROJECT_NAME}_example-worker-2
37-
build:
38-
target: development
39-
dockerfile: ./docker/app/php-fpm/Dockerfile
40-
additional_contexts:
41-
php-fpm-ctx: ./docker/app/php-fpm
42-
args:
43-
PHP_VERSION: ${PHP_VERSION}
44-
env_file:
45-
- ./src/.env
46-
environment:
47-
CONTAINER_ROLE: worker
48-
SQS_FIFO_QUEUE: "example-worker-2.fifo"
15+
SQS_FIFO_QUEUE: "example-emails.fifo"
4916
volumes:
5017
- type: bind
5118
source: ./src

deploy/buildspec.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ phases:
4949
- printf '{"name":"app","imageUri":"%s"},' ${APP_IMAGE_REPO_URI}:${IMAGE_TAG} >> apiImagedefinitions.json
5050

5151
# For Worker Services
52-
- printf '{"name":"example-worker-1","imageUri":"%s"},' ${APP_IMAGE_REPO_URI}:${IMAGE_TAG} >> workerImagedefinitions.json
53-
- printf '{"name":"example-worker-2","imageUri":"%s"},' ${APP_IMAGE_REPO_URI}:${IMAGE_TAG} >> workerImagedefinitions.json
52+
- printf '{"name":"example-emails-worker","imageUri":"%s"},' ${APP_IMAGE_REPO_URI}:${IMAGE_TAG} >> workerImagedefinitions.json
5453

5554
# Close JSON array
5655
- sed -i '$ s/,$//' apiImagedefinitions.json

docker/queue/elasticmq/custom.conf

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,37 +63,20 @@ queues {
6363
fifo = true #FIFOキューの設定
6464
}
6565

66-
# http://localhost:9324/queue/example-worker-1.fifo
67-
"example-worker-1.fifo" {
66+
# http://localhost:9324/queue/example-emails.fifo
67+
"example-emails.fifo" {
6868
defaultVisibilityTimeout = 10 seconds # 可視性タイムアウトの設定
6969
delay = 5 seconds # 遅延キューの設定
7070
receiveMessageWait = 0 seconds # ロングポーリング
7171
fifo = true #FIFOキューの設定
7272
contentBasedDeduplication = true # 重複排除ID
7373
deadLettersQueue {
74-
name = "example-worker-1-dead-letters.fifo"
74+
name = "example-emails-dead-letters.fifo"
7575
maxReceiveCount = 3 # from 1 to 1000
7676
}
7777
}
78-
# http://localhost:9324/queue/example-worker-1-dead-letters.fifo
79-
"example-worker-1-dead-letters.fifo" {
80-
fifo = true #FIFOキューの設定
81-
}
82-
83-
# http://localhost:9324/queue/example-worker-2.fifo
84-
"example-worker-2.fifo" {
85-
defaultVisibilityTimeout = 10 seconds # 可視性タイムアウトの設定
86-
delay = 5 seconds # 遅延キューの設定
87-
receiveMessageWait = 0 seconds # ロングポーリング
88-
fifo = true #FIFOキューの設定
89-
contentBasedDeduplication = true # 重複排除ID
90-
deadLettersQueue {
91-
name = "example-worker-2-dead-letters.fifo"
92-
maxReceiveCount = 3 # from 1 to 1000
93-
}
94-
}
95-
# http://localhost:9324/queue/example-worker-2-dead-letters.fifo
96-
"example-worker-2-dead-letters.fifo" {
78+
# http://localhost:9324/queue/example-emails-dead-letters.fifo
79+
"example-emails-dead-letters.fifo" {
9780
fifo = true #FIFOキューの設定
9881
}
9982
}

src/.env.development.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ FILESYSTEM_DISK=local
6363
# QUEUE_CONNECTION=database
6464
QUEUE_CONNECTION=sqs-fifo
6565

66-
SQS_PREFIX="http://queue:9324/queue"
66+
SQS_PREFIX="http://elasticmq:9324/queue"
6767
SQS_QUEUE="example"
6868
SQS_SUFFIX=-dev
6969

src/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ FILESYSTEM_DISK=local
6363
# QUEUE_CONNECTION=database
6464
QUEUE_CONNECTION=sqs-fifo
6565

66-
SQS_PREFIX="http://queue:9324/queue"
66+
SQS_PREFIX="http://elasticmq:9324/queue"
6767
SQS_QUEUE="example"
6868
SQS_SUFFIX=
6969

70-
SQS_FIFO_PREFIX="http://queue:9324/queue"
70+
SQS_FIFO_PREFIX="http://elasticmq:9324/queue"
7171
# NOTE: 個別のWorkerコンテナ起動時に環境変数で指定すること!!
7272
SQS_FIFO_QUEUE="example.fifo"
7373
SQS_FIFO_SUFFIX=

src/.env.production.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ FILESYSTEM_DISK=local
6363
# QUEUE_CONNECTION=database
6464
QUEUE_CONNECTION=sqs-fifo
6565

66-
SQS_PREFIX="http://queue:9324/queue"
66+
SQS_PREFIX="http://elasticmq:9324/queue"
6767
SQS_QUEUE="example"
6868
SQS_SUFFIX=
6969

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Console\Commands;
6+
7+
use App\Mail\ExampleMail;
8+
use Illuminate\Console\Command;
9+
use Illuminate\Support\Facades\Mail;
10+
11+
class SendEmails extends Command
12+
{
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'mail:send';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Send a mail for testing purposes';
26+
27+
/**
28+
* Execute the console command.
29+
*/
30+
public function handle(): void
31+
{
32+
// 送信先のメールアドレスを設定
33+
34+
35+
// メール送信
36+
Mail::to($to)->send(new ExampleMail(
37+
envelopeSubject: 'テストメール (Command から送信)',
38+
envelopeFromName: 'From Command',
39+
body: 'これは Command から送信されたテストメールです。',
40+
));
41+
42+
$this->info('Test email sent successfully to ' . $to);
43+
}
44+
}

src/app/Jobs/SendEmailsJob.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Jobs;
6+
7+
use App\Mail\ExampleMail;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Foundation\Queue\Queueable;
10+
use Illuminate\Support\Facades\Mail;
11+
12+
class SendEmailsJob implements ShouldQueue
13+
{
14+
use Queueable;
15+
16+
/**
17+
* Create a new job instance.
18+
*/
19+
public function __construct()
20+
{
21+
22+
}
23+
24+
/**
25+
* Execute the job.
26+
*/
27+
public function handle(): void
28+
{
29+
// 送信先のメールアドレスを設定
30+
31+
32+
// バックグラウンドでメール送信
33+
Mail::to($to)->send(new ExampleMail(
34+
envelopeSubject: 'テストメール (Background Job から送信)',
35+
envelopeFromName: 'From Background Job',
36+
body: 'これは Background Job から送信されたテストメールです。',
37+
));
38+
}
39+
}

src/app/Mail/ExampleMail.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Mail;
6+
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Mail\Mailable;
9+
use Illuminate\Mail\Mailables\Address;
10+
use Illuminate\Mail\Mailables\Content;
11+
use Illuminate\Mail\Mailables\Envelope;
12+
use Illuminate\Queue\SerializesModels;
13+
14+
class ExampleMail extends Mailable
15+
{
16+
use Queueable;
17+
use SerializesModels;
18+
19+
/**
20+
* Create a new message instance.
21+
*/
22+
public function __construct(
23+
private string $envelopeSubject,
24+
private string $envelopeFromName,
25+
private string $body
26+
) {
27+
28+
}
29+
30+
/**
31+
* Get the message envelope.
32+
*/
33+
public function envelope(): Envelope
34+
{
35+
return new Envelope(
36+
subject: $this->envelopeSubject,
37+
from: new Address('[email protected]', $this->envelopeFromName),
38+
);
39+
}
40+
41+
/**
42+
* Get the message content definition.
43+
*/
44+
public function content(): Content
45+
{
46+
return new Content(
47+
view: 'mail.example',
48+
with: [
49+
'body' => $this->body,
50+
],
51+
);
52+
}
53+
54+
/**
55+
* Get the attachments for the message.
56+
*
57+
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
58+
*/
59+
public function attachments(): array
60+
{
61+
return [];
62+
}
63+
}

0 commit comments

Comments
 (0)