0.0.2 • Published 4 years ago

@tshio/scheduler-client v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

RAD Scheduler Client

npm version

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
NameTypeDescriptionDefault
namestringJob name
typestringJob type (always "http")
payloadobjectJob payload
payload.methodstringoptional HTTP method, allowed values: GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH
payload.urlstringRequest URL string
payload.headersobjectoptional Request headers, example: { "Content-Type": "application/json" }
payload.bodyobject stringoptional Request body, string or any of object
payload.optionsobjectoptional Request options
payload.compressbooleanoptional Support gzip/deflate content encoding. false to disablefalse
payload.follownumberoptional Maximum redirect count. 0 to not follow redirect20
payload.sizenumberoptional Maximum response body size in bytes. 0 to disable0
payload.timeoutnumberoptional Request/response timeout in ms, it resets on redirect. 0 to disable (OS limit applies)0
jobOptionsobjectoptional Job configuration object
jobOptions.prioritynumberoptional 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.delaynumberoptional 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.attemptsnumberoptional The total number of attempts to try the job until it completes.3
jobOptions.cronstringoptional Repeat job according to a cron specification.
jobOptions.cronStartDatestringoptional Start date when the repeat job should start repeating. Example: "2020-01-01 10:00:00"
jobOptions.cronEndDatestringoptional End date when the repeat job should stop repeating. Example: "2020-01-02 15:30:00"
jobOptions.cronTimeZonestringoptional Cron Timezone
jobOptions.cronLimitnumberoptional Number of times the job should repeat at max.
jobOptions.backoffnumberoptional Setting for automatic retries if the job fails.5000
jobOptions.lifobooleanoptional If true, adds the job to the right of the queue instead of the left.false
jobOptions.timeoutnumberoptional The number of milliseconds after which the job should be fail with a timeout error.
jobOptions.removeOnCompletebooleanoptional If true, removes the job when it successfully completes.
jobOptions.removeOnFailbooleanoptional If true, removes the job when it fails after all attempts.
jobOptions.stackTraceLimitnumberoptional Limits the amount of stack trace lines that will be recorded in the stacktrace.

Back to API

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
NameTypeDescriptionDefault
queryFilterobjectoptionalQuery filter
queryFilter.pagenumberoptionalPage number1
queryFilter.limitnumberoptionalResponse limit25
queryFilter.filternumberoptionalFilter object
queryFilter.querynumberoptionalQuery 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

    NameTypeDescription
    columnstringColumn name
    operatorstringOperator name
    valuestring or number or boolean (depending on the column type)

    Examples

    Single parameter filter

    filter: {
      name: {
        include: "job"
      }
    }

    Two parameter filter

    filter: {
      name: {
        include: "job"
      },
      status: {
        eq: "active",
      },
    }
  • order

    NameTypeDescriptionDefault
    bystringoptional column name for order sorting, allowed values: "id", "name", "status", "createdAt", "updatedAt"id
    typeasc or descoptional Ascending or descending orderasc

    Examples

    order: {
      by: "name",
      type: "desc"
    }

Back to API

schedulerClient.jobs.cancelJob({ jobId }) => Promise< void >

Cancels a job with given id

Returns void or throw HttpError

Parameters
NameTypeDescription
jobIdstringJob ID

Back to API

License

license

This project is licensed under the terms of the MIT license.

About us:

The Software House