{"id":7068,"date":"2022-11-30T15:16:37","date_gmt":"2022-11-30T06:16:37","guid":{"rendered":"https:\/\/www.sria.co.jp\/blog\/?p=7068"},"modified":"2022-12-01T16:52:41","modified_gmt":"2022-12-01T07:52:41","slug":"laravel-job-queue","status":"publish","type":"post","link":"https:\/\/www.sria.co.jp\/blog\/2022\/11\/laravel-job-queue\/","title":{"rendered":"Laravel\u306eJOB\u3068QUEUE\u3092\u4f7f\u7528\u3057\u3066\u975e\u540c\u671f\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u3066\u307f\u305f\u3002"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002\u9060\u85e4\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306fLaravel\u306eJOB\u3068QUEUE\u3092\u4f7f\u7528\u3057\u3066\u975e\u540c\u671f\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u3066\u307f\u305f\u306e\u3067\u3001\u3064\u307e\u305a\u3044\u305f\u70b9\u306b\u3082\u89e6\u308c\u306a\u304c\u3089\u5b9f\u88c5\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u4eca\u56de\u5b9f\u88c5\u3057\u305f\u975e\u540c\u671f\u51e6\u7406\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>A\uff1a\u65b0\u898f\u4f1a\u54e1\u5165\u4f1a \u2192 B\uff1a\u4f1a\u54e1\u767b\u9332\u51e6\u7406\u5b9f\u65bd\uff08 \u2192 C\uff1a\u975e\u540c\u671f\u3067\u95a2\u9023\u3059\u308b\u4f1a\u54e1\u306eDB\u60c5\u5831\u66f4\u65b0\uff09 \u2192 \u51e6\u7406\u7d42\u4e86<\/p>\n<p>C\u306e\u51e6\u7406\u304c\u91cd\u3044\u51e6\u7406\u3060\u3063\u305f\u305f\u3081\u3001\u975e\u540c\u671f\u3067\u306e\u5b9f\u88c5\u3092\u8a66\u307f\u307e\u3057\u305f\u3002<\/p>\n<ol>\n<li>JOB\u3068QUEUE\u306b\u3064\u3044\u3066<\/li>\n<li>.env\u306e\u8a2d\u5b9a<\/li>\n<li>DB\u306bQUEUE\u7528\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210<\/li>\n<li>JOB\u30af\u30e9\u30b9\u306e\u4f5c\u6210<\/li>\n<li>QUEUE\u3078\u683c\u7d0d<\/li>\n<li>Worker\u306b\u3088\u308bJOB\u306e\u5b9f\u884c<\/li>\n<li><span>Supervisor\u306b\u3088\u308bWorker\u306e\u7ba1\u7406<\/span><\/li>\n<\/ol>\n<h2>JOB\u3068QUEUE\u306b\u3064\u3044\u3066<\/h2>\n<p>Laravel\u306b\u304a\u3044\u3066JOB\u3068\u306f\u51e6\u7406\u305d\u306e\u3082\u306e\u306e\u3053\u3068\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u4f1a\u54e1\u7ba1\u7406\u30b7\u30b9\u30c6\u30e0\u306b\u304a\u3044\u3066\u4f1a\u54e1\u306eDB\u60c5\u5831\u3092\u66f4\u65b0\u3059\u308b\u3059\u308b\u51e6\u7406\u306e\u3053\u3068\u3092JOB\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n<p>Laravel\u306b\u304a\u3044\u3066QUEUE\u3068\u306fJOB\u3092\u305f\u3081\u8fbc\u3080\u7bb1\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002\u6700\u521d\u306b\u7bb1\u306b\u5165\u3063\u305fJOB\u304b\u3089\u9806\u756a\u306b\u51e6\u7406\u3057\u3066\u3044\u304f\u4ed5\u7d44\u307f\u3067\u3059\u3002\u3053\u306eQUEUE\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067JOB\u3092\u975e\u540c\u671f\u3067\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002QUEUE\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7b49\u3092\u7528\u3044\u3066\u7528\u610f\u3057\u307e\u3059\u3002\u4eca\u56de\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092QUEUE\u3068\u3057\u3066\u4f7f\u7528\u3057\u305f\u306e\u3067\u305d\u306e\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<h2>.env\u306e\u8a2d\u5b9a<\/h2>\n<p>.env\u30d5\u30a1\u30a4\u30eb\u306e<span>QUEUE_CONNECTION\u5909\u6570\u306e\u8a2d\u5b9a\u3092\u4ee5\u4e0b\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/span><\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>QUEUE_CONNECTION=database<\/code><\/pre>\n<\/div>\n<p>\u6700\u521d\u3053\u306e\u8a2d\u5b9a\u3092\u5fd8\u308c\u3066\u304a\u308a\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u4ee5\u4e0b\u306e\u8a2d\u5b9a\u306b\u306a\u3063\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>QUEUE_CONNECTION=sync<\/code><\/pre>\n<\/div>\n<p>sync\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u683c\u7d0d\u3055\u308c\u307e\u305b\u3093\u3002\u305d\u306e\u305f\u3081\u975e\u540c\u671f\u5b9f\u884c\u3067\u306f\u306a\u304f\u540c\u671f\u5b9f\u884c\u3068\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001.env\u30d5\u30a1\u30a4\u30eb\u306eQUEUE_DRIVER<span>\u5909\u6570\u306e\u8a2d\u5b9a\u3082\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/span><\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>QUEUE_DRIVER=database<\/code><\/pre>\n<\/div>\n<p>\u4ee5\u4e0a\u3067.env\u306e\u8a2d\u5b9a\u306f\u7d42\u4e86\u3067\u3059\u3002<\/p>\n<p>\u203b \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u8a2d\u5b9a\u306f\u3067\u304d\u3066\u3044\u308b\u524d\u63d0\u3067\u3059\u3002<\/p>\n<h2>DB\u306bQUEUE\u7528\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210<\/h2>\n<p>JOB\u3092\u305f\u3081\u8fbc\u3080\u305f\u3081\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u30c6\u30fc\u30d6\u30eb\u306f\u4ee5\u4e0b\u306eartisan\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u81ea\u52d5\u3067\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>php artisan queue:table\nphp artisan migrate<\/code><\/pre>\n<\/div>\n<p>\u4ee5\u4e0b\u306f\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u8eab\u3067\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code><?php \nuse Illuminate\\Database\\Migrations\\Migration; \nuse Illuminate\\Database\\Schema\\Blueprint; \nuse Illuminate\\Support\\Facades\\Schema; \n\nclass CreateJobsTable extends Migration { \n\/**\n * Run the migrations. \n * \n * @return void\n *\/ \n  public function up() { \n    Schema::create('jobs', function (Blueprint $table) { \n      $table->bigIncrements('id');\n      $table->string('queue')->index();\n      $table->longText('payload');\n      $table->unsignedTinyInteger('attempts');\n      $table->unsignedInteger('reserved_at')->nullable();\n      $table->unsignedInteger('available_at');\n      $table->unsignedInteger('created_at');\n    });\n  }\n\n\n\/**\n * Reverse the migrations.\n *\n * @return void\n *\/\n  public function down()\n  {\n    Schema::dropIfExists('jobs');\n  }\n}\n<\/code><\/pre>\n<\/div>\n<p>migration\u3092\u4f7f\u3063\u3066\u3044\u306a\u3044\u3068\u3044\u3046\u65b9\u306f\u4ee5\u4e0b\u306eSQL\u3092\u3054\u4f7f\u7528\u304f\u3060\u3055\u3044\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>CREATE TABLE `jobs` (\n`id` bigint unsigned NOT NULL AUTO_INCREMENT,\n`queue` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,\n`payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,\n`attempts` tinyint unsigned NOT NULL,\n`reserved_at` int unsigned DEFAULT NULL,\n`available_at` int unsigned NOT NULL,\n`created_at` int unsigned NOT NULL,\nPRIMARY KEY (`id`),\nKEY `jobs_queue_index` (`queue`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;<\/code><\/pre>\n<\/div>\n<h2>JOB\u30af\u30e9\u30b9\u306e\u4f5c\u6210<\/h2>\n<p>JOB\u30af\u30e9\u30b9\u3092app\/jobs\u306b\u4f5c\u6210\u3057\u305f\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306eartisan\u30b3\u30de\u30f3\u30c9\u3067\u81ea\u52d5\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>php artisan make:job UpdateMemberInfoJob<\/p>\n<p>\u4ee5\u4e0bapp\/jobs\/UpdateMemberInfoJob.php\u306e\u4e2d\u8eab\u3067\u3059\u3002<\/p>\n<p>handle\u306e\u4e2d\u306b\u51e6\u7406\u3092\u8a18\u8ff0\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code><?php \nnamespace App\\Jobs; \n\nuse Illuminate\\Bus\\Queueable; \nuse Illuminate\\Contracts\\Queue\\ShouldBeUnique; \nuse Illuminate\\Contracts\\Queue\\ShouldQueue; \nuse Illuminate\\Foundation\\Bus\\Dispatchable; \nuse Illuminate\\Queue\\InteractsWithQueue; \nuse Illuminate\\Queue\\SerializesModels; \n\n\/\/ \u4f7f\u7528\u3059\u308b\u51e6\u7406\u3092\u66f8\u3044\u305f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u547c\u3076\nuse App\\Http\\Controllers\\MemberController; \n\nclass UpdateMemberInfoJob implements ShouldQueue { \n\n  use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; \n  \/**\n   * Create a new job instance.\n   *\n   * @return void\n   *\/ \n  public function __construct() { \n    \/\/\n  } \n\n  \/**\n   * Execute the job.\n   * \n   * @return void\n   *\/ \n  public function handle() { \n    \n    \/\/ \u975e\u540c\u671f\u3067\u5b9f\u884c\u3059\u308b\u51e6\u7406\uff08JOB\uff09\u3092\u8a18\u8ff0\n    $memberController = new MemberController; \n    $memberController->updateMemberInfo();\n  }\n}\n<\/code><\/pre>\n<\/div>\n<h2>QUEUE\u3078\u683c\u7d0d<\/h2>\n<p>JOB\u30af\u30e9\u30b9\u304c\u3067\u304d\u305f\u3089\u3001dispatch\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3067\u3001QUEUE\u3078JOB\u3092\u683c\u7d0d\u3057\u307e\u3059\u3002<\/p>\n<p>JOB\u306f\u975e\u540c\u671f\u5b9f\u884c\u3055\u308c\u308b\u305f\u3081\u3001\u51e6\u7406A\u306e\u5f8c\u3001\u3059\u3050\u306b\u51e6\u7406B\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code><?php\n\nnamespace App\\Http\\Controllers;\n\nuse Illuminate\\Http\\Request;\n\/\/ JOB\u3092\u5b9f\u884c\u3057\u305f\u3044\u7b87\u6240\u306b\u8ffd\u8a18\u3059\u308b\nuse App\\jobs\\UpdateMemberInfoJob;\n\nclass EntryController extends Controller\n{\n    public function entry(Request $request)\n    {\n        \/\/ \u51e6\u7406A\uff08\u7701\u7565\uff09\n        \/\/ JOB\u3092QUEUE\u306b\u683c\u7d0d\n        UpdateMemberInfoJob::dispatch();\n        \n        \/\/ \u51e6\u7406B\uff08\u7701\u7565\uff09\n    }\n}<\/code><\/pre>\n<\/div>\n<h2>Worker\u306b\u3088\u308bJOB\u306e\u5b9f\u884c<\/h2>\n<p>\u4e0a\u8a18\u3067QUEUE\u306b\u305f\u3081\u8fbc\u3093\u3060JOB\u3092\u5b9f\u884c\u3059\u308b\u306e\u304cWoker\u3067\u3059\u3002<\/p>\n<p>entry\u30e1\u30bd\u30c3\u30c9\u304c\u5b9f\u884c\u3055\u308c\u308b\u305f\u3073\u306b\u3001QUEUE\uff08jobs\u30c6\u30fc\u30d6\u30eb\uff09\u306bJOB\u304c\u305f\u307e\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u306b\u3088\u308a\u3001Worker\u3092\u8d77\u52d5\u3055\u305b\u308b\u3053\u3068\u3067JOB\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>control +\u00a0 - \u7b49\u3067\u629c\u3051\u308b\u307e\u3067\u9806\u6b21JOB\u3092\u51e6\u7406\u3057\u7d9a\u3051\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>php artisan queue:work\n<\/code><\/pre>\n<\/div>\n<h2>Supervisor\u306b\u3088\u308bWorker\u306e\u7ba1\u7406<\/h2>\n<p>QUEUE\u306b\u305f\u307e\u3063\u305fJOB\u3092\u3059\u3050\u306b\u51e6\u7406\u3057\u305f\u3044\u3002\u3068\u601d\u3063\u3066\u3044\u307e\u3057\u305f\u304c\u3001\u4e0a\u8a18\u306e\u65b9\u6cd5\u3067\u306eWorker\u8d77\u52d5\u3067\u3059\u3068\u30ea\u30ea\u30fc\u30b9\u6642\u306b\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304a\u3044\u305f\u3082\u306e\u306eWEB\u30b5\u30fc\u30d0\u30fc\u306e\u518d\u8d77\u52d5\u7b49\u306e\u4f55\u3089\u304b\u306e\u7406\u7531\u3067Worker\u304c\u7d42\u4e86\u3057\u3066\u3057\u307e\u3063\u305f\u5834\u5408\u3001QUEUE\u306b\u305f\u307e\u3063\u305fJOB\u304c\u51e6\u7406\u3055\u308c\u306a\u3044\u3068\u3044\u3046\u554f\u984c\u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u305d\u3053\u3067linux\u306eSupervisor\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u30b3\u30de\u30f3\u30c9\u3092\u7ba1\u7406\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-plain\" data-lang=\"Plain Text\"><code>\/\/ Supervisor\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\nroot\u30ed\u30b0\u30a4\u30f3\nyum -y install supervisor\nsystemctl start supervisord\nsystemctl enable supervisord\nsystemctl status supervisord\n\n\/\/ Supervisor\u306bworker\u8a2d\u5b9a\nvi \/etc\/supervisord.conf\n\u4ee5\u4e0b\u3092\u8a2d\u7f6e\n  ```\n  [program:laravel-worker]\n  process_name=%(program_name)s_%(process_num)02d\n  command=php \/var\/www\/app_name\/artisan queue:work\n  autostart=true\n  autorestart=true\n  user=root\n  numprocs=1\n  startsecs=0\n  redirect_stderr=true\n  stdout_logfile=\/var\/log\/queue_worker\/worker.log\n  stopwaitsecs=3600\n ```\n\n\/\/ \u30ed\u30b0\u51fa\u529b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u4f5c\u6210\nmkdir \/var\/log\/queue_worker\n\n\/\/ \u518d\u8d77\u52d5\nsystemctl restart supervisord\nsystemctl status supervisord\n<\/code><\/pre>\n<\/div>\n<p>numprocs\u30921\u306b\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u30671\u3064\u305a\u3064\u51e6\u7406\u304c\u5b9f\u884c\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002\u9060\u85e4\u3067\u3059\u3002 \u4eca\u56de\u306fLaravel\u306eJOB\u3068QUEUE\u3092\u4f7f\u7528\u3057\u3066\u975e\u540c\u671f\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u3066\u307f\u305f\u306e\u3067\u3001\u3064\u307e\u305a\u3044\u305f\u70b9\u306b\u3082\u89e6\u308c\u306a\u304c\u3089\u5b9f\u88c5\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3059\u3002 \u3061\u306a\u307f\u306b\u4eca\u56de\u5b9f\u88c5\u3057\u305f\u975e\u540c\u671f\u51e6\u7406\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u3082\u306e&#8230;<\/p>\n","protected":false},"author":5,"featured_media":7070,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,8],"tags":[186],"_links":{"self":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/7068"}],"collection":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/comments?post=7068"}],"version-history":[{"count":14,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/7068\/revisions"}],"predecessor-version":[{"id":7106,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/7068\/revisions\/7106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/media\/7070"}],"wp:attachment":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/media?parent=7068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/categories?post=7068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/tags?post=7068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}