DEV Community

Aditya Pratap Singh
Aditya Pratap Singh

Posted on

Background media processing with automated Courier notifications

Any website that accepts media content (photos, videos, etc) does some or the other form of background processing on said media. Operations like compression, validation, re-encoding and watermarking are often essential to services of many popular web apps. Even more important is the surety of being notified whenever something goes wrong in crucial operations like these.

Hence, when participating in Courier hackathon, I thought of building something that might help the developer community. While researching serverless functions, I realised that their nature is to handle short bursty workloads asynchronously. Further, when I asked my experienced seniors about these kinds of workloads, they informed me that the management of media files in the backends of websites is a huge task which could use the asynchronous nature of serverless functions.

The first step is to decide the types of media we'll be operating upon. Since I was building a simple PoC, I decided to stick with the standard - Photos and Videos. Now, we need to decide what operations our utility will support. Here are the ones I went with (feel free to choose your own):

  • Compression
  • Watermarking
  • Inversion
  • Resizing
  • Trimming

Now, we'll build standalone functions to support these operations, as can be seen in utils.py.

Finally, onto the most important thing. Every code has edge cases that it fails on. We need to be certain that we'll be notified of all failures so we can take appropriate action if and when an exception occurs. Hence, we'll wrap relevant parts of code in a try except block.

This is where the beauty of Courier's seamless notification API comes in. It allows us to easily integrate the code to send a variety of notifications to admins (i.e. us) as well as users about errors in processing of files. It supports a variety of notification services, from emails to Slack to platform native notifications.

Courier passes messages to Integrations via the Send endpoint. We must send an Authorization header with each request. The Courier Send API also requires an event. The authorization token and event values are the "Auth Token" and "Notification ID" we see in the detail view of our “Test Appointment Reminder” event. Click the gear icon next to the Notification's name to reveal them.

Image description

These variables can finally be fed into Courier's Python SDK to facilitate simple notification sending.

Top comments (0)