I’ve a operate which takes the incoming request, parses the info and performs an motion and posts the outcomes to a webhook. That is operating as background as a Celery Job. This operate is a standard interface for a couple of dozen Processors, so could be stated to comply with the Manufacturing facility Sample. Right here is the psuedo code:
processors = {
"action_1": ProcessorClass1,
"action_2": ProcessorClass2,
...
}
def run_task(motion, input_file, *args, **kwargs):
# Get the enter file from a URL
log = create_logitem()
strive:
file = get_input_file(input_file)
besides:
log.standing = "Failure"
# course of the enter file
strive:
processor = processors[action](file)
outcomes = processor.execute()
besides:
log.standing = "Failure"
# add the outcomes to a different location
strive:
upload_result_file(outcomes.file)
besides:
log.standing = "Failure"
# Put up the log about the whole course of to a webhoook
post_results_to_webhook(log)
This has been working nicely for many half because the the inputs have been restricted to motion and a single argument (input_file
). Because the software program has grown, the processors have elevated and the enter arguments have began to fluctuate. All the brand new arguments are handed as key phrase arguments and the logic has change into extra like this.
strive:
input_file = get_input_file(input_file)
if motion == "action_2":
input_file_2 = get_input_file(kwargs.get("input_file_2"))
besides:
log.standing = "failure"
strive:
processor = processors[action](file)
if motion == "action_1":
extra_argument = kwargs.get("extra_argument")
outcomes = processor.execute(extra_argument)
elif motion == "action_2":
extra_1 = kwargs.get("extra_1")
extra_2 = kwargs.get("extra_2")
outcomes = processor.execute(input_file_2, extra_1, extra_2)
else:
outcomes = processor.execute()
besides:
log.standing = "Failure"
Including the if situations for a few issues did not make a distinction, however now virtually 6 of the 11 processors have further inputs particular to them and the code is beginning to look complicated and I’m not positive tips on how to simplify it. Or if in any respect I ought to try at simplifying it.
One thing I’ve thought-about:
- Create a separate process for the processors with further inputs – However this may imply, I will likely be repeating the file fetching, logging, end result add and webhook code in every process.
- Shifting the file obtain and argument parsing into the BaseProcessor – This isn’t doable because the processor is utilized in different contexts with out the file obtain and webhooks as nicely.