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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.5.1

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

## 0.5.0

- Improved time parsing. [#21](https://github.com/agilord/cron/issues/21) by [myConsciousness](https://github.com/myConsciousness)
Expand Down
12 changes: 12 additions & 0 deletions lib/cron.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,23 @@ class _ScheduledTask implements ScheduledTask {
Future? _running;
bool _overrun = false;

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

_ScheduledTask(this.schedule, this._task);

void tick(DateTime now) {
if (_closed) return;
if (!schedule.shouldRunAt(now)) return;
if ((schedule.seconds == null || lastTime.second == now.second) &&
(schedule.minutes == null || lastTime.minute == now.minute) &&
(schedule.hours == null || lastTime.hour == now.hour) &&
(schedule.days == null || lastTime.day == now.day) &&
(schedule.months == null || lastTime.month == now.month) &&
(schedule.weekdays == null || lastTime.weekday == now.weekday)) {
return;
}
lastTime = now;
_run();
}

Expand Down
3 changes: 2 additions & 1 deletion lib/src/constraint_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ List<int>? parseConstraint(dynamic constraint) {
if (constraint is int) return [constraint];
if (constraint is List<int>) return constraint;
if (constraint is String) {
if (constraint == '*' || constraint == '') return null;
if (constraint == '*') return List.generate(60, (i) => i);
if (constraint == '') return null;
final parts = constraint.split(',');
if (parts.length > 1) {
final items =
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: cron
description: A time-based job scheduler similar to cron. Run tasks periodically at fixed times or intervals.
version: 0.5.0
version: 0.5.1
homepage: https://github.com/agilord/cron

environment:
Expand Down