{"id":19491167,"url":"https://github.com/hramov/node-cron-worker","last_synced_at":"2025-07-10T06:38:28.868Z","repository":{"id":168913588,"uuid":"644733937","full_name":"hramov/node-cron-worker","owner":"hramov","description":"One more cron-based library for running cron tasks in separate worker threads","archived":false,"fork":false,"pushed_at":"2023-06-12T09:07:57.000Z","size":63,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-08T08:45:59.947Z","etag":null,"topics":["cronjob","library","typescript","worker-threads"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hramov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-24T06:33:35.000Z","updated_at":"2024-04-22T14:04:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"7edd2127-230d-4389-b71c-2ea0e71dba34","html_url":"https://github.com/hramov/node-cron-worker","commit_stats":null,"previous_names":["hramov/node-cron-worker"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hramov%2Fnode-cron-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hramov%2Fnode-cron-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hramov%2Fnode-cron-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hramov%2Fnode-cron-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hramov","download_url":"https://codeload.github.com/hramov/node-cron-worker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240738091,"owners_count":19849546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cronjob","library","typescript","worker-threads"],"created_at":"2024-11-10T21:16:01.009Z","updated_at":"2025-02-25T19:44:04.973Z","avatar_url":"https://github.com/hramov.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Node Cron Worker Lib \n- Write cron jobs with typescript\n- Run jobs in parallel with worker_threads\n\n[Github repository](https://github.com/hramov/node-cron-worker)\n\nBased on [node-cron](https://www.npmjs.com/package/node-cron) scheduler + time parser (classic cron times like 0/5 * * * *) and [worker_threads](https://nodejs.org/api/worker_threads.html).\n\nTry this at your own risk! \u003ccode\u003enpm i cron-worker-threads\u003c/code\u003e\n\nExamples can be found in \u003ccode\u003e./example\u003c/code\u003e\n\n### Basic usage\n\u003cpre\u003e\u003ccode\u003econst options = {\n        timezone: 'Europe/Moscow',\n        poolMin: 1,\n        poolMax: 5,\n        logs: true, // show logs in console or not\n    }\n\n    const task = {\n        name: 'Job',\n        path: join(__dirname, 'jobs', 'job.ts'),\n        enabled: true,\n        cronTime: '*/2 * * * * *',\n        params: {\n            foo: 'bar'\n        },\n        runOnce: false, // if true stops job after first execution\n    };\n\n    const supervisor = new Supervisor([task], options);\n    supervisor.start();\u003c/code\u003e\u003c/pre\u003e\n\nYou can also add job to executing after starting the scheduler\n\u003cpre\u003e\u003ccode\u003e...\nsupervisor.addTask(task) // add one job\nsupervisor.addTasks([task]) // add multiple jobs\n...\n\u003c/code\u003e\u003c/pre\u003e\n\nIf job property \u003ccode\u003eenabled\u003c/code\u003e is set to \u003ccode\u003etrue\u003c/code\u003e, it's immediately added to execution.\n\nIf you want to use your custom logger, just pass it to Supervisor constructor after options\n\u003cpre\u003e\u003ccode\u003e...\nconst supervisor = new Supervisor([task], options, logger);\nsupervisor.start();\n...\u003c/code\u003e\u003c/pre\u003e\n\nLogger should implement \u003ccode\u003eILogger\u003c/code\u003e interface\n\u003cpre\u003e\u003ccode\u003eexport interface ILogger {\n    debug(message: string, context?: string): void;\n    info(message: string, context?: string): void;\n    warning(message: string, context?: string): void;\n    error(message: string, stack?: string, context?: string): void;\n}\u003c/code\u003e\u003c/pre\u003e\n\nDefault logger output\n\u003cpre\u003e\u003ccode\u003e{\"ts\":\"28.05.2023, 12:43:58\",\"level\":\"info\",\"message\":\"Start cron thread\"}\n{\"ts\":\"28.05.2023, 12:43:58\",\"level\":\"info\",\"message\":\"Send command to start jobs\"}\n{\"ts\":\"28.05.2023, 12:43:58\",\"level\":\"info\",\"message\":\"Cron scheduler is online\"}\n{\"ts\":\"28.05.2023, 12:44:00\",\"level\":\"info\",\"message\":\"Task Job scheduled\"}\n{\"ts\":\"28.05.2023, 12:44:00\",\"level\":\"info\",\"message\":\"Task Job moved to execution queue\"}\n{\"ts\":\"28.05.2023, 12:44:00\",\"level\":\"info\",\"message\":\"Worker d19af155-04ac-479b-a1cf-69204f6c5f25 online\"}\n{\"ts\":\"28.05.2023, 12:44:00\",\"level\":\"info\",\"message\":\"Worker 6fab4a75-cf71-45fe-b22c-eacf37c7f395 online\"}\n\u003c/code\u003e\u003c/pre\u003e\n\n### Job communication with parent thread\n1) To indicate that job is finished successfully, just return something you want to see in logs that describes job's finish\n2) To indicate an error, throw an exception inside \u003ccode\u003erun\u003c/code\u003e function with error description\n\n\u003cpre\u003e\u003ccode\u003eexport async function run(params: any) {\n    const result = //... your code\n    if (error) {\n        throw new Error(error);\n    }\n    return result;\n}\u003c/code\u003e\u003c/pre\u003e\n\n### Worker status\nYou can easily get worker status by invoking supervisor method \u003ccode\u003egetStat()\u003c/code\u003e\n\u003cpre\u003e\u003ccode\u003e...\nsupervisor.start();\nsetInterval(async () =\u003e {\n    const stat = await supervisor.getStat();\n    console.table(stat)\n}, 1000);\n...\n\u003c/code\u003e\u003c/pre\u003e\n\nIt returns object with following fields:\n1) poolSize - count of active/waiting worker threads\n2) taskPoolSize - count of tasks in processing queue\n3) taskProcessing - count of tasks currently processing by workers\n\n## Should you use it?\n### Advantages\n - [X] Cron scheduler starts in separate thread and thus doesn't affect main thread and doesn't depend on its event loop  \n - [X] Every cron job implements as a separate TypeScript file that allows you to use all power of typing\n - [X] Every cron job starts in its own thread that allows you to perform complex calculations and CPU intensive tasks with no effect on your server performance\n - [X] Get worker status at every moment of execution\n\n### Disadvantages\n- [X] Still in alpha\n- [X] No tests\n- [X] Not all bugs are found","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhramov%2Fnode-cron-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhramov%2Fnode-cron-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhramov%2Fnode-cron-worker/lists"}