Hone logo
Hone
Problems

Go Cron Job Scheduler

This challenge asks you to implement a simple cron job scheduler in Go. Cron jobs are a powerful tool for automating tasks at specific times or intervals, and understanding how to implement them is a valuable skill for any Go developer. Your task is to create a scheduler that can register functions to be executed based on cron expressions.

Problem Description

You need to build a CronScheduler that allows users to register functions to be executed at specific times defined by cron expressions. The scheduler should run in a goroutine and execute the registered functions whenever their cron expression matches the current time.

Key Requirements:

  • Cron Expression Parsing: The scheduler should be able to parse cron expressions (e.g., "0 0 * * * *" for midnight every day). You can use a third-party library for this, such as github.com/robfig/cron.
  • Function Registration: Provide a method to register functions with a cron expression. The registered function should take no arguments and return no value (func()).
  • Scheduling and Execution: The scheduler should run continuously, checking the current time against the registered cron expressions. When a match is found, the corresponding function should be executed in a separate goroutine to avoid blocking the scheduler.
  • Error Handling: Handle potential errors during cron expression parsing and function execution gracefully. Log errors instead of panicking.
  • Concurrency Safety: Ensure that the scheduler is concurrency-safe, allowing multiple goroutines to register functions without race conditions.

Expected Behavior:

  • The scheduler should start running immediately after initialization.
  • Registered functions should be executed at the times specified by their cron expressions.
  • The scheduler should continue running even if some functions fail to execute.
  • The scheduler should not block the main goroutine.

Edge Cases to Consider:

  • Invalid cron expressions (e.g., "abc").
  • Functions that panic during execution.
  • Multiple functions with the same cron expression. (The scheduler should execute all of them.)
  • Empty cron expression list.
  • Time zone considerations (assume UTC for simplicity).

Examples

Example 1:

Input:
Scheduler initialized with a function "print_hello" scheduled for "* * * * *" (every minute) and a function "print_goodbye" scheduled for "0 12 * * *" (noon every day).

Output:
The scheduler runs continuously.  "print_hello" is executed every minute. "print_goodbye" is executed at noon every day.  The output to the console will reflect these executions.
Explanation:
The scheduler parses the cron expressions and executes the corresponding functions at the specified times.  The "print_hello" function will be executed frequently, while "print_goodbye" will be executed less often.

Example 2:

Input:
Scheduler initialized with an invalid cron expression "invalid_cron".

Output:
The scheduler logs an error message indicating that the cron expression is invalid and ignores the registration attempt. The scheduler continues to run.
Explanation:
The scheduler detects the invalid cron expression and handles the error gracefully, preventing the program from crashing.

Example 3:

Input:
Scheduler initialized with a function "panic_function" scheduled for "0 * * * * *" (every hour) that contains `panic("Simulated error")`.

Output:
The scheduler logs an error message when "panic_function" panics. The scheduler continues to run and executes other registered functions.
Explanation:
The scheduler catches the panic within the goroutine executing the function and logs the error, preventing it from crashing the entire scheduler.

Constraints

  • The cron expression parsing library should be a well-established and maintained Go library.
  • The scheduler should be able to handle at least 100 registered functions without significant performance degradation.
  • Function execution time should be limited to 1 second. If a function takes longer than 1 second to execute, log an error and move on.
  • The scheduler should use goroutines to execute functions concurrently.

Notes

  • Consider using a sync.Mutex to protect shared data structures within the scheduler.
  • The robfig/cron library provides a convenient way to parse cron expressions and schedule tasks.
  • Logging is crucial for debugging and monitoring the scheduler's behavior. Use the standard log package.
  • Focus on correctness and robustness. Error handling and concurrency safety are important aspects of this challenge.
  • You don't need to implement a full-fledged cron daemon; a simplified scheduler that demonstrates the core concepts is sufficient.
Loading editor...
go