If this is better suited for StackOverflow let me know and I'll head over there... I think it belongs here though.
I'm writing an ASP.NET Core REST API that returns 201s and 202s after kicking off long running asynchronous jobs using Hangfire. If the jobs fail mid way Hangfire will pick things back up, run retries, etc. Obviously in order to do so, it needs to know some state.
Hangfire recommends that the arguments to background jobs remain "small and simple", and that if a background job needs some more complicated object to write the ID to a database.
My background jobs need several API keys, passwords, some not so easy to serialize objects, and some YAML files loaded as config via an internal common configuration library. Because of the sensitivity of these data, I would rather not write them to the database. The options I see here are two:
- Pass each and every piece of the larger objects as arguments to the Hangfire job and let Hangfire serialize them to its DB. For sensitive arguments like API keys and passwords, first encrypt them using some cert bundled with the app. The background job will have to decrypt them using the same cert after fetching them from the database when it begins running.
- Set the objects to some
public static
property in apublic static
class at startup. The background job can just refer to that when it needs. For example:
// Startup code simplified for the sake of the example
{
var envConfigs = new EnvironmentConfiguration( ... a bunch of random stuff ... );
service.AddSingleton(envConfigs);
EnvironmentConfiguration.Instance = envConfigs;
}
// Hangfire job
{
var result = BusinessLogicDoerMethod(EnvironmentConfiguration.Instance.BusinessLogicInformation);
}
Option 1 is annoying because we have to pass a lot of arguments, and also have to deal with the encrypt / decrypt logic.
Option 2 is probably fine, the danger I see here is if one background job mutates the EnvironmentConfiguration
object, the other jobs and the rest of the app will also see that mutation. However, I can easily solve this problem by simply not doing that.