Savory / Danet

The most mature backend framework for Deno. Create awesome HTTP and WebSocket server as well as KVQueue workers !
https://danet.land
Other
269 stars 18 forks source link

Feature: Task Scheduling #79

Closed marco-souza closed 7 months ago

marco-souza commented 7 months ago

Description

Task scheduling allows you to schedule arbitrary code (methods/functions) to execute at a fixed date/time, at recurring intervals, or once after a specified interval. In the Linux world, this is often handled by packages like cron at the OS level. For Deno apps, we have a native module (yet in unstable) called Deno.cron, to provide cron-like functionality

References:


Issue Ticket Number

Fixes #78


Type of change


Checklist:

codecov-commenter commented 7 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (8e7c930) 86.65% compared to head (bff9373) 87.53%.

:exclamation: Current head bff9373 differs from pull request most recent head 18bffda. Consider uploading reports for the commit 18bffda to get more accurate results

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #79 +/- ## ========================================== + Coverage 86.65% 87.53% +0.87% ========================================== Files 54 59 +5 Lines 1664 1781 +117 Branches 176 181 +5 ========================================== + Hits 1442 1559 +117 Misses 220 220 Partials 2 2 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

marco-souza commented 7 months ago

@Sorikairox I plan to extract part of this logic into a ScheduleRegistry to leave the module file cleaner, but the main business logic is ready for review, please take a look when you have the time! 🙏🏼

Sorikairox commented 7 months ago

@marco-souza Overall it looks good !

Not sure an additional "Registry" class is necessary for now, the code is pretty simple and will probably not evolve that much 😁

There are improvements on test indeed 😭 Maybe Deno has something to fake timers. Or we can test with 1 seconds timeouts, intervals and cron. It shouldn't matter if it's a minute or a few seconds, internal behaviours is the same 🤔

marco-souza commented 7 months ago

@Sorikairox Thanks for the review

Regarding the Registry class, I considered creating it to move all this registration logic from this module, but I agree this registry might not be needed, as we only use it here.

About the test cases, testing timeout and intervals will be simple, but the issue with cronjob is that Deno.cron is based on POSIX crontab, which doesn't support seconds by default - for reference

IMO, if we test timeout and intervals, the registration logic will be tested, which is the same used from @Cron. Do you think it would be ok if I remove this test case and only validate @Interval & @Timeout?

Sorikairox commented 7 months ago

@marco-souza Thank for the explanation and reference. I learned something !

What about a special Deno task to run cron test ? This one can run in the CI (as it's free for open source projects) and our "normal" test task keep its speed ?

marco-souza commented 7 months ago

@Sorikairox Nice, I think that works fine!

But I was investigating about deno fake timer and I found this: https://deno.land/std@0.216.0/testing/time.ts?s=FakeTime

Let me try it first but if it doesn't work, I'll continue with the CI task approach.

marco-souza commented 7 months ago

@Sorikairox I was not able to use FakeTime for Deno.cron, as it uses an OS-level implementation, while the FakeTime only mocks the JS runtime tick.

Furtunatelly, I was able to mock Deno.cron, with a spy, and validated that Deno.cron is being called with the proper info 🥳

marco-souza commented 7 months ago

Yeah, the CronExpression comes partially from the Nest.js project itself, but the IntervalExpression enum is new. How should I put the license in this case?

Sorikairox commented 7 months ago

@marco-souza

A comment on top of the file // Copyright (c) 2017-2024 Kamil Mysliwiec MIT