Background Functions

Contrary to the Queue’s status-quo of Producers and Consumers, Defer brings the concept of Background Functions as a basic unit of work:

defer/importContacts.ts
import { defer } from "@defer/client";

async function importContacts(userId: string) {
  // ... import contacts from Hubspot
}

export default defer(importContacts, { concurrency: 2, retry: 2 });

About the defer/ folder

All files inside the defer/ folder must be Background Functions (wrapped with defer()); any shared code (ex: utils) should be moved outside of the defer/ folder.

The defer/ folder can be placed anywhere in your project and can contain nested sub-folders.

Enqueuing an execution is achieved by simply calling Background Functions (Producer pattern):

api.ts
import express from "express";

import importContacts from "./defer/importContacts";

const app = express();

app.get("/", (req, res) => {
  // calling a background function triggers an execution on Defer Platform
  await importContacts(req.session.userId);

  res.send("Import started!");
});

app.listen(3000);

Calling a Background Function (await importContacts();) triggers a call to the Defer API to enqueue an Execution on The Defer Platform.

The lifecycle of a Background Function call.

Builds & Executions

Builds

Defer builds your Background Function into optimized bundles to run pending Executions in isolated containers.

A Defer Build extracts the Background Function defined in your project with their configuration:

Background Functions identified in a project.

Executions

Executions get processed based on their schedule and available account concurrency capacity.

Each Execution benefits from dedicated resources and rich runtime supporting native dependencies and local imports.

Finally, Executions comes with detailed timings, logs in the Console, and quick actions such as rerun or cancel:

A successful execution with logs and detailed timings.