Skip to content

Commit

Permalink
Fix/aut 3877/archive tasks (#4105)
Browse files Browse the repository at this point in the history
* feat: archive tasks using bulk update with predefined batch
  • Loading branch information
tikhanovichA authored Oct 1, 2024
1 parent 478e5ff commit 17d836c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
40 changes: 36 additions & 4 deletions actions/class.TaskQueueWebApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,48 @@ public function archive()
$this->checkIfTaskIdExists();
$taskIds = $this->detectTaskIds();

// Get task log service
$taskLogService = $this->getTaskLogService();

$filter = $taskIds === static::ALL
? (new TaskLogFilter())->availableForArchived($this->getSessionUserUri())
: (new TaskLogFilter())
// Define batch size for the chunk of tasks to be processed in each iteration
$batchSize = $taskLogService->getOption(TaskLogInterface::OPTION_DEFAULT_BATCH_SIZE);
$success = false;

// If taskIds is ALL, we'll fetch all tasks using pagination
if ($taskIds === static::ALL) {
do {
// Set the filter with limit and offset for pagination
$filter = (new TaskLogFilter())
->availableForArchived($this->getSessionUserUri())
->setLimit($batchSize)
->setSortBy(TaskLogBrokerInterface::COLUMN_CREATED_AT);
// Fetch the tasks for the current batch
$taskLogCollection = $taskLogService->search($filter);

// If no tasks are returned, break the loop
if (count($taskLogCollection) === 0) {
break;
}

// Process the current batch
$success = $taskLogService->archiveCollection($taskLogCollection) || $success;
} while (count($taskLogCollection) === $batchSize);
} else {
// Handle specific task IDs (no need for pagination)
$filter = (new TaskLogFilter())
->addAvailableFilters($this->getSessionUserUri())
->in(TaskLogBrokerInterface::COLUMN_ID, $taskIds);

// Fetch all tasks based on provided task IDs
$taskLogCollection = $taskLogService->search($filter);

// Process the tasks without pagination
$success = $taskLogService->archiveCollection($taskLogCollection);
}

// Return JSON response
return $this->returnJson([
'success' => (bool) $taskLogService->archiveCollection($taskLogService->search($filter))
'success' => (bool) $success
]);
} catch (Exception $e) {
$this->setErrorJsonResponse(
Expand Down
42 changes: 42 additions & 0 deletions migrations/Version202409301236442234_tao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace oat\tao\migrations;

use Doctrine\DBAL\Schema\Schema;
use oat\tao\model\taskQueue\TaskLogInterface;
use oat\tao\scripts\tools\migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*
* phpcs:disable Squiz.Classes.ValidClassName
*/
final class Version202409301236442234_tao extends AbstractMigration
{
public function getDescription(): string
{
return 'Update taoTaskLog with default batch size';
}

public function up(Schema $schema): void
{
$serviceManager = $this->getServiceManager();
$taskLog = $serviceManager->get(TaskLogInterface::SERVICE_ID);
$taskLog->setOption(TaskLogInterface::OPTION_DEFAULT_BATCH_SIZE, TaskLogInterface::DEFAULT_BATCH_SIZE);
$serviceManager->register(TaskLogInterface::SERVICE_ID, $taskLog);
}

public function down(Schema $schema): void
{
$serviceManager = $this->getServiceManager();
$taskLog = $serviceManager->get(TaskLogInterface::SERVICE_ID);
$options = $taskLog->getOptions();
if (isset($options[TaskLogInterface::OPTION_DEFAULT_BATCH_SIZE])) {
unset($options[TaskLogInterface::OPTION_DEFAULT_BATCH_SIZE]);
}
$taskLog->setOptions($options);
$serviceManager->register(TaskLogInterface::SERVICE_ID, $taskLog);
}
}
3 changes: 3 additions & 0 deletions models/classes/taskQueue/TaskLogInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ interface TaskLogInterface extends LoggerAwareInterface

public const DEFAULT_LIMIT = 20;

public const OPTION_DEFAULT_BATCH_SIZE = 'default_batch_size';
public const DEFAULT_BATCH_SIZE = 1000;

/**
* It's not related to the user management, just a placeholder to distinguish the user running the script from CLI.
*/
Expand Down
3 changes: 2 additions & 1 deletion scripts/install/RegisterTaskQueueServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class RegisterTaskQueueServices extends InstallAction
public function __invoke($params)
{
$taskLogService = new TaskLog([
TaskLogInterface::OPTION_TASK_LOG_BROKER => new TaskLog\Broker\RdsTaskLogBroker('default')
TaskLogInterface::OPTION_TASK_LOG_BROKER => new TaskLog\Broker\RdsTaskLogBroker('default'),
TaskLogInterface::OPTION_DEFAULT_BATCH_SIZE => TaskLogInterface::DEFAULT_BATCH_SIZE
]);
$this->registerService(TaskLogInterface::SERVICE_ID, $taskLogService);

Expand Down

0 comments on commit 17d836c

Please sign in to comment.