Module: Executors::YamlParser

Included in:
Services
Defined in:
lib/executors/yaml_parser.rb

Overview

YAML executor and command configuration parser.

Constant Summary collapse

YAML_EXECUTOR_ID_KEY =
"id"
YAML_EXECUTOR_TYPE_KEY =
"type"
YAML_EXECUTOR_TYPE_VALID =
[ "cached", "fixed", "scheduled", "single", "single_scheduled" ]
YAML_EXECUTOR_SIZE_KEY =
"size"
YAML_EXECUTOR_TYPES_REQUIRING_SIZE =
[ "fixed", "scheduled" ]
YAML_COMMMAND_COMMANDS_KEY =
"commands"
YAML_COMMMAND_COMMAND_KEY =
"command"
YAML_COMMMAND_INITIAL_KEY =
"initial"
YAML_COMMMAND_DELAY_KEY =
"delay"
YAML_COMMMAND_UNITS_KEY =
"units"
YAML_COMMAND_UNITS_VALID =
[ "days", "hours", "microseconds", "milliseconds", "minutes", "nanoseconds", "seconds" ]

Instance Method Summary collapse

Instance Method Details

#load_yaml_string(yaml) ⇒ Object

Loads a YAML configuration string and instantiates executors and commands as defined.

Example Usage

Executors::Services.load_yaml_string YAML.load_file(Rails.root.join("config", "executors.yml"))



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
131
# File 'lib/executors/yaml_parser.rb', line 26

def load_yaml_string(yaml)
  # Each executor definition
  yaml.each do |y|
    # Executor identifier
    id = parse_symbol y[YAML_EXECUTOR_ID_KEY]
    if id.nil?
      logger.warn { "YAML executor definition does not have an id. Skipping" } unless logger.nil?
      next
    end
    if !get(id).nil?
      logger.warn { "YAML executor definition for id \"" + id.to_s + "\" has already been defined. Duplicates not allowed. Skipping" } unless logger.nil?
      next
    end

    # Executor type
    type = parse_string y[YAML_EXECUTOR_TYPE_KEY]
    if type.nil?
      logger.warn { "YAML executor definition for id \"" + id.to_s + "\" does not have an type. Skipping" } unless logger.nil?
      next
    end
    if !YAML_EXECUTOR_TYPE_VALID.include?(type)
      logger.warn { "YAML executor definition for id \"" + id.to_s + "\" contains invalid type \"" + type + "\". Skipping" } unless logger.nil?
      next
    end

    # Executor size
    size = parse_string y[YAML_EXECUTOR_SIZE_KEY]
    if YAML_EXECUTOR_TYPES_REQUIRING_SIZE.include?(type)
      if size.nil?
        logger.warn { "YAML executor definition for id \"" + id.to_s + "\" of type \"" + type + "\" does not have a size. Skipping" } unless logger.nil?
        next
      end
      if size.to_i < 1
        logger.warn { "YAML executor definition for id \"" + id.to_s + "\" has an invalid size of \"" + size.to_s + "\". Size must be bigger than 0. Skipping" } unless logger.nil?
        next
      end
    end

    # Create & set executor
    executor = get_executor type, size
    if executor.nil?
      logger.error { "Unknown executor type \"" + type + "\". Unable to create executor" } unless logger.nil?
      next
    end
    set id, executor

    # Command definitions present?
    if !y[YAML_COMMMAND_COMMANDS_KEY].nil?
      # Each command definition
      y[YAML_COMMMAND_COMMANDS_KEY].each do |c|
        # Command commmand class
        command = parse_string c[YAML_COMMMAND_COMMAND_KEY]
        if command.nil?
          logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have a command attribute. Skipping" } unless logger.nil?
          next
        end
        command = parse_object command
        if command.nil?
          logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not reference a valid class. Skipping" } unless logger.nil?
          next
        end

        case executor
          when ScheduledExecutorService
            # Command initial
            initial = parse_string c[YAML_COMMMAND_INITIAL_KEY]
            if initial.nil?
              logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have an initial. Skipping" } unless logger.nil?
              next
            end
            if initial.to_i < 1
              logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" has an invalid initial of \"" + initial.to_s + "\". Initial must be bigger than 0. Skipping" } unless logger.nil?
              next
            end

            # Command delay
            delay = parse_string c[YAML_COMMMAND_DELAY_KEY]
            if delay.nil?
              logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have a delay. Skipping" } unless logger.nil?
              next
            end
            if delay.to_i < 1
              logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" has an invalid delay of \"" + delay.to_s + "\". Delay must be bigger than 0. Skipping" } unless logger.nil?
              next
            end

            # Command units
            units = parse_string c[YAML_COMMMAND_UNITS_KEY]
            if units.nil?
              logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have a units. Skipping" } unless logger.nil?
              next
            end
            if !YAML_COMMAND_UNITS_VALID.include?(units.downcase)
              logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" with units of \"" + units + "\" is not valid. Skipping" } unless logger.nil?
              next
            end
            units = TimeUnit.value_of units.upcase

            executor.schedule_with_fixed_delay command, initial, delay, units
          when ExecutorService
            executor.submit command
        end
      end
    end
  end
end