GithubHelp home page GithubHelp logo

dalpuche / salesforce-apex-retry Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jantaks/salesforce-apex-retry

0.0 0.0 0.0 30 KB

Retry framework for Salesforce applications

License: BSD 3-Clause "New" or "Revised" License

Apex 100.00%

salesforce-apex-retry's Introduction

Salesforce APEX Retry framework

This is an easy-to-use, yet powerful framework to add retry logic to your classes. I wrote it specifically for webservice callouts but it can be used for other (distributed) jobs that have some chance of failing.

A custom object provides the ability to monitor the state of your retryable jobs. For instance, you can quickly find the last result and the next scheduled retry.

It is easy to implement your own retry schedule by overriding the provided base schedule. Schedules are set in minutes after the first execution. For example, if the first execution was at 8:00 and you have set your schedule to {1,5,10,30} the first retry will be close to 8:01, the second retry close to 8:05, the third close to 8:10 and so forth.

The retries are selected in a batch job. The exact timing of the retries depends on how frequent the batch job is run. By default, the batch runs every 5 minutes.

I would love to hear your feedback and suggestions for improvement.

How to use

Simply extend the abstract class Retryable and implement protected abstract JobResult startJob();. If the status of JobResult is FAILED_RETRY the framework will retry the job at the specified interval.

Retryable implements Queueable so your job should be run asynchronously. For the example below this means:

System.enqueueJob(new SomeCalloutRetryable('"Post":"This is my Post"'));

The project has 100% test coverage. For more implementation details see the Test classes.

Example implementation:

public with sharing class SomeCalloutRetryable extends Retryable {

    private final String body;

    public SomeCalloutRetryable(String body) {
        this.body = body;
        retryScheduleInMinutes = new List<Integer>{1, 5, 25, 60, 2*60}; //OVERRIDE DEFAULT RETRY SCHEDULE
    }

    public override JobResult startJob() {
        log.d('Started MockCallout');
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('callout:YOUR_SERVICE_ENDPOINT');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json');
        request.setHeader('Accept', 'application/json');
        request.setBody(this.body);
        HttpResponse response = http.send(request);
        Retryable.Status retryStatus = Utils.httpRetryScenario(response.getStatusCode(), response.getStatus());
        return new JobResult(retryStatus, response.getBody());
    }
}

salesforce-apex-retry's People

Contributors

jantaks avatar

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.