The batch operation I created works fine, until it's done. Once it's done it just loops back and does it again. It will loop through until the VM runs out of resources.
I am fairly certain this has to do with my completeImport
method. If I throw a syntax error in there it will finish the import successfully but then error out once it gets to that method.
The controller for the batch op: (I ommited methods that had nothing to do with the batch itself. They were for CRUD or building URLs.)
class MyModuleController {
public function import() {
$endpoints = array(
'courses' => $this->getEndPoint('courses', NULL),
);
foreach ($endpoints as $data_type => $endpoint) {
$response = $this->getResponse($endpoint);
$operations = MyModuleController::processResponse($response, $data_type);
$batch = array(
'title' => 'Importing ' . $data_type,
'operations' => $operations,
'finished' => 'mymodule_complete_import',
'init_message' => t('Importing...'),
'progress_message' => t('Operation @current out of @total.'),
'error_message' => t('Batch failed.'),
'file' => drupal_get_path('module', 'mymodule') . '/mymodule.import.inc',
);
batch_set($batch);
return batch_process('academy/courses/import');
}
return array(
'#type' => 'markup',
'value' => t('Imported?'),
);
}
public static function processResponse($response_data, $data_type) {
$operations = array();
foreach ($response_data as $data) {
switch ($data_type) {
case 'courses':
// Used to look up if a course was already imported.
$id = (string) $data->Id;
$data_array = array(
'Name' => (string) $data->Name,
'Active' => ((string) $data->Active == 'true' ? 1 : 0),
'CourseCodeForBulkImport' => (string) $data->CourseCodeForBulkImport,
'EcommerceLongDescription' => (string) $data->EcommerceLongDescription,
'EcommerceShortDescription' => (string) $data->EcommerceShortDescription,
'ForSale' => ((string) $data->ForSale == 'true' ? 1 : 0),
'Code' => (string) $data->Code,
'Id' => $id,
'OriginalId' => (string) $data->OriginalId,
'Price' => (string) $data->Price,
'Description' => (string) $data->Description,
);
break;
}
$operations[] = array(
'mymodule_response_importer',
array($id, $data_array, $data_type),
);
}
return $operations;
}
public static function completeImport() {
return new RedirectResponse(Url::fromRoute('system.admin_content'));
}
}
mymodule.import.inc:
function mymodule_response_importer($id, $data, $data_type, &$context) {
$entity = MyModuleController::findExisting($data_type, $id);
switch ($data_type) {
case 'courses':
if ($entity === FALSE) {
MyModuleController::createNode($data);
}
else {
MyModuleController::updateNode($data, NULL, $entity);
}
break;
}
}
function mymodule_complete_import() {
MyModuleController::completeImport();
}