Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewScheduler ¶
func NewScheduler(job gocron.JobDefinition) ro.Observable[ScheduleJob]
NewScheduler creates a new observable that emits a notification on each tick of the scheduler.
Example: trigger a job every night at 23:42.
NewScheduler(gocron.CronJob("42 23 * * *"), false).Subscribe(...)
Example (DailyAtSpecificTime) ¶
// Create a scheduler that emits every 75ms for testing observable := ro.Pipe1( NewScheduler( gocron.DurationJob(75*time.Millisecond), ), extractCounter(), ) subscription := observable.Subscribe(ro.PrintObserver[int]()) defer subscription.Unsubscribe() // Wait for a few events to be emitted time.Sleep(250 * time.Millisecond)
Output: Next: 0 Next: 1 Next: 2
Example (EveryMinute) ¶
// Create a scheduler that emits every 100ms for testing observable := ro.Pipe1( NewScheduler( gocron.DurationJob(100*time.Millisecond), ), extractCounter(), ) subscription := observable.Subscribe(ro.PrintObserver[int]()) defer subscription.Unsubscribe() // Wait for a few events to be emitted time.Sleep(325 * time.Millisecond)
Output: Next: 0 Next: 1 Next: 2
Example (EverySecond) ¶
// Create a scheduler that emits every 50ms for testing observable := ro.Pipe1( NewScheduler( gocron.DurationJob(50*time.Millisecond), ), extractCounter(), ) subscription := observable.Subscribe(ro.PrintObserver[int]()) defer subscription.Unsubscribe() // Wait for a few events to be emitted time.Sleep(175 * time.Millisecond)
Output: Next: 0 Next: 1 Next: 2
Example (MonthlyOnFirstDay) ¶
// Create a scheduler that emits every 150ms for testing observable := ro.Pipe1( NewScheduler( gocron.DurationJob(150*time.Millisecond), ), extractCounter(), ) subscription := observable.Subscribe(ro.PrintObserver[int]()) defer subscription.Unsubscribe() // Wait for a few events to be emitted time.Sleep(475 * time.Millisecond)
Output: Next: 0 Next: 1 Next: 2
Example (WeeklyOnMonday) ¶
// Create a scheduler that emits every 125ms for testing observable := ro.Pipe1( NewScheduler( gocron.DurationJob(125*time.Millisecond), ), extractCounter(), ) subscription := observable.Subscribe(ro.PrintObserver[int]()) defer subscription.Unsubscribe() // Wait for a few events to be emitted time.Sleep(400 * time.Millisecond)
Output: Next: 0 Next: 1 Next: 2
Example (WithContext) ¶
// Create a scheduler with context for cancellation ctx, cancel := context.WithTimeout(context.Background(), 225*time.Millisecond) defer cancel() observable := ro.Pipe1( NewScheduler( gocron.DurationJob(50*time.Millisecond), ), extractCounter(), ) subscription := observable.SubscribeWithContext(ctx, ro.PrintObserver[int]()) defer subscription.Unsubscribe() // Wait for context to timeout time.Sleep(300 * time.Millisecond)
Output: Next: 0 Next: 1 Next: 2 Next: 3 Error: context deadline exceeded
Example (WithProcessing) ¶
// Create a scheduler and process the events
observable := ro.Pipe3(
NewScheduler(
gocron.DurationJob(50*time.Millisecond),
),
extractCounter(),
ro.Map(func(counter int) string {
return fmt.Sprintf("Scheduled job #%d executed", counter)
}),
ro.Take[string](3), // Only take first 3 events
)
subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
// Wait for events to be processed
time.Sleep(200 * time.Millisecond)
Output: Next: Scheduled job #0 executed Next: Scheduled job #1 executed Next: Scheduled job #2 executed Completed
Types ¶
type ScheduleJob ¶
Click to show internal directories.
Click to hide internal directories.