-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
400 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/dartness_server/lib/src/configuration/schedule/cron_expression.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/// A class representing a cron expression, used for scheduling tasks. | ||
class CronExpression { | ||
final List<String> seconds; | ||
final List<String> minutes; | ||
final List<String> hours; | ||
final List<String> days; | ||
final List<String> months; | ||
final List<String> weekdays; | ||
|
||
CronExpression({ | ||
required this.seconds, | ||
required this.minutes, | ||
required this.hours, | ||
required this.days, | ||
required this.months, | ||
required this.weekdays, | ||
}); | ||
|
||
/// Parses a cron string into its components. | ||
/// | ||
/// The cron string must have 6 parts, separated by spaces, representing: | ||
/// seconds, minutes, hours, day of month, month, and day of week. | ||
factory CronExpression.parse(String cronString) { | ||
final parts = cronString.split(' '); | ||
if (parts.length != 6) { | ||
throw FormatException('Invalid cron string length'); | ||
} | ||
|
||
// Validate and parse each field | ||
return CronExpression( | ||
seconds: _parseField(parts[0], 0, 59), | ||
minutes: _parseField(parts[1], 0, 59), | ||
hours: _parseField(parts[2], 0, 23), | ||
days: _parseField(parts[3], 1, 31), | ||
months: _parseField(parts[4], 1, 12), | ||
weekdays: _parseField(parts[5], 0, 7), | ||
); | ||
} | ||
|
||
static List<String> _parseField(String field, int min, int max) { | ||
// Wildcard | ||
if (field == '*') { | ||
return [field]; | ||
} | ||
|
||
// Step values | ||
if (field.contains('/')) { | ||
final stepParts = field.split('/'); | ||
if (stepParts[0] != '*' && | ||
!stepParts[0].contains('-') && | ||
int.tryParse(stepParts[1]) == null) { | ||
throw FormatException('Unsupported or malformed step value'); | ||
} | ||
if (stepParts[0] == '*' && int.parse(stepParts[1]) > max) { | ||
throw FormatException('Step value out of range'); | ||
} | ||
return [field]; | ||
} | ||
|
||
// Range values | ||
if (field.contains('-')) { | ||
final rangeParts = field.split('-').map(int.parse).toList(); | ||
if (rangeParts[0] > rangeParts[1]) { | ||
throw FormatException('Invalid range'); | ||
} | ||
if (rangeParts.any((val) => val < min || val > max)) { | ||
throw FormatException('Range value out of bounds'); | ||
} | ||
return [field]; | ||
} | ||
|
||
// List values | ||
final listParts = field.split(','); | ||
for (var part in listParts) { | ||
final value = int.tryParse(part); | ||
if (value == null || value < min || value > max) { | ||
throw FormatException('Value out of bounds'); | ||
} | ||
} | ||
return listParts; | ||
} | ||
|
||
@override | ||
String toString() { | ||
return 'CronExpression(seconds: $seconds, minutes: $minutes, hours: $hours, days: $days, months: $months, weekdays: $weekdays)'; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/dartness_server/lib/src/configuration/schedule/cron_scheduler.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import 'dart:async'; | ||
|
||
import 'cron_expression.dart'; | ||
import 'system_time_provider.dart'; | ||
import 'time_provider.dart'; | ||
|
||
/// Scheduler class that uses a [CronExpression] to execute tasks periodically. | ||
class CronScheduler { | ||
final CronExpression cronExpression; | ||
// TimeProvider allows for injecting a custom time source, facilitating testing. | ||
final TimeProvider timeProvider; | ||
Timer? _timer; | ||
|
||
// Constructor accepts a cron expression and an optional custom TimeProvider. | ||
// If no TimeProvider is provided, it defaults to the system clock. | ||
CronScheduler(this.cronExpression, {TimeProvider? timeProvider}) | ||
: timeProvider = timeProvider ?? SystemTimeProvider(); | ||
|
||
/// Starts the periodic execution of a task based on the cron expression. | ||
/// [task] is a function with no parameters that encapsulates | ||
/// the invocation of the actual task function with its parameters. | ||
void start(void Function() task) { | ||
_timer = Timer.periodic(Duration(seconds: 1), (Timer t) { | ||
var now = timeProvider.now(); | ||
// Checks if the current time matches the cron expression before executing the task. | ||
if (_matches(now)) { | ||
task(); | ||
} | ||
}); | ||
} | ||
|
||
/// Checks if the current time matches the cron expression. | ||
bool _matches(final DateTime dateTime) { | ||
return (cronExpression.seconds.contains('*') || | ||
cronExpression.seconds.contains(dateTime.second.toString())) && | ||
(cronExpression.minutes.contains('*') || | ||
cronExpression.minutes.contains(dateTime.minute.toString())) && | ||
(cronExpression.hours.contains('*') || | ||
cronExpression.hours.contains(dateTime.hour.toString())) && | ||
(cronExpression.days.contains('*') || | ||
cronExpression.days.contains(dateTime.day.toString())) && | ||
(cronExpression.months.contains('*') || | ||
cronExpression.months.contains(dateTime.month.toString())) && | ||
(cronExpression.weekdays.contains('*') || | ||
cronExpression.weekdays.contains(dateTime.weekday.toString())); | ||
} | ||
|
||
/// Stops the periodic execution of the task. | ||
void stop() { | ||
_timer?.cancel(); | ||
} | ||
} |
8 changes: 5 additions & 3 deletions
8
packages/dartness_server/lib/src/configuration/schedule/scheduled.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
/// Represents an annotation for scheduling task | ||
import 'package:dartness_server/src/configuration/schedule/time_unit.dart'; | ||
|
||
/// Represents an annotation for scheduling tasks with various parameters. | ||
class Scheduled { | ||
/// A cron-like expression defining the triggers for the scheduled method. | ||
final String cron; | ||
final String? cron; | ||
|
||
/// Constructor for the Scheduled annotation. | ||
const Scheduled({ | ||
required this.cron, | ||
this.cron, | ||
}); | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/dartness_server/lib/src/configuration/schedule/system_time_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import 'time_provider.dart'; | ||
|
||
// Default implementation of TimeProvider using the system clock. | ||
class SystemTimeProvider implements TimeProvider { | ||
@override | ||
DateTime now() => DateTime.now(); | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/dartness_server/lib/src/configuration/schedule/time_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Abstract class defining the interface for a time provider. | ||
abstract class TimeProvider { | ||
// Method that should return the current time. | ||
DateTime now(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ dev_dependencies: | |
http: ^1.2.1 | ||
lints: ^4.0.0 | ||
test: ^1.25.7 | ||
build_runner: ^2.4.11 | ||
build_runner: ^2.4.11 |
Oops, something went wrong.