Git Product home page Git Product logo

apex-chainable's Introduction

apex-chainable

A framework for managing multiple asynchronous processes in Salesforce. Uses Queuable to chain multiple processes together.

Features

  • Allows sequential execution multiple asynchronous actions
  • Access to "Results" produced by executions further up the chain
  • Fault tolerant. Failures are tracked and chain can be reprocessed
  • Custom UI to view and debug executions

Example Use case

You need to perform multiple actions that cannot be completed in a single execution context. Each actions is dependant on the outcome of the previous action. If any part of the action fails, you need the ability to correct the errors (via data or metadata changes) and pick up where you left off.

Limitations

  • A ChainAction implemenations must be serializable!

Best Practices:

  • Do not serialize entire SObject and then run DML on them later. This may result in you overwriting fields with stale data. It is best to construct/query the SObject in the action itself if you need to run DML.

Usage

Extend ChainableAction

// Telephone action copies previous Telephone actions and appends it's own message to the response
// - Example of how an action can read response from previous actions
public class TelephoneAction extends ChainableAction {
    private String message;
    public TelephoneAction(String message) {
        this.message = message;
    }

    public override Type getType() {
        return TelephoneAction.class;
    }

    public override Type getResponseType() {
        return TelephoneResponse.class;
    }

    public override Object execute(Chainable chain) {
        //read past messages
        String s = '';
        for (ChainableLink link : chain.processedLinks) {
            if (link.completed) {
                TelephoneResponse prevResp = (TelephoneResponse) link.getResponse();
                s += prevResp.message;
            }
        }
        return new TelephoneResponse(s + message);
    }
}```

### Create and Run Chainable

```java
// Setup your "ChainLinks"
String[] words = new String[]{'Hello', 'World'};
for(Integer i = 0; i < words.size(); i++){
    String word  = words[i] + ' ';
    ChainableLink link = new ChainableLink(new TelephoneAction(word));
    links.add(link);
}

// Create the Chainable
Chainable chain = new Chainable(links);
String key = chain.key; //Save this somewhere if you want to check status / rerun

// enqueue execution
chain.enqueue();

Rerun any failures

Chainable.reprocessChain(key, 0, true);

Viewing Chains

There is a tab for queuable jobs which can be viewed.

apex-chainable's People

Contributors

chuckjonas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

apex-chainable's Issues

Add logging

I think it would be nice to have a way to log arbitrary messages about the execution which might help with debugging but aren't wanted in the Action Response.

We could do this by adding a base class method log(String msg) which would just append to a string and then save the result on the Execution Status (see issue #1).

Handle Limits

Due to the release of theBatchApexErrorEvent, there is a clear advantage in running this process in Batchable instead of Queuable.

The only existing functionality that might be tricky to recreate might be appendLinks(). Still, being able to properly handle limit exceptions is much more important then the abiblity to append actions mid-execution

Better Status tracking

Currently the status is just tracked directly on the Chainable_Link__c object. This makes it easy to access the result, but unfortunately complicates scenario's where a link gets run multiple times.

I think it would make more sense to track the status as a separate child object:

Chainable_Link_Execution__c

  • Chainable_Link: Master-Detail
  • Execution_Date
  • Status: Picklist
  • Error_Details:
  • Response: LONG Text String (or maybe even attachment?)
  • Response_Type*: ONLY if we decide that the response could be set dynamically at runtime. Otherwise the Chainable_Link__c would track the response type

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.