Appearance
同期送信 vs Queue ― なぜ直接送らないのか
この章のキーワード
| 用語 | 意味 | なぜ重要か |
|---|---|---|
| 同期処理 | 呼び出し元が処理完了を待つ方式 | 処理が遅いとレスポンスも遅い |
| Queue(キュー) | 処理を後で実行するために積む仕組み | レスポンスと処理を分離 |
| ShouldQueue | Laravel の Job を非同期化するインターフェース | これを実装すると Queue に積まれる |
| Worker | Queue からジョブを取り出して実行するプロセス | 常駐して処理を消化する |
Phase 1: 観察
trade-off Queue 化はされているが、障害設計に改善余地があります。
LIBOT の LINE メッセージ送信ジョブ:
php
// SendMessage.php (excerpt)
// Label: trade-off — Queue化はされているが $tries / failed() がない
class SendMessage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
// ※ $tries, $timeout, $backoff の明示的な定義がない
public function handle(): void
{
$message = $this->message;
if ($message->status !== Message::STATUS_SENDING) {
return; // 送信中でなければスキップ
}
try {
$line = new Libot($message->bot);
$line->pushMultiMessage(
$message->friend->friend_id,
$message->messages_array
);
$message->update([
'status' => Message::STATUS_SENT,
'sent_at' => now(),
]);
} catch (\Exception $e) {
$message->update([
'status' => Message::STATUS_FAILED,
'error_message' => $e->getMessage(),
]);
}
}
// ※ failed() メソッドが定義されていない
}Chapter 4.2 で見た ProcessExternalEventJob と比べてみましょう。
| 項目 | SendMessage | ProcessExternalEventJob |
|---|---|---|
$tries | なし(デフォルト1) | 3 |
$timeout | なし | 120 |
failed() | なし | なし(ただし例外再throw) |
| ステータス管理 | あり | あり |
| 例外の再throw | なし(握りつぶし) | あり |
Phase 2: 判断
AI禁止ゾーン
- もし SendMessage が Queue ではなく同期実行 だったら、何が起きますか?
- SendMessage は Queue 化されていますが、まだ足りないもの は何ですか?
- 例外を catch して握りつぶす(再 throw しない)と、何が失われますか?
AIに聞く前に、自分の頭で考えてみましょう。
テキストを入力すると有効になります