Skip to main content

Jobs

Jobs in Ductape are scheduled events that allow you to automate tasks at specific times or intervals. They are essential for running background processes, recurring tasks, notifications, database actions, and more. Jobs can execute any previously created product event type, including actions, notifications, database actions, functions, and message broker events.

A job is always associated with a specific event type and event tag. You can control how often a job runs, how many times it executes, and what event it triggers.

Creating a Job

To create a job, use the create function from the product.jobs interface. You must specify the job details, including the event type and tag to execute, and scheduling options.

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

const details: IProductJob = {
name: 'Example Job',
tag: 'example_job',
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);
FieldTypeRequiredDescription
namestringYesHuman-readable name for the job.
tagstringYesUnique identifier for the job.
event_typeJobEventTypes enumYesThe type of event to execute (see below).
event_tagstringYesTag of the event being executed.
executionsnumberYesNumber of times to execute the job. Set to 0 for unlimited executions.
intervalsnumber (milliseconds)YesInterval in milliseconds between each execution.

Note: Setting executions to 0 means the job will run indefinitely at the specified interval until deleted or updated.

Supported 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 a Job

To update an existing job, use the update function, providing the job tag and the updated details. You can change the name, executions, intervals, or event details as needed.

const tag = 'example_job';
const data: Partial<IProductJob> = {
name: 'Updated Job Name',
executions: 10,
intervals: 120000,
};
await ductape.product.jobs.update(tag, data);
Updatable FieldDescription
nameHuman-readable name for the job.
event_typeThe type of event to execute.
event_tagTag of the event being executed.
executionsNumber of times to execute the job.
intervalsInterval in milliseconds between each execution.

Tip: Use descriptive names and tags for jobs to clarify their purpose and make management easier.

Fetching Jobs

  • Fetch all jobs:
    const jobs = await ductape.product.jobs.fetchAll();
  • Fetch a specific job by tag:
    const job = await ductape.product.jobs.fetch('example_job');

Best Practices

  • Use descriptive names and tags for jobs to clarify their purpose.
  • Set intervals and executions thoughtfully to avoid overloading your system.
  • Use unlimited executions (executions: 0) only for jobs that should run continuously.
  • Regularly review and update job definitions as your integration needs evolve.
  • Use the same naming conventions for tags and slugs across your product.
  • Monitor job execution and failures to ensure reliability.
  • Schedule jobs in non-overlapping intervals if they operate on shared resources.

See Also