Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support async handling and add CronJob status tracking #894

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Zamoca42
Copy link

@Zamoca42 Zamoca42 commented Sep 4, 2024

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 isRunning 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

스크린샷 2024-09-03 오후 7 10 58

Related Issue

Closes #713
Closes #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)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • 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).

# Improve async handling and add status tracking

This commit introduces the following changes:

1. Change the `fireOnTick()` method to return a `Promise` to handle asynchronous
   callbacks more effectively.
2. Add a new `isRunning` property to track the running state of the `CronJob`
   instance.
3. Update the test suite to use the new asynchronous behavior and track the
   running state of the job.

These changes ensure that the `CronJob` class can properly handle asynchronous
callbacks, and provide a way to track the running state of the job, which can
be useful for various use cases.
…outs

# Short Description

The changes in this commit remove repeated `callback` variables from the cron tests and adjust the test timeouts to use the `tickAsync` method instead of `tick`.

## Detailed Changes

1. Removed repeated `callback` variables from several test cases.
2. Adjusted the test timeouts to use the `tickAsync` method instead of `tick` to ensure the tests properly wait for the asynchronous operations to complete.
3. Removed the `done` callback from the test cases and instead used the `async` function syntax to handle the asynchronous nature of the tests.

## Motivation

These changes improve the readability and maintainability of the cron tests by removing unnecessary variables and ensuring the tests properly handle the asynchronous nature of the CronJob functionality.
This commit introduces a new test case to ensure that the `CronJob` class waits for the running callback to complete before stopping the job.

The changes include:

- Adding a new test case `should wait for running callback to complete before stopping` that simulates a long-running callback and verifies that the job is not stopped until the callback has finished executing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant