@tshio/scheduler-client v0.0.2
RAD Scheduler Client
Non-blocking RAD Scheduler client for Node.js.
This is a 100% JavaScript library, with TypeScript definition, with the Promise API.
This module makes it simple to implement a Node.js application that uses RAD Scheduler.
Table of Contents
Installing
$ npm install @tshio/scheduler-client
or
$ yarn add @tshio/scheduler-client
Loading and configuration module
// CommonJS
const { SchedulerClient } = require('@tshio/scheduler-client');
// ES Module
import { SchedulerClient } from '@tshio/scheduler-client';
const options = {
host: "localhost",
port: "50070",
}
const schedulerClient = new SchedulerClient(options);
Examples
const SchedulerClient = require('@tshio/scheduler-client');
const schedulerClient = new SchedulerClient({
host: "localhost",
port: 50070,
});
(async () => {
// Add job
const job = {
name: "JobExample",
type: "http",
payload: {
url: "example.com",
},
};
const { id } = await schedulerClient.jobs.addJob(job).catch();
// Get jobs
const jobsQueryFilter = {};
const jobs = await schedulerClient.jobs.getJobs(jobsQueryFilter);
// Cancel job
await schedulerClient.jobs.cancelJob({ jobId: id });
})();
API
schedulerClient.jobs.addJob({ name, type, payload?, jobOptions? }) => Promise<{ id }>
Schedule an action to another service - either to run immediately, at some specific timestamp or as a cron job.
Returns an object
{
id: string; // job id
}
or throw HttpError
Parameters
Name | Type | Description | Default |
---|---|---|---|
name | string | Job name | |
type | string | Job type (always "http") | |
payload | object | Job payload | |
payload.method | string | optional HTTP method, allowed values: GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH | |
payload.url | string | Request URL string | |
payload.headers | object | optional Request headers, example: { "Content-Type": "application/json" } | |
payload.body | object string | optional Request body, string or any of object | |
payload.options | object | optional Request options | |
payload.compress | boolean | optional Support gzip/deflate content encoding. false to disable | false |
payload.follow | number | optional Maximum redirect count. 0 to not follow redirect | 20 |
payload.size | number | optional Maximum response body size in bytes. 0 to disable | 0 |
payload.timeout | number | optional Request/response timeout in ms, it resets on redirect. 0 to disable (OS limit applies) | 0 |
jobOptions | object | optional Job configuration object | |
jobOptions.priority | number | optional Optional priority value. ranges from 1 (highest priority) to MAX_INT (lowest priority). Note that using priorities has a slight impact on performance, so do not use it if not required. | |
jobOptions.delay | number | optional An amount of milliseconds to wait until this job can be processed. Note that for accurate delays, both server and clients should have their clocks synchronized. | |
jobOptions.attempts | number | optional The total number of attempts to try the job until it completes. | 3 |
jobOptions.cron | string | optional Repeat job according to a cron specification. | |
jobOptions.cronStartDate | string | optional Start date when the repeat job should start repeating. Example: "2020-01-01 10:00:00" | |
jobOptions.cronEndDate | string | optional End date when the repeat job should stop repeating. Example: "2020-01-02 15:30:00" | |
jobOptions.cronTimeZone | string | optional Cron Timezone | |
jobOptions.cronLimit | number | optional Number of times the job should repeat at max. | |
jobOptions.backoff | number | optional Setting for automatic retries if the job fails. | 5000 |
jobOptions.lifo | boolean | optional If true, adds the job to the right of the queue instead of the left. | false |
jobOptions.timeout | number | optional The number of milliseconds after which the job should be fail with a timeout error. | |
jobOptions.removeOnComplete | boolean | optional If true, removes the job when it successfully completes. | |
jobOptions.removeOnFail | boolean | optional If true, removes the job when it fails after all attempts. | |
jobOptions.stackTraceLimit | number | optional Limits the amount of stack trace lines that will be recorded in the stacktrace. |
schedulerClient.jobs.getJobs( queryFilter? ) => Promise< object >
Get jobs list (if no query parameters it returns first 25 jobs ordered by name)
Returns an object
{
jobs: Job[];
page: number;
limit: number;
total: number;
}
interface Job {
id: string;
name: string;
type: JobType;
cron?: string;
status: JobStatus;
jobOptions?: JobOptions;
payload?: jsonB;
createdAt: Date;
updatedAt: Date;
}
or throw HttpError
Parameters
Name | Type | Description | Default |
---|---|---|---|
queryFilter | object | optionalQuery filter | |
queryFilter.page | number | optionalPage number | 1 |
queryFilter.limit | number | optionalResponse limit | 25 |
queryFilter.filter | number | optionalFilter object | |
queryFilter.query | number | optionalQuery object |
Filters can be used search for a single condition or they can be wrapped in logical operands AND and OR. Filtering can be a simple conditional evaluation of a single field.
//
export type GetJobsColumns = "id" | "name" | "status" | "createdAt" | "updatedAt";
export type GetJobsFilterOperators =
| "eq"
| "eqOr"
| "neq"
| "neqOr"
| "lt"
| "ltOr"
| "lte"
| "lteOr"
| "gt"
| "gtOr"
| "gte"
| "gteOr"
| "include"
| "includeOr"
| "in"
| "inOr";
export interface JobsQueryFilter {
page?: number;
limit?: number;
filter?: {
[column in GetJobsColumns]?: {
[operator in GetJobsFilterOperators]?: string;
};
};
order?: {
by: "id" | "name" | "status" | "createdAt" | "updatedAt";
type: "asc" | "desc";
};
}
filtercolumn = value
Name Type Description column string
Column name operator string
Operator name value string
ornumber
orboolean
(depending on thecolumn
type)Examples
Single parameter filter
filter: { name: { include: "job" } }
Two parameter filter
filter: { name: { include: "job" }, status: { eq: "active", }, }
order
Name Type Description Default by string
optional column name for order sorting, allowed values: "id", "name", "status", "createdAt", "updatedAt"
id
type asc
ordesc
optional Ascending or descending order asc
Examples
order: { by: "name", type: "desc" }
schedulerClient.jobs.cancelJob({ jobId }) => Promise< void >
Cancels a job with given id
Returns void or throw HttpError
Parameters
Name | Type | Description |
---|---|---|
jobId | string | Job ID |
License
This project is licensed under the terms of the MIT license.
About us:
The Software House