Skip to content

Commit 755e18d

Browse files
committed
add how the scheduler works
Signed-off-by: nyagamunene <stevenyaga2014@gmail.com>
1 parent 0b01f85 commit 755e18d

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

re/README.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,44 @@ When a message arrives on a rule's input channel, the Rules Engine:
5454

5555
### Scheduling
5656

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+
```
5868

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
6395

6496
For example, to run a rule:
6597
- Every day at 9 AM: Set recurring to "daily" with recurring_period = 1

0 commit comments

Comments
 (0)