-
Notifications
You must be signed in to change notification settings - Fork 15
fix(QueueableProcess): Updates Queueable Process #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,11 +68,17 @@ public with sharing class EnqueueNextQueueableProcessStep implements System.Fina | |
| switch on context.getResult() { | ||
| when SUCCESS { | ||
| if (this.processSteps.size() > 0) { | ||
| // Remove the first element from the list - this was constructed using the .then() method. | ||
| QueueableProcess nextProcessStep = this.processSteps.remove(0); | ||
| // Set the remaining steps on the next process step. | ||
| nextProcessStep.processSteps = this.processSteps; | ||
| // Set the dataPassthrough on the next process step. | ||
| nextProcessStep.dataPassthrough = dataPassthrough; | ||
| // Set the queueableContextHistory on the next process step. | ||
| nextProcessStep.queueableContextHistory = this.queueableContextHistory; | ||
| // Set the finalizerContextHistory on the next process step. | ||
| nextProcessStep.finalizerContextHistory = this.finalizerContextHistory; | ||
| // Enqueue the next process step. | ||
|
Comment on lines
+71
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic here seems to be correct, but it's not very efficient. You're directly modifying the Instead, consider creating a new list that excludes the first element and assign this new list to the - QueueableProcess nextProcessStep = this.processSteps.remove(0);
- nextProcessStep.processSteps = this.processSteps;
+ QueueableProcess nextProcessStep = this.processSteps[0];
+ nextProcessStep.processSteps = this.processSteps.subList(1, this.processSteps.size());This change also avoids the need to remove an element from the beginning of the list, which can be an expensive operation for large lists. |
||
| System.enqueueJob(nextProcessStep); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,9 +61,11 @@ public abstract class QueueableProcess implements Queueable, Database.AllowsCall | |
|
|
||
| /** | ||
| * @description This must be implemented by extending classes. Developers - implement this method with the work you | ||
| * want executed asynchronously. | ||
| * want executed asynchronously. The returned Object will be passed as dataPassthrough to the next step. | ||
| * | ||
| * @return Object - data to pass through to the next step in the process | ||
| */ | ||
| abstract public void execute(); | ||
| abstract public Object execute(); | ||
|
Comment on lines
+64
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change from - abstract public void execute();
+ abstract public Object execute(); |
||
|
|
||
| /** | ||
| * @description this is a default implementation of an handleError method. It's called by the finalizer if the | ||
|
|
@@ -85,23 +87,23 @@ public abstract class QueueableProcess implements Queueable, Database.AllowsCall | |
| */ | ||
| public virtual void execute(QueueableContext context) { | ||
| // if the queueableContextHistory is null, initialize it. | ||
| if (this.queueableContextHistory == null) { | ||
| this.queueableContextHistory = new List<QueueableContext>(); | ||
| } | ||
| this.queueableContextHistory = this.queueableContextHistory ?? | ||
| new List<QueueableContext>(); | ||
| this.queueableContextHistory.add(context); | ||
|
|
||
| // Call the abstract method `execute` and capture its return value as the dataPassthrough for the next step. | ||
| Object nextDataPassthrough = execute(); | ||
|
|
||
| // create a new instance of the finalizer class. Note that we're passing in the list of remaining steps and the | ||
| // passthrough data. | ||
| // returned data from execute() as the passthrough data for the next step. | ||
| Finalizer nextStep = new EnqueueNextQueueableProcessStep( | ||
| this.processSteps, | ||
| this.dataPassthrough, | ||
| nextDataPassthrough, | ||
| this.queueableContextHistory, | ||
| this.finalizerContextHistory | ||
| ); | ||
| // Attach the finalizer to system context. This will take care of enqueueing the next QueueableProcess step in | ||
| // the nextStep. | ||
| System.attachFinalizer(nextStep); | ||
|
Comment on lines
+90
to
107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The refactoring in these lines improves the code by removing the need for an explicit null check on - if (this.queueableContextHistory == null) {
- this.queueableContextHistory = new List<QueueableContext>();
- }
- this.queueableContextHistory.add(context);
- Finalizer nextStep = new EnqueueNextQueueableProcessStep(
- this.processSteps,
- this.dataPassthrough,
- this.queueableContextHistory,
- this.finalizerContextHistory
- );
- System.attachFinalizer(nextStep);
- execute();
+ this.queueableContextHistory = this.queueableContextHistory ??
+ new List<QueueableContext>();
+ this.queueableContextHistory.add(context);
+ Object nextDataPassthrough = execute();
+ Finalizer nextStep = new EnqueueNextQueueableProcessStep(
+ this.processSteps,
+ nextDataPassthrough,
+ this.queueableContextHistory,
+ this.finalizerContextHistory
+ );
+ System.attachFinalizer(nextStep); |
||
| // invoke the abstract method `execute`. see the comment above. | ||
| execute(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
createSObjectsmethod call has been updated with new parameters. Ensure that the new parameters (null,false,true) are correctly passed and they align with the method's expectations. If these parameters have default values, consider using the method overloading principle to avoid passing them explicitly each time.Please also ensure that the changes do not break any existing functionality or tests.