I have a controller that suppose to run the import
. The importing process might take a long time so I have decided to use a message queue
(async). I have created a wrapper
interface that has a method import
to encapsulate the implementation. From the controller's perspective it shouldn't care how it is imported (whether straight away or async). But the original code can throw exceptions and if it is async then there is no way to catch these exceptions in the controller.
public function execute()
{
try {
$this->importer->import();
$this->messageManager->addSuccessMessage(__('The import has been successfully performed.'));
} catch (Exception $e) {
$this->logger->error($e->getMessage());
}
I mean the problem is that if I swap the async importer with the original one, we can know whether it will succeed or fail. But when we use the async one then code can't simply just output "the import has been successful", nor can it output "the import has been scheduled" because it is an implementation detail leak.
Any recommendations on how to solve this problem?
Update: I assume these are two different responsibilities:
- Import
- Schedule the import
So I guess they are not interchangeable at all.