The Apex Async Worker Pattern to rule them all

gregheartcloud
2 min readApr 2, 2023
Someday I will be recognized for genius or locked up for insanity

As part of my series… lets build a salesforce app… I have another utility to add to the Enterprise Utilities library. This thing is awesome. I call it a “worker framework” to attempt to bring some organization and control what is otherwise a chaotic unorganized universe of async processing in Salesforce.

Here is what I don’t like about my experience on salesforce with async processing:

  1. Scheduled job in general. I find the solution extremely lacking from other CRON based tools and especially I don’t like the fact that the schedules are not able to be paused or easily created in refreshed sandboxes or scratch orgs
  2. Too much boiler plate. Each batch job I end up putting in the exact same boiler plate code, especially in relation to cross cutting concerns (sensing a pattern in my blog?)
  3. The user interface could use a major upgrade (Reactforce to the rescue!)
  4. My obsession with api driven development requires me to attach every feature of the system to an api… including all operations related to managing async jobs
  5. Common exception handling, logging, and monitoring are just the right things to do

Here are the requirements for the framework:

  1. A new worker implementation can be added by creating a record in the ImplementationHandler custom metadata and implementing the enterprise.IWorker interface. Creating scalable, robust, and powerful async jobs becomes extremely easy once all of the non-functional features have been managed by the framework.
  2. The implemented interface can be scheduled, unscheduled(paused), paused, and executed immediately.
  3. The workers can be scheduled with CRON schedules but also the CRON schedule can be created with human readable and manageable concepts like repeat jobs per month, per day, per hour, every x number of minutes, etc. Trust me. Its awesome.
  4. The api framework can be used to manage all features of the worker framework. This makes building tooling easy to build and it opens the door to front end developers (Reactforce anyone?) to manage async tasks easily from the front end.

This feature shall ever be known as feature/ENT-18__WorkerFramework

I’ll update this blog post when its done. Happy coding!

--

--