@qrvey/scheduler
v0.1.1
Published
Library to schedule the datasync cronjobs
Downloads
1,867
Maintainers
Keywords
Readme
Qrvey scheduler
This is a library to use AWS Eventbridge, k8s scheduler or K8s jobs.
Installation
npm install @qrvey/scheduler
Require environment variables
AWS_ACCOUNT_ID; //AWS AccountId to send EventBridge service
AWS_DEFAULT_REGION; //AWS Region for EventBridge service
PLATFORM_TYPE; //Platform type (possible value CONTAINER)
CRONJOB_NAMESPACE //This env variable is required for k8s scheduler
Usage
Sample usage for scheduler
const { SchedulerService } = require("@qrvey/scheduler");
(async () => {
const scheduleParams = {
cronJobName: "multicloud-testcron",
scheduleExpression: "cron(*/1 * * * * *)",
description:
'{"userEmail":"[email protected]","datasetName":"DBName","appName":"AppName","readableCronExpr":"Every hour","owner":"ownerId","datasetId":"datasetId","userId":"userId","appId":"appId"}',
targetParams: {
Name: "demo_drScheduler_CJ_mra43sr0U",
Input: {
detail: {
appId: "appId",
userId: "userId",
owner: "owner",
datasetId: "datasetId",
cronSyncFrequency: "0 0/1 * * ? *",
version: 2.1,
},
source: "aws.events",
},
},
tags: [
{
Key: "version",
Value: "2.1",
},
{
Key: "datasetId",
Value: "datasetId",
},
{
Key: "appId",
Value: "appId",
},
{
Key: "userId",
Value: "userId",
},
{
Key: "state",
Value: "ENABLED",
},
{
Key: "owner",
Value: "userId",
},
],
targetResource: "arn:aws:lambda:{region}:{accountId}:function:{functionName}", //For ks8 scheduler the targetRosource will be the entire container url
state: "DISABLED",
};
const schedulerClient = new schedulerService();
const res = await schedulerClient.create(scheduleParams);
})();
Sample usage for tasks
Create task
const { TaskService } = require('@qrvey/scheduler');
const main = async () => {
try {
const namespace = 'service-app';
const jobManifest = {
taskName: 'task-name',
containerName: 'container-name',
description: 'this is to run a task',
image: `image_service_registry_container/image_name:latest`, // image to pull from registry container service
imagePullSecret: 'image-secret', // secret access to pull the image
environmentVariables: [ // env variables to pass for the job container
{ name: 'SERVER', value: 'some_prefix' },
{ name: 'NUMBER_OF_TYPES', value: 5 },
{ name: 'PLATFORM', value: 'AZURE' },
],
arguments: [ // arguments that you can get in the container
{ name: '--operation', value: 'getNames' },
{ name: '--id', value: 'mdK23ds' },
{ name: '--version', value: 'published' },
],
command: ['node', 'index.js'], // command to run
};
const newJob = new TaskService();
return await newJob.create(namespace, jobManifest);
/*
{
'annotations'?: {
[key: string]: string;
};
'creationTimestamp'?: Date;
'deletionGracePeriodSeconds'?: number;
'deletionTimestamp'?: Date;
'finalizers'?: Array<string>;
'generateName'?: string;
'generation'?: number;
'labels'?: {
[key: string]: string;
};
'managedFields'?: Array<V1ManagedFieldsEntry>;
'name'?: string;
'namespace'?: string;
'ownerReferences'?: Array<V1OwnerReference>;
'resourceVersion'?: string;
'selfLink'?: string;
'uid'?: string;
}
*/
} catch (error) {
console.error(error);
}
}
main();
Get Task
const { TaskService } = require('@qrvey/scheduler');
const main = async () => {
try {
const namespace = 'namespace';
const jobName = 'remove-something';
const getJob = new TaskService();
return await getJob.get(namespace, jobName);
/*
{
statusCode: 200,
metadata: {
'annotations'?: {
[key: string]: string;
};
'creationTimestamp'?: Date;
'deletionGracePeriodSeconds'?: number;
'deletionTimestamp'?: Date;
'finalizers'?: Array<string>;
'generateName'?: string;
'generation'?: number;
'labels'?: {
[key: string]: string;
};
'managedFields'?: Array<V1ManagedFieldsEntry>;
'name'?: string;
'namespace'?: string;
'ownerReferences'?: Array<V1OwnerReference>;
'resourceVersion'?: string;
'selfLink'?: string;
'uid'?: string;
},
status: {
'active'?: number;
'completedIndexes'?: string;
'completionTime'?: Date;
'conditions'?: Array<V1JobCondition>;
'failed'?: number;
'failedIndexes'?: string;
'ready'?: number;
'startTime'?: Date;
'succeeded'?: number;
'terminating'?: number;
'uncountedTerminatedPods'?: V1UncountedTerminatedPods;
},
}
*/
} catch (error) {
console.error(error);
}
}
main();
List Tasks
const { TaskService } = require('@qrvey/scheduler');
const main = async () => {
try {
const namespace = 'namespace';
const listJobs = new TaskService();
return await listJobs.list(namespace);
/*
[
"remove-orphan-data",
"migrate-database-data",
"api-data",
]
*/
} catch (error) {
console.error(error);
}
}
main();
Delete Task
const { TaskService } = require('@qrvey/scheduler');
const main = async () => {
try {
const namespace = 'namespace';
const jobName = 'remove-something';
const deleteJob = new TaskService();
return await deleteJob.delete(namespace, jobName);
/*
{
'apiVersion'?: string;
'code'?: number;
'details'?: V1StatusDetails;
'kind'?: string;
'message'?: string;
'metadata'?: V1ListMeta;
'reason'?: string;
'status'?: string;
}
*/
} catch (error) {
console.error(error);
}
}
main();