MeWrite Docs

Laravel queue:work not working on Windows

WindowsでLaravelのqueue:workやqueue:listenが正常に動作しない問題

概要

Windows環境でLaravelのqueue:workqueue:listenコマンドが正常に動作しない問題です。ジョブが処理されない、プロセスがすぐに終了する、シグナルハンドリングの問題などが発生します。

エラーメッセージ

The Process class relies on proc_open, which is not available on your PHP installation.
PHP Fatal error: Call to undefined function pcntl_signal()
queue:work command not processing jobs
Worker stops immediately after starting

原因

  1. pcntl拡張がない: WindowsのPHPにはpcntl拡張がデフォルトで含まれていない
  2. proc_open無効化: セキュリティ設定でproc_openが無効
  3. シグナルハンドリング: Windowsはposixシグナルをサポートしていない
  4. パス区切り文字: Windows/Linuxのパス区切り文字の違い
  5. プロセス管理: Supervisorが使えない

解決策

1. queue:workの代わりにqueue:listenを使用

1
2
3
4
5
# queue:work(推奨されるが、Windowsで問題あり)
php artisan queue:work

# queue:listen(Windowsで動作しやすい)
php artisan queue:listen --timeout=60 --sleep=3 --tries=3

2. 同期ドライバを開発環境で使用

1
2
3
4
5
// .env(開発環境)
QUEUE_CONNECTION=sync

// config/queue.php
'default' => env('QUEUE_CONNECTION', 'sync'),

3. Windowsタスクスケジューラを使用

1
2
3
4
# タスクスケジューラで定期実行
# PowerShellスクリプト: run-queue.ps1
cd C:\path\to\laravel
php artisan queue:work --stop-when-empty --max-jobs=100
# タスクスケジューラの設定
# トリガー: 1分ごとに実行
# アクション: PowerShell -ExecutionPolicy Bypass -File "C:\path\to\run-queue.ps1"

4. NSSM (Non-Sucking Service Manager) を使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# NSSMをインストール
# https://nssm.cc/download からダウンロード

# サービスとして登録
nssm install LaravelWorker

# サービス設定
# Path: C:\php\php.exe
# Startup directory: C:\path\to\laravel
# Arguments: artisan queue:work --sleep=3 --tries=3

5. バッチファイルでループ実行

1
2
3
4
5
@echo off
:loop
php artisan queue:work --stop-when-empty --max-jobs=50
timeout /t 5 /nobreak
goto loop

6. データベースキュードライバの使用

1
2
3
4
5
6
// .env
QUEUE_CONNECTION=database

// マイグレーション作成
php artisan queue:table
php artisan migrate
1
2
3
4
5
6
7
// config/queue.php
'database' => [
    'driver' => 'database',
    'table' => 'jobs',
    'queue' => 'default',
    'retry_after' => 90,
],

7. Windows Subsystem for Linux (WSL) を使用

1
2
3
4
5
6
# WSLをインストール
wsl --install

# WSL内でLaravelを実行
cd /mnt/c/path/to/laravel
php artisan queue:work

8. Redisキューの設定(推奨)

1
2
3
4
5
# Redisをインストール(Windows版またはWSL経由)
# https://github.com/microsoftarchive/redis/releases

# または Memurai(Windows用Redis互換)
# https://www.memurai.com/
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// .env
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

// config/queue.php
'redis' => [
    'driver' => 'redis',
    'connection' => 'default',
    'queue' => env('REDIS_QUEUE', 'default'),
    'retry_after' => 90,
    'block_for' => null,
],

9. Laravel Horizonの代替(Windows)

 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
// app/Console/Commands/WindowsWorker.php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class WindowsWorker extends Command
{
    protected $signature = 'windows:worker {--max-jobs=100}';
    protected $description = 'Windows compatible queue worker';

    public function handle(): void
    {
        while (true) {
            $this->info('Processing queue...');

            Artisan::call('queue:work', [
                '--stop-when-empty' => true,
                '--max-jobs' => $this->option('max-jobs'),
            ]);

            $this->info(Artisan::output());

            sleep(5);
        }
    }
}

10. Docker for Windowsを使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# docker-compose.yml
version: '3'
services:
  app:
    build: .
    volumes:
      - .:/var/www/html
    depends_on:
      - redis

  worker:
    build: .
    command: php artisan queue:work --sleep=3 --tries=3
    volumes:
      - .:/var/www/html
    depends_on:
      - redis

  redis:
    image: redis:alpine
    ports:
      - "6379:6379"

11. ジョブの同期的なディスパッチ(デバッグ用)

1
2
3
4
5
6
// 開発環境でのみ同期的に実行
if (app()->environment('local')) {
    ProcessPodcast::dispatchSync($podcast);
} else {
    ProcessPodcast::dispatch($podcast);
}

デバッグ方法

1
2
3
4
5
6
7
8
# キューの状態確認
php artisan queue:failed

# ジョブを手動で実行(デバッグ)
php artisan queue:work --once

# 詳細ログを出力
php artisan queue:work -vvv
1
2
3
4
5
6
7
// ジョブ内でログ出力
public function handle(): void
{
    \Log::info('Job started', ['job_id' => $this->job->getJobId()]);
    // 処理
    \Log::info('Job completed');
}

よくある間違い

  • Windowsでqueue:workが動かないのをバグと勘違いする
  • pcntl拡張をWindowsにインストールしようとする
  • 本番環境でもsyncドライバを使ってしまう
  • WSLを使わずにSupervisorをインストールしようとする

関連エラー

参考リンク

Laravel の他のエラー

最終更新: 2025-12-14