In a controller within a package I am triggering a batch task using code very similar to Automated Tasks :: Concrete CMS except I need to pass in an itemID through the Input to the task runner to process a specific batch of items.
This all works great however the task does not actually start running until a concrete page is viewed. Is there something special which happens when a PageController handles the request which initiates pending batches?
The custom code is not part of a single page controller so there is no page within the site tree - just a controller and a view.
Is there a way within my custom code I can initiate the batch process as well?
code sample:
$itemToProcessBatch = 123;
$taskRepo = $this->getEntityManager()->getRepository(Task::class);
$task = $taskRepo->findOneBy(['handle' => 'task_name']);
$definition = $task->getController()->getInputDefinition();
$input = new Input();
foreach ($definition->getFields() as $field) {
if ($field->getKey() !== 'itemID') {
continue;
}
$fieldInput = new LoadedField($field->getKey(), $itemToProcessBatch);
$input->addField($fieldInput);
}
$runner = $task->getController()->getTaskRunner($task, $input);
$handler = $this->app->make($runner->getTaskRunnerHandler());
$handler->boot($runner);
$contextFactory = $this->app->make(ContextFactory::class);
$context = $contextFactory->createDashboardContext($runner);
$handler->start($runner, $context);
$handler->run($runner, $context);
$response = $handler->complete($runner, $context);