0.3.5 ā€¢ Published 1 year ago

simplejob v0.3.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

simplejob

A tool class to wrap your jobs/scripts and easily get logs and reports like šŸ‘‡

[12:11:49] šŸš€ Job started...
[12:11:49] Error on user [42]
[12:11:49] Error while fetching date
[12:11:49] āœ… Job done.
+------------------- Job report ---------------------+
šŸ‘· Job >  testJob
šŸ“ Path > /Users/alex/Documents/perso/simplelogs-package/src/testJob.ts
šŸš¦ Status >  warning
ā° Duration >  0.004s
šŸ’¬ Args >
        - startDate: "2021-10-10"
        - endDate: "2023-10-10"
šŸ“Š Results >
        - notificationSent: 42
šŸš© Errors >
        - Error on user [42]
        - Error while fetching date
+------------------------------------------------------+

Installaton

yarn add simplejob
# or
npm i -S simplejob
import { SimpleJob, JobStatus } from "simplejob";

class MyJob extends SimpleJob {
  connect = async () => {
    // your database connection
  };

  disconnect = async () => {
    // your database connection
  };

  onEnd = async (status: JobStatus, crashError?: any) => {
    // your custom actions
  };
}

Usage

Create your job

const job = new MyJob({
  maintainer: "John Doe",
  filename: __filename,
});

job.start(async () => {
  // Your job operations...
});

Adding results/errors/logs

job.addResult("userMigrated"); // Adding 1 result.
job.addResult("userMigrated", 42); // Adding 42 results.

job.addError(`Error on user ${user.id}`, { userId: user.id }); // Adding an error with data.

job.addLog(`${user.id} migrated`);

Getting args

getArgs helps you getting args params, logging errors and usage using Joi.

const args = job.getArgs<{
  country: "france" | "usa";
  city?: string;
  confirm?: boolean;
}>(
  {
    country: Joi.string().valid("usa").valid("france").required(),
    city: Joi.string(),
  },
  {
    confirm: Joi.boolean(),
  }
);

will log

> node myjob germany orleans
"country" is invalid
Usage: "node myjob <country> [city] [--confirm]"

šŸ’” Note: for now, compatible Joi package is "17.4.0"

Exporting csv

await job.exportCsv(`${__dirname}/data/user_data.csv`, [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
]);

Will write a csv file using writeStream (create path if it doesn't exist).

Incoming...

This package doc and Class is still in construction, some features are coming in next versions! fork children, getArgs without Joi, exportJson, saveReport, customizations...

API

šŸš§ In construction...

SimpleJob Class api

propertydescriptiontypedefault
connectConnect function called before job executionasync () => void
disconnectDisconnect function called after the job executionasync () => void
onEndFunction called after the job executionasync (status: JobStatus, crashError?: any) => void
startStartasync (async () => any) => void
statusActual status of the job instance'pending' | 'running' | 'warning' | 'success' | 'error' | 'exit' | 'pending'
timeformatFormat of time to be displayed in logsdayjs format'hh:mm:ss'
envEnvironment of the jobstring | undefinedprocess.env.NODE_ENV
getArgsGet args and return an object of it(params, namedParams) => objectfunction
exportCsvExport csv data in a file (create dir)(path, data) => voidfunction
addErrorAdd an error(message: string, data: any) => voidfunction
addLogAdd a log(message: string, data: any) => voidfunction
argsArguments object filled with .getArgsobject{}
logsLogs filled with .addError and .addLogJobLog[][]
resultsResults filled with .addResultJobResult[][]
reportErrorsLimitLimit of errors to be displayed in the reportnumber6
disableReportDisable the report at the end of the jobbooleanfalse
disableConnectDisable the connect/disconnect methods at the start/end of the jobbooleanfalse
maintainerName of the maintainerstring | undefined
scriptNameName of the file (deducted from constructor option filename)string
scriptPathFull path (equal from constructor option filename)string
descriptionDescription of the jobstring
maintainername of the author/maintainerstring
startedAtDate of the start of the jobDate
endedAtDate of the end of the jobDate
startedAtTimestampTimestamp of the start of the jobnumber
endedAtTimestampTimestamp of the end of the jobnumber
simplelogsTokenToken to be used to send logs to simplelogs (incoming)string | undefined
tagsTags to be used to send logs to simplelogs (incoming)string[] | undefined
threadThread for simplelogs app (incoming)string

SimpleJob constructor options

Options used with constructor call šŸ‘‰ const job = new SimpleJob(options)

propertydescriptiontyperequired
filenameFull path of the file (__filename)stringx
0.1.10

1 year ago

0.1.11

1 year ago

0.1.12

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.8

1 year ago

0.3.5

1 year ago

0.1.7

1 year ago

0.1.9

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.4

1 year ago

0.1.6

1 year ago

0.3.3

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago