Skip to content

Commit

Permalink
Merge pull request #10196 from touhidurabir/i9823_main
Browse files Browse the repository at this point in the history
#9823 update the crontrol job processing via scheduler
  • Loading branch information
touhidurabir authored Aug 2, 2024
2 parents 11dd55e + 4c85bd3 commit c21cb43
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
18 changes: 11 additions & 7 deletions classes/core/PKPQueueProvider.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/**
* @file classes/core/PKPQueueProvider.php
*
Expand Down Expand Up @@ -76,10 +74,10 @@ public function getWorkerOptions(array $options = []): WorkerOptions
*/
public function runJobsViaDaemon(string $connection, string $queue, array $workerOptions = []): void
{
$worker = PKPContainer::getInstance()['queue.worker']; /** @var \Illuminate\Queue\Worker $worker */
$worker = $this->app->get('queue.worker'); /** @var \Illuminate\Queue\Worker $worker */

$worker
->setCache(app()->get('cache.store'))
->setCache($this->app->get('cache.store'))
->daemon(
$connection,
$queue,
Expand All @@ -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()
Expand All @@ -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'
Expand Down
22 changes: 16 additions & 6 deletions classes/task/ProcessQueueJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace PKP\task;

use APP\core\Application;
use PKP\config\Config;
use PKP\core\PKPContainer;
use PKP\queue\JobRunner;
Expand All @@ -38,28 +37,39 @@ 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();

if ($jobBuilder->count() <= 0) {
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()
Expand Down
2 changes: 1 addition & 1 deletion jobs/testJobs/TestJobFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public function __construct()
*/
public function handle(): void
{
throw new Exception('cli.test.job');
throw new Exception('Test failure job');
}
}
1 change: 1 addition & 0 deletions jobs/testJobs/TestJobSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ public function __construct()
*/
public function handle(): void
{
error_log('Test success job');
}
}

0 comments on commit c21cb43

Please sign in to comment.