diff --git a/classes/core/PKPQueueProvider.php b/classes/core/PKPQueueProvider.php index f644c3c62b7..644d5f856b4 100644 --- a/classes/core/PKPQueueProvider.php +++ b/classes/core/PKPQueueProvider.php @@ -1,7 +1,5 @@ app->get('queue.worker'); /** @var \Illuminate\Queue\Worker $worker */ $worker - ->setCache(app()->get('cache.store')) + ->setCache($this->app->get('cache.store')) ->daemon( $connection, $queue, @@ -98,9 +96,9 @@ public function runJobInQueue(): void return; } - $laravelContainer = PKPContainer::getInstance(); + $worker = $this->app->get('queue.worker'); /** @var \Illuminate\Queue\Worker $worker */ - $laravelContainer['queue.worker']->runNextJob( + $worker->runNextJob( 'database', $job->queue ?? Config::getVar('queues', 'default_queue', 'queue'), $this->getWorkerOptions() @@ -114,7 +112,13 @@ public function runJobInQueue(): void public function boot() { if (Config::getVar('queues', 'job_runner', true)) { - register_shutdown_function(function () { + $currentWorkingDir = getcwd(); + register_shutdown_function(function () use ($currentWorkingDir) { + + // restore the current working directory + // see: https://www.php.net/manual/en/function.register-shutdown-function.php#refsect1-function.register-shutdown-function-notes + chdir($currentWorkingDir); + // As this runs at the current request's end but the 'register_shutdown_function' registered // at the service provider's registration time at application initial bootstrapping, // need to check the maintenance status within the 'register_shutdown_function' diff --git a/classes/task/ProcessQueueJobs.php b/classes/task/ProcessQueueJobs.php index f93086880b3..ac1a21eb8a2 100644 --- a/classes/task/ProcessQueueJobs.php +++ b/classes/task/ProcessQueueJobs.php @@ -16,7 +16,6 @@ namespace PKP\task; -use APP\core\Application; use PKP\config\Config; use PKP\core\PKPContainer; use PKP\queue\JobRunner; @@ -38,11 +37,13 @@ public function getName(): string */ public function executeActions(): bool { - if (Application::isUnderMaintenance() || !Config::getVar('queues', 'job_runner', true)) { + // If processing of queue jobs via schedule task is disbaled + // will not process any queue jobs via scheduler + if (!Config::getVar('queues', 'process_jobs_at_task_scheduler', false)) { return true; } - $jobQueue = app('pkpJobQueue'); + $jobQueue = app('pkpJobQueue'); /** @var \PKP\core\PKPQueueProvider $jobQueue */ $jobBuilder = $jobQueue->getJobModelBuilder(); @@ -50,16 +51,25 @@ public function executeActions(): bool return true; } - // Executes all pending jobs when running the runScheduledTasks.php on the CLI + // When processing queue jobs vai schedule task in CLI mode + // will process a limited number of jobs at a single time if (PKPContainer::getInstance()->runningInConsole('runScheduledTasks.php')) { - while ($jobBuilder->count()) { + $maxJobCountToProcess = abs(Config::getVar('queues', 'job_runner_max_jobs', 30)); + + while ($jobBuilder->count() && $maxJobCountToProcess) { $jobQueue->runJobInQueue(); + --$maxJobCountToProcess; } return true; } - // Executes a limited number of jobs when processing a request + // We don't need to process jobs when the job runner is enabled + if (Config::getVar('queues', 'job_runner', false)) { + return true; + } + + // Executes a limited number of jobs when processing a via web request mode (new JobRunner($jobQueue)) ->withMaxExecutionTimeConstrain() ->withMaxJobsConstrain() diff --git a/jobs/testJobs/TestJobFailure.php b/jobs/testJobs/TestJobFailure.php index d5f2e54035a..ff2459e4420 100644 --- a/jobs/testJobs/TestJobFailure.php +++ b/jobs/testJobs/TestJobFailure.php @@ -53,6 +53,6 @@ public function __construct() */ public function handle(): void { - throw new Exception('cli.test.job'); + throw new Exception('Test failure job'); } } diff --git a/jobs/testJobs/TestJobSuccess.php b/jobs/testJobs/TestJobSuccess.php index 9ec8ed6bf08..f30a4195aae 100644 --- a/jobs/testJobs/TestJobSuccess.php +++ b/jobs/testJobs/TestJobSuccess.php @@ -45,5 +45,6 @@ public function __construct() */ public function handle(): void { + error_log('Test success job'); } }