denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.66k stars 5.25k forks source link

Add JS-friendly API for specifying Deno.cron schedule #21122

Closed igorzi closed 10 months ago

igorzi commented 11 months ago

Currently Deno.cron() 2nd argument is a string, which specifies the schedule using the cron format.

We'd like to extend this with a JS-friendly API, which allows the schedule to be specified using Javascript object literals. The conversion should happen in https://github.com/denoland/deno/blob/main/ext/cron/01_cron.ts.

Here are some examples:

Exact

Deno.cron("cron", "1 * * * *", ..);

becomes:

Deno.cron("cron", { minute: { exact: 1 } }, ..);
// Or
Deno.cron("cron", { minute: 1 }, ..);

Every nth

Deno.cron("cron", "0 7/2 * * *", ..);

becomes:

Deno.cron("cron", { minute: 0, hour: { start: 7, every: 2 } }, ..);

List of schedules

Deno.cron("cron", "0 0 * * 1,3,5", ..);

becomes:

Deno.cron("cron", { minute: 0, hour: 0, dayOfWeek: [ 1, 3, 5] }, ..);
Cedar-81 commented 11 months ago

I would like to handle this issue if it's okay by you. Still a beginner to open source though but I feel like I can try it out

Cedar-81 commented 11 months ago

While I wait to be assigned to this issue, I'd like to say I am currently working on this for anyone who might want to hop on as well. Thanks

igorzi commented 11 months ago

Thanks @Cedar-81

Cedar-81 commented 11 months ago

Quick questions can you give more information about the schedule string, you've stated that it can accept minute, hour, date_of_week, but there are 2 asterisks left. If there is any resource I can use to understand the schedule string better it would be really helpful cause I'd need to reconstruct the schedule string from the passed in object so I need to understand its format.

igorzi commented 11 months ago

It's a standard cron schedule string. See https://en.wikipedia.org/wiki/Cron.

Cedar-81 commented 11 months ago

okay, thanks.

fightbulc commented 10 months ago

on a side note there is a npm package which helps to abstract that string: https://www.npmjs.com/package/cron-time-generator

raashidanwar commented 10 months ago

I want to pick this task @igorzi.

Where exactly are we writing test cases for corn?

igorzi commented 10 months ago

Sounds good, @raashidanwar! Please check with @Cedar-81.

Current cron tests are here: https://github.com/denoland/deno/blob/main/cli/tests/unit/cron_test.ts

Cedar-81 commented 10 months ago

Sorry for not attending to this for so long, funny enough I have actually implemented this but I couldnt run tests because I had some problems setting up Rust and according to the Docs I needed to run all tests.

Feel free to go ahead as I am currently swamped with some other tasks. Thanks a lot for this opportunity and sorry again for not reaching out for so long.

raashidanwar commented 10 months ago

Hi @igorzi 👋 .

Here is the draft PR https://github.com/denoland/deno/pull/21340. Do have a look. I tried writing the test cases, but when I am running it using ./target/debug/deno test --unstable --allow-env cli/tests/unit/cron_test.ts somehow, it gave a type error that only string is expected, but I have tested it with a dummy code, and it is correctly parsing the JSON. would be glad if you could help it the test case issue.

raashidanwar commented 10 months ago

@igorzi I added the test cases. Do have a look at PR https://github.com/denoland/deno/pull/21340.