Part of the application I am working on involves the download of emails containing attachments, detecting a barcode in each attachment, scanning it for an identifier and using the identifier to insert data into a database.
So far my usage of message buses has been limited to Pub/Sub and I've never really had the need to use Sagas. However, given that each of the above steps is part of the same related process, and we want durability should any part of the process fail, is a Saga a good fit?
Details of our process and how I thought we could use a Saga are as follows:
- A Quartz job is used to download emails that contain PDF attachments. The Quartz job uses a
Connector
that includes information about theTenant
. This tenant information will be needed by the Saga so we can interact with tenant specific resources (database/storage). - For each PDF attachment we kick off a "Process Document" Saga passing it the tenant information and the document itself (assuming the Message Bus can persist this) or the path of the file on disk. After a Saga has been started for each attachment on the source email, the connector can remove/archive the email from the mailbox so that it is not processed again.
Each Saga will then perform the following tasks:
- Attempt to read a barcode from the attachment/file. If the barcode detection fails we would need to upload the file to a specified location so it can be processed manually. This would complete the Saga. If a barcode was detected we decode a database identifier.
- Using the identifier we load the relevant
Order
record from our database and if it exists:- Upload the file to a specified location returning the URI
- Insert a new
OrderDocument
record into our database that references the file URI.
- We update a
OrderDocumentBundle
that is a single file containing all documents linked to an order. So when a newOrderDocument
is created we should load theOrderDocumentBundle
for the relatedOrder
and append the pages from the newOrderDocument
. - The Saga completes.
I am concerned about concurrency issues during the final bundling process as we may have multiple messages that attempt to update the bundle. We would need to ensure that the Saga waits for the bundle file to be available so that it complete.