Skip to main content

Jobs

Jobs are scheduled events in Ductape that allow you to execute tasks at a specified time, at intervals, or both. Any previously created product event types can be scheduled as jobs, including actions, notifications, database_actions, and functions.

Creating Jobs

To create a job, use the create function of the product.jobs interface:

import ductape from './ductapeClient'
import { JobEventTypes, IProductJob } from "ductape-sdk/dist/types";

const details: IProductJob = {
name: 'Example Job', // name of job
tag: 'example_job', // unique job identifier
event_type: JobEventTypes.ACTION, // event type to execute
event_tag: 'action_tag', // tag of the event to run
executions: 5, // set to 0 for unlimited executions
intervals: 60000, // interval in milliseconds
};
await ductape.product.jobs.create(details);
FieldTypeDescription
namestringThe name of the job, used for identification and logging.
tagstringA unique identifier for the job, used to manage and retrieve specific jobs.
event_typeJobEventTypes enumThe type of event to execute (e.g., ACTION, NOTIFICATION).
event_tagstringThe tag for the event being executed, corresponding to the specific event type chosen.
executionsnumberThe number of times to execute the job. Set to 0 for unlimited executions.
intervalsnumber (milliseconds)The interval, in milliseconds, between each job execution.

Job Event Types

The JobEventTypes enum specifies the type of events you can schedule as jobs.

JobEventTypesDescription
ACTIONExecutes an action event.
NOTIFICATIONExecutes a notification event.
DATABASE_ACTIONExecutes a database action.
FUNCTIONExecutes a cloud function.
MESSAGEBROKERExecutes a message broker event emit

Updating Jobs

To update an existing job, use the updateJob function, providing the job tag and the updated details:

const tag = 'example_job';
const data: Partial<IProductJob> = {
name: 'Updated Job Name', // optional updated name
executions: 10, // optional updated execution limit
intervals: 120000, // optional updated interval
};
await ductape.product.jobs.update(tag, data);

Fetching Jobs

To retrieve a list of all jobs, use the fetchAll function:

const jobs = await ductape.product.jobs.fetchAll(); // returns an array of all jobs

To fetch a specific job, use the fetch function with the job tag:

const tag = 'example_job';
const job = await ductape.product.jobs.fetch(tag); // retrieves a specific job by tag

These functions provide flexibility for creating, updating, and managing scheduled events across your Ductape applications.