queue:work ã® --memory å¼æ°ãâ¦â¦ã¡ãã£ã¨â¦â¦
php artisan queue:work ã«ã¯ãè²ã
ãªå¼æ°ãããããã§ãã
ãã¼ã¸ã§ã³ã«ããããã ãããªããã¨æãã®ã§ãããã¨ããããæå
ã® 8.83.27 ã®ãã¼ã¸ã§ã³ã§ã話ãé²ãã¾ãã
ã¨ããããå¼æ°ã®ä¸è¦§ã¯ãvendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php ã«ããã°
protected $signature = 'queue:work {connection? : The name of the queue connection to work} {--name=default : The name of the worker} {--queue= : The names of the queues to work} {--daemon : Run the worker in daemon mode (Deprecated)} {--once : Only process the next job on the queue} {--stop-when-empty : Stop when the queue is empty} {--delay=0 : The number of seconds to delay failed jobs (Deprecated)} {--backoff=0 : The number of seconds to wait before retrying a job that encountered an uncaught exception} {--max-jobs=0 : The number of jobs to process before stopping} {--max-time=0 : The maximum number of seconds the worker should run} {--force : Force the worker to run even in maintenance mode} {--memory=128 : The memory limit in megabytes} {--sleep=3 : Number of seconds to sleep when no job is available} {--rest=0 : Number of seconds to rest between jobs} {--timeout=60 : The number of seconds a child process can run} {--tries=1 : Number of times to attempt a job before logging it failed}';
ãããªæãã®ããã§ãã
ããã¾ãä¾ãã°sleepã¨ãtimeoutã¨ããtriesã¨ããã¾ããããçã«ã¯ããããå¿
è¦ã ããããããã£ã¦æããªã®ã§ããã
memoryããã¶ã£ã¡ããã¾ããç´æçã§ã¯ãªãããããªãããã?ãã¨ãæãããã§ãããã¾ãã
ãThe memory limit in megabytes(ã¡ã¬ãã¤ãåä½ã®ã¡ã¢ãªå¶é)ãã¨ãããããããªã®ã§ãããã¡ãã£ã¨ããèªå¼ãããããããªãããã? ã¨ãæã£ãããã¾ãã
å
ã«çããã²ããã¨
ã»ãããã§æå®ããã¡ã¢ãªä»¥ä¸ã®ã¡ã¢ãªãé£ãã¨ããããæ¢ãããå®è£
ã¯ãã
ã»PHPã®memory_limitãå¼ãä¸ãããããªåä½ã¯ããªã
ã»--onceãæå®ãããããã¡ã¢ãªãã§ãã¯ã¯ããªã
ã¨ãªãããã§ãã
1çªç®ã¯ã¾ãããããããªã®ã§ãããå¤åå¼ã£ããããããªã®ã2çªç®ã®ãPHPã®memory_limitãå¼ãä¸ãããããªåä½ã¯ããªããã3çªç®ã¯â¦â¦ãããã®å¼æ°ã使ããã¦ãã®ãã?ãã¨ãæããã§ããã使ã£ã¦ãã¨ããã¨ãããããããã¡ããã£ã¨æ°ã«ãªãããã
ãªããæ®éã«ãã®å¼æ°ãèããã¨ããã®å¼æ°ã¾ã§ã¯ã¡ã¢ãªã確ä¿ãã¦ãããããããªåä½ãæå¾
ããããããªæ°ãããã®ã§ãããå®éã«ã¯ããã£ã¦ãªãã¨æããã¾ããã
ã³ã¼ããéã«è¿½ãããã¦ããéã
vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php
public function handle() { if ($this->downForMaintenance() && $this->option('once')) { return $this->worker->sleep($this->option('sleep')); } // We'll listen to the processed and failed events so we can write information // to the console as jobs are processed, which will let the developer watch // which jobs are coming through a queue and be informed on its progress. $this->listenForEvents(); $connection = $this->argument('connection') ?: $this->laravel['config']['queue.default']; // We need to get the right queue for the connection which is set in the queue // configuration file for the application. We will pull it based on the set // connection being run for the queue operation currently being executed. $queue = $this->getQueue($connection); return $this->runWorker( $connection, $queue ); }
ãã
protected function runWorker($connection, $queue) { return $this->worker->setName($this->option('name')) ->setCache($this->cache) ->{$this->option('once') ? 'runNextJob' : 'daemon'}( $connection, $queue, $this->gatherWorkerOptions() ); }
ãã
vendor/laravel/framework/src/Illuminate/Queue/Worker.php
public function daemon($connectionName, $queue, WorkerOptions $options) { if ($supportsAsyncSignals = $this->supportsAsyncSignals()) { $this->listenForSignals(); } (ä¸ç¥) // Finally, we will check to see if we have exceeded our memory limits or if // the queue should restart based on other indications. If so, we'll stop // this worker and let whatever is "monitoring" it restart the process. $status = $this->stopIfNecessary( $options, $lastRestart, $startTime, $jobsProcessed, $job ); if (! is_null($status)) { return $this->stop($status); } } }
ãã
protected function stopIfNecessary(WorkerOptions $options, $lastRestart, $startTime = 0, $jobsProcessed = 0, $job = null) { if ($this->shouldQuit) { return static::EXIT_SUCCESS; } elseif ($this->memoryExceeded($options->memory)) { return static::EXIT_MEMORY_LIMIT; } elseif ($this->queueShouldRestart($lastRestart)) { return static::EXIT_SUCCESS; } elseif ($options->stopWhenEmpty && is_null($job)) { return static::EXIT_SUCCESS; } elseif ($options->maxTime && hrtime(true) / 1e9 - $startTime >= $options->maxTime) { return static::EXIT_SUCCESS; } elseif ($options->maxJobs && $jobsProcessed >= $options->maxJobs) { return static::EXIT_SUCCESS; } }
ãã
public function memoryExceeded($memoryLimit) { return (memory_get_usage(true) / 1024 / 1024) >= $memoryLimit; }
ã£ã¦ãªæãã½ãã§ãã
ã¡ãªãã³ã¼ãã»ããã£ã¦ã¦æ°ã¥ããã®ã§ããã
vendor/laravel/framework/src/Illuminate/Queue/Listener.php
public function runProcess(Process $process, $memory) { $process->run(function ($type, $line) { $this->handleWorkerOutput($type, $line); }); // Once we have run the job we'll go check if the memory limit has been exceeded // for the script. If it has, we will kill this script so the process manager // will restart this with a clean slate of memory automatically on exiting. if ($this->memoryExceeded($memory)) { $this->stop(); } } (ç¥) public function memoryExceeded($memoryLimit) { return (memory_get_usage(true) / 1024 / 1024) >= $memoryLimit; }
ã£ã¦ã³ã¼ãããããã§ããããâ¦â¦ã¾ãããã¨ãã¦ã
ãªã®ã§ã
å¤åãªãã¨ãªããPHPããã£ã¤ãè½ã¨ãã¦ããåã«æåã§ã³ã³ããã¼ã«ã§ãã¦ããéã«ããããæ¢ãããã£ã¦ã®ãæå³ãã¦ããä¸æ¹ã§ãã¡ã¢ãªã®ä¸éã¯ç¹ã«ä¸ãã¦ãããªããã£ã½ããã§ããããã
ãã¨ãç´°ããæã§ãæ示çã« --once ã®æã¯å¤åãä¸è¿°ã®ã¡ã¢ãªãã§ãã¯ãããªããæãã§ãããããããã
ãããããªãã§ãããæå³ã¨ãã¦ã¯ãããããdaemonã®ããã«ã°ã«ã°ã«åãç¶ããæã«ãPHPã®ã¡ã¢ãªãªã¼ã¯ã¨ãå«ãã¦"ã¡ã¢ãªå¤§éæ¶è²»"ãããã®ãé²ããããã£ã¦æãããªããããªããã§ãããããããã???
ãã ã¾ããã ã¨ãããããã¡ãã£ã¨å½åãå¤ããã»ãããããããªæ°ãããã§ããªãã®ã§ããâ¦â¦ã¾ããã¾ãã
軽ãã°ã°ã£ã¦ã¿ãéãã ã¨ãæå¤ã«è¨äºããªãã£ãã½ãã®ã§ãã¡ãã£ããä¸çã