You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: support async handling and add CronJob status tracking (#894)
## Description
This PR improves async handling in the CronJob class and adds status
tracking functionality.
- Modified the `fireOnTick()` method to return a Promise for better
async callback handling
- Added an `isCallbackRunning` flag to track the running state of
CronJob instances
- Updated the test suite to use the new async behavior and track the
job's running state
- Added `waitForCompletion` functionality to the `job.stop()` method
- waits for running jobs to complete before executing the `onComplete`
callback
During test case writing, I encountered a type error with sinon.
To resolve this, added `sinon.restore()` to the `afterEach` block.
Reference:
https://stackoverflow.com/questions/73232999/sinon-cant-install-fake-timers-twice-on-the-same-global-object
<img width="811" alt="스크린샷 2024-09-03 오후 7 10 58"
src="https://github.com/user-attachments/assets/b87deee7-14b2-4407-8fea-a1cb469ef44b">
## Related Issue
Closes#713Closes#556
## Motivation and Context
These changes allow the CronJob class to handle asynchronous callbacks
more effectively and provide a way to track the running state of jobs.
## How Has This Been Tested?
- Updated existing test suite to verify the new async behavior
- Adjusted test timeouts to use the `tickAsync` method
- Added new test cases to check for proper waiting of running callbacks
before stopping
## Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
## Checklist:
- [x] My code follows the code style of this project.
- [x] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [ ] If my change introduces a breaking change, I have added a `!`
after the type/scope in the title (see the Conventional Commits
standard).
---------
Co-authored-by: Brandon der Blätter <intcreator@users.noreply.github.com>
Copy file name to clipboardexpand all lines: README.md
+19
Original file line number
Diff line number
Diff line change
@@ -194,6 +194,8 @@ day of week 0-7 (0 or 7 is Sunday, or use names)
194
194
195
195
-`unrefTimeout`: [OPTIONAL] - Useful for controlling event loop behavior. More details [here](https://nodejs.org/api/timers.html#timers_timeout_unref).
196
196
197
+
-`waitForCompletion`: [OPTIONAL] - If `true`, no additional instances of the `onTick` callback function will run until the current onTick callback has completed. Any new scheduled executions that occur while the current callback is running will be skipped entirely. Default is `false`.
198
+
197
199
#### Methods
198
200
199
201
-`from` (static): Create a new CronJob object providing arguments as an object. See argument names and descriptions above.
@@ -214,6 +216,23 @@ day of week 0-7 (0 or 7 is Sunday, or use names)
214
216
215
217
-`addCallback`: Permits addition of `onTick` callbacks.
216
218
219
+
#### Properties
220
+
221
+
-`isCallbackRunning`: [READ-ONLY] Indicates if a callback is currently executing.
222
+
223
+
```javascript
224
+
constjob=newCronJob('* * * * * *', async () => {
225
+
console.log(job.isCallbackRunning); // true during callback execution
226
+
awaitsomeAsyncTask();
227
+
console.log(job.isCallbackRunning); // still true until callback completes
228
+
});
229
+
230
+
console.log(job.isCallbackRunning); // false
231
+
job.start();
232
+
console.log(job.running); // true (schedule is active)
233
+
console.log(job.isCallbackRunning); // false (no callback executing)
0 commit comments