Skip to content

Commit

Permalink
Fix issue #29 (#34)
Browse files Browse the repository at this point in the history
* Fix #21

* Fix #29

* updated changelog

* async main needs to return Future<void>

Co-authored-by: István Soós <isoos@users.noreply.github.com>
  • Loading branch information
myConsciousness and isoos committed Apr 10, 2022
1 parent edb8bcb commit fe95433
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## 0.5.0

- Improved time parsing. [#21](https://github.com/agilord/cron/issues/21)
- Improved time parsing. [#21](https://github.com/agilord/cron/issues/21) by [myConsciousness](https://github.com/myConsciousness)
- Improved exception that occurred when parsing cron-like string. Currently throws `ScheduleParseException` if parsing fails. [[#29](https://github.com/agilord/cron/issues/29)] by [myConsciousness](https://github.com/myConsciousness)

## 0.4.1

Expand Down
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
# A cron-like time-based job scheduler for Dart
# A cron-like time-based job scheduler for Dart

Run tasks periodically at fixed times or intervals.

## Usage

A simple usage example:

import 'package:cron/cron.dart';

main() {
final cron = Cron();
cron.schedule(Schedule.parse('*/3 * * * *'), () async {
print('every three minutes');
});
cron.schedule(Schedule.parse('8-11 * * * *'), () async {
print('between every 8 and 11 minutes');
});
}
```dart
import 'package:cron/cron.dart';
main() {
final cron = Cron();
cron.schedule(Schedule.parse('*/3 * * * *'), () async {
print('every three minutes');
});
cron.schedule(Schedule.parse('8-11 * * * *'), () async {
print('between every 8 and 11 minutes');
});
}
```

## Links

Expand Down
15 changes: 11 additions & 4 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import 'package:cron/cron.dart';

Future<void> main() async {
final cron = Cron()
..schedule(Schedule.parse('*/6 * * * * *'), () {
final cron = Cron();

try {
cron.schedule(Schedule.parse('*/6 * * * * *'), () {
print(DateTime.now());
});
await Future.delayed(Duration(seconds: 20));
await cron.close();

await Future.delayed(Duration(seconds: 20));
await cron.close();
} on ScheduleParseException {
// "ScheduleParseException" is thrown if cron parsing is failed.
await cron.close();
}
}
2 changes: 2 additions & 0 deletions lib/cron.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:clock/clock.dart';

import 'src/constraint_parser.dart';

export 'package:cron/src/schedule_parse_exception.dart';

final _whitespacesRegExp = RegExp('\\s+');

/// A task may return a Future to indicate when it is completed. If it wouldn't
Expand Down
5 changes: 4 additions & 1 deletion lib/src/constraint_parser.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:cron/src/schedule_parse_exception.dart';

List<int>? parseConstraint(dynamic constraint) {
if (constraint == null) return null;
if (constraint is int) return [constraint];
Expand Down Expand Up @@ -33,5 +35,6 @@ List<int>? parseConstraint(dynamic constraint) {
}
}
}
throw Exception('Unable to parse: $constraint');

throw ScheduleParseException('Unable to parse: $constraint');
}
9 changes: 9 additions & 0 deletions lib/src/schedule_parse_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2022, Agilord. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

/// Exception thrown when a cron data does not have an expected
/// format and cannot be parsed or processed.
class ScheduleParseException extends FormatException {
/// Creates a new `FormatException` with an optional error [message].
ScheduleParseException([String message = '']) : super(message);
}
25 changes: 25 additions & 0 deletions test/src/schedule_parse_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:cron/src/schedule_parse_exception.dart';
import 'package:test/test.dart';

void main() {
test('Test throw without message', () {
try {
throw ScheduleParseException();
} catch (error) {
expect(error is ScheduleParseException, isTrue);
expect(error is FormatException, isTrue);
expect(error is Exception, isTrue);
}
});

test('Test throw with message', () {
try {
throw ScheduleParseException('parse failed');
} catch (error) {
expect(error.toString(), 'FormatException: parse failed');
expect(error is ScheduleParseException, isTrue);
expect(error is FormatException, isTrue);
expect(error is Exception, isTrue);
}
});
}

0 comments on commit fe95433

Please sign in to comment.