Skip to content

Commit

Permalink
refactor: Mark batches as failed if any exception occurs during export
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmackay committed Nov 27, 2018
1 parent 385dc25 commit 4812223
Showing 1 changed file with 63 additions and 50 deletions.
113 changes: 63 additions & 50 deletions src/Core/Usecase/Post/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,57 +110,70 @@ public function interact()
]);
$batchId = $this->repo->create($batchEntity);

// verify the user can export posts
$this->verifyAuth($job, 'export');
// merge filters from the controller/cli call with the job's saved filters
$data = $this->constructSearchData($job);
$this->postExportRepository->setSearchParams($data);

// get the form attributes for the export
$attributes = $this->formAttributeRepository->getExportAttributes($data->include_attributes);
$keyAttributes = $this->getAttributesWithKeys($attributes);

/**
* get the search results based on filters
* and retrieve the metadata for each of the posts
**/
$posts = $this->postExportRepository->getSearchResults();
foreach ($posts as $idx => $post) {
// Retrieved Attribute Labels for Entity's values
$post = $this->postExportRepository->retrieveMetaData($post->asArray(), $keyAttributes);
$posts[$idx] = $post;
try {
// verify the user can export posts
$this->verifyAuth($job, 'export');
// merge filters from the controller/cli call with the job's saved filters
$data = $this->constructSearchData($job);
$this->postExportRepository->setSearchParams($data);

// get the form attributes for the export
$attributes = $this->formAttributeRepository->getExportAttributes($data->include_attributes);
$keyAttributes = $this->getAttributesWithKeys($attributes);

/**
* get the search results based on filters
* and retrieve the metadata for each of the posts
**/
$posts = $this->postExportRepository->getSearchResults();
foreach ($posts as $idx => $post) {
// Retrieved Attribute Labels for Entity's values
$post = $this->postExportRepository->retrieveMetaData($post->asArray(), $keyAttributes);
$posts[$idx] = $post;
}
Log::debug('EXPORTER: on interact Count posts: ' . count($posts));

/**
* update the header attributes
* in the job table so we know which headers to
* use in other chunks of the export
*/
$this->saveHeaderRow($job, $attributes);

/**
* set 'add header' in the formatter
* so it knows how to return the results
* for the csv (with or without a header row)
*/
$this->formatter->setAddHeader($this->filters['add_header']);
// handle hxl
$hxl_rows = $this->formatter->generateHXLRows(
$this->formatter->createHeading($attributes),
$this->getHxlRows($job)
);
$this->saveHXLHeaderRow($job, $hxl_rows);
$this->formatter->setHxlHeading($hxl_rows);
$formatter = $this->formatter;
Log::debug('EXPORTER: Count posts: ' . count($posts));

/**
* KeyAttributes is sent instead of the header row because it contains
* the attributes with the corresponding features (type, priority) that
* we need for manipulating the data
*/
$file = $formatter($posts, $job, $keyAttributes);
} catch (\Exception $e) {
// Mark batch as failed
$batchEntity = $this->repo->get($batchId);
$batchEntity->setState([
'status' => ExportBatch::STATUS_FAILED,
'filename' => $file->file,
'rows' => count($posts),
]);
$this->repo->update($batchEntity);
// And rethrow the error
throw $e;
}
Log::debug('EXPORTER: on interact Count posts: ' . count($posts));

/**
* update the header attributes
* in the job table so we know which headers to
* use in other chunks of the export
*/
$this->saveHeaderRow($job, $attributes);

/**
* set 'add header' in the formatter
* so it knows how to return the results
* for the csv (with or without a header row)
*/
$this->formatter->setAddHeader($this->filters['add_header']);
// handle hxl
$hxl_rows = $this->formatter->generateHXLRows(
$this->formatter->createHeading($attributes),
$this->getHxlRows($job)
);
$this->saveHXLHeaderRow($job, $hxl_rows);
$this->formatter->setHxlHeading($hxl_rows);
$formatter = $this->formatter;
Log::debug('EXPORTER: Count posts: ' . count($posts));

/**
* KeyAttributes is sent instead of the header row because it contains
* the attributes with the corresponding features (type, priority) that
* we need for manipulating the data
*/
$file = $formatter($posts, $job, $keyAttributes);

// Update export batch status=done
// Include filename, post count, header row etc
Expand Down

0 comments on commit 4812223

Please sign in to comment.