Class: SharedTools::Tools::CronTool

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/shared_tools/tools/cron_tool.rb

Overview

A tool for parsing, validating, and explaining cron expressions. Supports standard 5-field cron format (minute, hour, day of month, month, day of week).

Examples:

tool = SharedTools::Tools::CronTool.new
result = tool.execute(action: 'parse', expression: '0 9 * * 1-5')
puts result[:description]  # "At 09:00, Monday through Friday"

Constant Summary collapse

DAYS_OF_WEEK =
{
  'SUN' => 0, 'MON' => 1, 'TUE' => 2, 'WED' => 3,
  'THU' => 4, 'FRI' => 5, 'SAT' => 6
}.freeze
MONTHS =
{
  'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4,
  'MAY' => 5, 'JUN' => 6, 'JUL' => 7, 'AUG' => 8,
  'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ CronTool

Returns a new instance of CronTool.

Parameters:

  • logger (Logger) (defaults to: nil)

    optional logger



95
96
97
# File 'lib/shared_tools/tools/cron_tool.rb', line 95

def initialize(logger: nil)
  @logger = logger || RubyLLM.logger
end

Class Method Details

.nameObject



16
# File 'lib/shared_tools/tools/cron_tool.rb', line 16

def self.name = 'cron'

Instance Method Details

#execute(action:, expression: nil, description: nil, count: nil) ⇒ Hash

Execute cron action

Parameters:

  • action (String)

    action to perform

  • expression (String, nil) (defaults to: nil)

    cron expression

  • description (String, nil) (defaults to: nil)

    schedule description for generate

  • count (Integer, nil) (defaults to: nil)

    number of next executions

Returns:

  • (Hash)

    result



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/shared_tools/tools/cron_tool.rb', line 106

def execute(action:, expression: nil, description: nil, count: nil)
  @logger.info("CronTool#execute action=#{action.inspect}")

  case action.to_s.downcase
  when 'parse'
    parse_expression(expression)
  when 'validate'
    validate_expression(expression)
  when 'next'
    next_executions(expression, count || 5)
  when 'generate'
    generate_expression(description)
  else
    {
      success: false,
      error: "Unknown action: #{action}. Valid actions are: parse, validate, next, generate"
    }
  end
rescue => e
  @logger.error("CronTool error: #{e.message}")
  {
    success: false,
    error: e.message
  }
end