@@ -54,12 +54,44 @@ When a message arrives on a rule's input channel, the Rules Engine:
54
54
55
55
### Scheduling
56
56
57
- Rules can be scheduled to run at specific times with various recurring patterns:
57
+ Rules can be scheduled to run at specific times with various recurring patterns. The scheduler works through several key components:
58
+
59
+ #### Schedule Structure
60
+ ``` go
61
+ type Schedule struct {
62
+ StartDateTime time.Time // When the schedule becomes active
63
+ Time time.Time // Specific time for the rule to run
64
+ Recurring Recurring // None, Daily, Weekly, Monthly
65
+ RecurringPeriod uint // Interval between executions: 1 = every interval, 2 = every second interval, etc.
66
+ }
67
+ ```
58
68
59
- - ` start_datetime ` - When the schedule becomes active
60
- - ` time ` - Specific time for the rule to run
61
- - ` recurring ` - Pattern: none, daily, weekly, or monthly
62
- - ` recurring_period ` - Interval between executions (1 = every interval, 2 = every second interval, etc.)
69
+ #### How Scheduling Works
70
+
71
+ 1 . ** Initialization** :
72
+ - The scheduler starts when the service begins running via ` StartScheduler() `
73
+ - It uses a ticker to check for rules that need to be executed at regular intervals
74
+
75
+ 2 . ** Rule Evaluation** :
76
+ - For each tick, the scheduler:
77
+ - Gets all enabled rules scheduled before the current time
78
+ - For each rule, checks if it should run using ` shouldRunRule() `
79
+ - If a rule should run, processes it asynchronously
80
+
81
+ 3 . ** Execution Timing** :
82
+ The ` shouldRunRule() ` function determines if a rule should run by checking:
83
+ - If the rule's start time has been reached
84
+ - If the current time matches the scheduled execution time
85
+ - For recurring rules:
86
+ - ** Daily** : Checks if the correct number of days have passed since start
87
+ - ** Weekly** : Checks if the correct number of weeks have passed since start
88
+ - ** Monthly** : Checks if the correct number of months have passed since start
89
+
90
+ 4 . ** Recurring Patterns** :
91
+ - ` None ` : Rule runs once at the specified time
92
+ - ` Daily ` : Rule runs every N days where N is the RecurringPeriod
93
+ - ` Weekly ` : Rule runs every N weeks
94
+ - ` Monthly ` : Rule runs every N months
63
95
64
96
For example, to run a rule:
65
97
- Every day at 9 AM: Set recurring to "daily" with recurring_period = 1
0 commit comments