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

Fix scheduling issue #41 #42

Merged
merged 1 commit into from Nov 13, 2022
Merged

Fix scheduling issue #41 #42

merged 1 commit into from Nov 13, 2022

Conversation

ccl0326
Copy link
Contributor

@ccl0326 ccl0326 commented Nov 10, 2022

No description provided.

lib/cron.dart Outdated
@@ -47,6 +47,24 @@ class Schedule {
/// The weekdays a Task should be started.
final List<int>? weekdays;

/// The second a Task last run.
int? lastSecond = -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these fields should be private (_lastSecond) and if you initialize them, they can be int instead of int?. e.g. int _lastSecond = -1;.

lib/cron.dart Outdated
final currentDay = (days == null) ? days : time.day;
final currentMonth = (months == null) ? months : time.month;
final currentWeekday = (weekdays == null) ? weekdays : time.weekday;
if (currentSecond == lastSecond &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the condition expression a separate variable and add comment on what that means...

// comment
final xyz = currentSecond == lastSecond && .... ;
if (xyz) {
  return false;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    if ((seconds == null || lastTime.second == time.second) &&
        (minutes == null || lastTime.minute == time.minute) &&
        (hours == null || lastTime.hour == time.hour) &&
        (days == null || lastTime.day == time.day) &&
        (months == null || lastTime.month == time.month) &&
        (weekdays == null || lastTime.weekday == time.weekday)) {
      return false;
    }

I think there is no need to comment here, the condition means the whole logical. seconds equals null means any second can trigger cron, so no matter seconds equals null or second match last datatime means the condition for the task has been ran.

lib/cron.dart Outdated
currentDay == lastDay &&
currentMonth == lastMonth &&
currentWeekday == lastWeekday) return false;
lastSecond = currentSecond as int?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the cast is not needed here.

@isoos
Copy link
Contributor

isoos commented Nov 10, 2022

I'm also wondering if we could just store the DateTime time field and compare it without the sub-second parts...

@ccl0326
Copy link
Contributor Author

ccl0326 commented Nov 10, 2022

I'm also wondering if we could just store the DateTime time field and compare it without the sub-second parts...

i have pushed new commit with datetime.

Copy link
Contributor

@isoos isoos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the check per the comment, and also update the CHANGELOG.md following the pattern to reference the PR, and also please bump the version. Thank you!

lib/cron.dart Outdated
@@ -47,6 +47,9 @@ class Schedule {
/// The weekdays a Task should be started.
final List<int>? weekdays;

/// The datetime a Task last run.
DateTime lastTime = DateTime(0, 0, 0, 0, 0, 0, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it more, I think this field shouldn't be here, rather it should be moved to _ScheduledTask.
The check should be in

  void tick(DateTime now) {
    if (_closed) return;
    if (!schedule.shouldRunAt(now)) return;
// this is the new part:
   if (<big-condition>) {
    return;
   }
    _run();
  }

The reason being: Schedule instances may be shared even between Cron instances, and we could get parallel updates. _ScheduledTask is private to the Cron instance and it is not shared, it would be the correct place to put this field in.

@ccl0326
Copy link
Contributor Author

ccl0326 commented Nov 11, 2022

@isoos all done.

@isoos isoos changed the title Fix issue #41 Fix scheduling issue #41 Nov 11, 2022
@isoos
Copy link
Contributor

isoos commented Nov 11, 2022

I think this is almost ready to be merged, however, two things: the CI tests are failing (test/cron_test.dart) - could you please take a look on why that is (I guess the '* * * * *' pattern makes a special case)?

Also, please add this to the top of the changelog:

## 0.5.1

- Fixed scheduling issue. [#42](https://github.com/agilord/cron/issues/42) by [ccl0326](https://github.com/ccl0326)

@ccl0326 ccl0326 force-pushed the clchen_dev branch 2 times, most recently from d01ed02 to 096a1a6 Compare November 12, 2022 14:04
@ccl0326
Copy link
Contributor Author

ccl0326 commented Nov 12, 2022

@isoos all done.

@isoos
Copy link
Contributor

isoos commented Nov 13, 2022

there are still a few tests failing, but I'll fix them after merging

@isoos isoos merged commit 0e17cd8 into agilord:master Nov 13, 2022
@isoos
Copy link
Contributor

isoos commented Nov 13, 2022

Published as 0.5.1.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants