executors 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/executors/services.rb +18 -158
- data/lib/executors/version.rb +1 -1
- data/lib/executors/yaml_parser.rb +177 -0
- metadata +6 -5
data/lib/executors/services.rb
CHANGED
|
@@ -1,180 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
java_import java.util.concurrent.ScheduledExecutorService
|
|
3
|
-
java_import java.util.concurrent.TimeUnit
|
|
1
|
+
require "executors/yaml_parser"
|
|
4
2
|
|
|
5
3
|
module Executors
|
|
4
|
+
# Executor services top-level point of entry.
|
|
6
5
|
class Services
|
|
7
|
-
|
|
8
|
-
YAML_EXECUTOR_ID_KEY = "id"
|
|
9
|
-
YAML_EXECUTOR_TYPE_KEY = "type"
|
|
10
|
-
YAML_EXECUTOR_TYPE_VALID = [ "cached", "fixed", "scheduled", "single" ]
|
|
11
|
-
YAML_EXECUTOR_SIZE_KEY = "size"
|
|
12
|
-
YAML_EXECUTOR_TYPES_REQUIRING_SIZE = [ "fixed", "scheduled" ]
|
|
13
|
-
|
|
14
|
-
YAML_COMMMAND_COMMANDS_KEY = "commands"
|
|
15
|
-
YAML_COMMMAND_COMMAND_KEY = "command"
|
|
16
|
-
YAML_COMMMAND_INITIAL_KEY = "initial"
|
|
17
|
-
YAML_COMMMAND_DELAY_KEY = "delay"
|
|
18
|
-
YAML_COMMMAND_UNITS_KEY = "units"
|
|
19
|
-
YAML_COMMAND_UNITS_VALID = [ "days", "hours", "microseconds", "milliseconds", "minutes", "nanoseconds", "seconds" ]
|
|
6
|
+
extend Executors::YamlParser
|
|
20
7
|
|
|
8
|
+
class << self
|
|
21
9
|
attr_accessor :logger
|
|
22
10
|
@@executors = {}
|
|
23
11
|
|
|
12
|
+
# Gets an executor with the specified identifer.
|
|
24
13
|
def get(id)
|
|
25
14
|
@@executors[id]
|
|
26
15
|
end
|
|
27
16
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return java.util.concurrent.Executors.new_cached_thread_pool
|
|
32
|
-
when "fixed"
|
|
33
|
-
return java.util.concurrent.Executors.new_fixed_thread_pool size
|
|
34
|
-
when "scheduled"
|
|
35
|
-
return java.util.concurrent.Executors.new_scheduled_thread_pool size
|
|
36
|
-
when "single"
|
|
37
|
-
return java.util.concurrent.Executors.new_single_thread_executor
|
|
38
|
-
else
|
|
39
|
-
return nil
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def load_yaml_string(yaml)
|
|
44
|
-
# Each executor definition
|
|
45
|
-
yaml.each do |y|
|
|
46
|
-
# Executor identifier
|
|
47
|
-
id = parse_symbol y[YAML_EXECUTOR_ID_KEY]
|
|
48
|
-
if id.nil?
|
|
49
|
-
logger.warn { "YAML executor definition does not have an id. Skipping" } unless logger.nil?
|
|
50
|
-
next
|
|
51
|
-
end
|
|
52
|
-
if @@executors.include?(id)
|
|
53
|
-
logger.warn { "YAML executor definition for id \"" + id.to_s + "\" has already been defined. Duplicates not allowed. Skipping" } unless logger.nil?
|
|
54
|
-
next
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# Executor type
|
|
58
|
-
type = parse_string y[YAML_EXECUTOR_TYPE_KEY]
|
|
59
|
-
if type.nil?
|
|
60
|
-
logger.warn { "YAML executor definition for id \"" + id.to_s + "\" does not have an type. Skipping" } unless logger.nil?
|
|
61
|
-
next
|
|
62
|
-
end
|
|
63
|
-
if !YAML_EXECUTOR_TYPE_VALID.include?(type)
|
|
64
|
-
logger.warn { "YAML executor definition for id \"" + id.to_s + "\" contains invalid type \"" + type + "\". Skipping" } unless logger.nil?
|
|
65
|
-
next
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
# Executor size
|
|
69
|
-
size = parse_string y[YAML_EXECUTOR_SIZE_KEY]
|
|
70
|
-
if YAML_EXECUTOR_TYPES_REQUIRING_SIZE.include?(type)
|
|
71
|
-
if size.nil?
|
|
72
|
-
logger.warn { "YAML executor definition for id \"" + id.to_s + "\" of type \"" + type + "\" does not have a size. Skipping" } unless logger.nil?
|
|
73
|
-
next
|
|
74
|
-
end
|
|
75
|
-
if size.to_i < 1
|
|
76
|
-
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?
|
|
77
|
-
next
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
# Create & set executor
|
|
82
|
-
executor = get_executor type, size
|
|
83
|
-
if executor.nil?
|
|
84
|
-
logger.error { "Unknown executor type \"" + type + "\". Unable to create executor" } unless logger.nil?
|
|
85
|
-
next
|
|
86
|
-
end
|
|
87
|
-
set id, executor
|
|
88
|
-
|
|
89
|
-
# Command definitions present?
|
|
90
|
-
if !y[YAML_COMMMAND_COMMANDS_KEY].nil?
|
|
91
|
-
# Each command definition
|
|
92
|
-
y[YAML_COMMMAND_COMMANDS_KEY].each do |c|
|
|
93
|
-
# Command commmand class
|
|
94
|
-
command = parse_string c[YAML_COMMMAND_COMMAND_KEY]
|
|
95
|
-
if command.nil?
|
|
96
|
-
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have a command attribute. Skipping" } unless logger.nil?
|
|
97
|
-
next
|
|
98
|
-
end
|
|
99
|
-
command = parse_object command
|
|
100
|
-
if command.nil?
|
|
101
|
-
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not reference a valid class. Skipping" } unless logger.nil?
|
|
102
|
-
next
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
case executor
|
|
106
|
-
when ScheduledExecutorService
|
|
107
|
-
# Command initial
|
|
108
|
-
initial = parse_string c[YAML_COMMMAND_INITIAL_KEY]
|
|
109
|
-
if initial.nil?
|
|
110
|
-
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have an initial. Skipping" } unless logger.nil?
|
|
111
|
-
next
|
|
112
|
-
end
|
|
113
|
-
if initial.to_i < 1
|
|
114
|
-
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?
|
|
115
|
-
next
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
# Command delay
|
|
119
|
-
delay = parse_string c[YAML_COMMMAND_DELAY_KEY]
|
|
120
|
-
if delay.nil?
|
|
121
|
-
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have a delay. Skipping" } unless logger.nil?
|
|
122
|
-
next
|
|
123
|
-
end
|
|
124
|
-
if delay.to_i < 1
|
|
125
|
-
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?
|
|
126
|
-
next
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
# Command units
|
|
130
|
-
units = parse_string c[YAML_COMMMAND_UNITS_KEY]
|
|
131
|
-
if units.nil?
|
|
132
|
-
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have a units. Skipping" } unless logger.nil?
|
|
133
|
-
next
|
|
134
|
-
end
|
|
135
|
-
if !YAML_COMMAND_UNITS_VALID.include?(units.downcase)
|
|
136
|
-
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" with units of \"" + units + "\" is not valid. Skipping" } unless logger.nil?
|
|
137
|
-
next
|
|
138
|
-
end
|
|
139
|
-
units = TimeUnit.value_of units.upcase
|
|
140
|
-
|
|
141
|
-
executor.schedule_with_fixed_delay command, initial, delay, units
|
|
142
|
-
when ExecutorService
|
|
143
|
-
executor.submit command
|
|
144
|
-
end
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
end
|
|
17
|
+
# Returns a cached ExecutorService[http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool()].
|
|
18
|
+
def get_cached_executor()
|
|
19
|
+
get_executor "cached"
|
|
148
20
|
end
|
|
149
21
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
else
|
|
154
|
-
begin
|
|
155
|
-
return Object.const_get(value).new
|
|
156
|
-
rescue NameError
|
|
157
|
-
return nil
|
|
158
|
-
end
|
|
159
|
-
end
|
|
22
|
+
# Returns a fixed ExecutorService[http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool(int)] of the specified size.
|
|
23
|
+
def get_fixed_executor(size)
|
|
24
|
+
get_executor "fixed", size
|
|
160
25
|
end
|
|
161
26
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
else
|
|
166
|
-
return value
|
|
167
|
-
end
|
|
27
|
+
# Returns a ScheduledExecutorService[http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html#newScheduledThreadPool(int)] of the specified size.
|
|
28
|
+
def get_scheduled_executor(size)
|
|
29
|
+
get_executor "scheduled", size
|
|
168
30
|
end
|
|
169
31
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
else
|
|
174
|
-
return value.to_sym
|
|
175
|
-
end
|
|
32
|
+
# Returns a single ExecutorService[http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html#newSingleThreadExecutor()].
|
|
33
|
+
def get_single_executor()
|
|
34
|
+
get_executor "single"
|
|
176
35
|
end
|
|
177
36
|
|
|
37
|
+
# Sets an executor against the specified identifer.
|
|
178
38
|
def set(id, executor)
|
|
179
39
|
@@executors[id] = executor
|
|
180
40
|
end
|
data/lib/executors/version.rb
CHANGED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
java_import java.util.concurrent.ExecutorService
|
|
2
|
+
java_import java.util.concurrent.ScheduledExecutorService
|
|
3
|
+
java_import java.util.concurrent.TimeUnit
|
|
4
|
+
|
|
5
|
+
module Executors
|
|
6
|
+
# YAML executor and command configuration parser.
|
|
7
|
+
module YamlParser
|
|
8
|
+
YAML_EXECUTOR_ID_KEY = "id"
|
|
9
|
+
YAML_EXECUTOR_TYPE_KEY = "type"
|
|
10
|
+
YAML_EXECUTOR_TYPE_VALID = [ "cached", "fixed", "scheduled", "single" ]
|
|
11
|
+
YAML_EXECUTOR_SIZE_KEY = "size"
|
|
12
|
+
YAML_EXECUTOR_TYPES_REQUIRING_SIZE = [ "fixed", "scheduled" ]
|
|
13
|
+
|
|
14
|
+
YAML_COMMMAND_COMMANDS_KEY = "commands"
|
|
15
|
+
YAML_COMMMAND_COMMAND_KEY = "command"
|
|
16
|
+
YAML_COMMMAND_INITIAL_KEY = "initial"
|
|
17
|
+
YAML_COMMMAND_DELAY_KEY = "delay"
|
|
18
|
+
YAML_COMMMAND_UNITS_KEY = "units"
|
|
19
|
+
YAML_COMMAND_UNITS_VALID = [ "days", "hours", "microseconds", "milliseconds", "minutes", "nanoseconds", "seconds" ]
|
|
20
|
+
|
|
21
|
+
# Loads a YAML configuration string and instantiates executors and commands as defined.
|
|
22
|
+
#
|
|
23
|
+
# ===Example Usage
|
|
24
|
+
#
|
|
25
|
+
# <tt>Executors::Services.load_yaml_string YAML.load_file(Rails.root.join("config", "executors.yml"))</tt>
|
|
26
|
+
def load_yaml_string(yaml)
|
|
27
|
+
# Each executor definition
|
|
28
|
+
yaml.each do |y|
|
|
29
|
+
# Executor identifier
|
|
30
|
+
id = parse_symbol y[YAML_EXECUTOR_ID_KEY]
|
|
31
|
+
if id.nil?
|
|
32
|
+
logger.warn { "YAML executor definition does not have an id. Skipping" } unless logger.nil?
|
|
33
|
+
next
|
|
34
|
+
end
|
|
35
|
+
if !get(id).nil?
|
|
36
|
+
logger.warn { "YAML executor definition for id \"" + id.to_s + "\" has already been defined. Duplicates not allowed. Skipping" } unless logger.nil?
|
|
37
|
+
next
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Executor type
|
|
41
|
+
type = parse_string y[YAML_EXECUTOR_TYPE_KEY]
|
|
42
|
+
if type.nil?
|
|
43
|
+
logger.warn { "YAML executor definition for id \"" + id.to_s + "\" does not have an type. Skipping" } unless logger.nil?
|
|
44
|
+
next
|
|
45
|
+
end
|
|
46
|
+
if !YAML_EXECUTOR_TYPE_VALID.include?(type)
|
|
47
|
+
logger.warn { "YAML executor definition for id \"" + id.to_s + "\" contains invalid type \"" + type + "\". Skipping" } unless logger.nil?
|
|
48
|
+
next
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Executor size
|
|
52
|
+
size = parse_string y[YAML_EXECUTOR_SIZE_KEY]
|
|
53
|
+
if YAML_EXECUTOR_TYPES_REQUIRING_SIZE.include?(type)
|
|
54
|
+
if size.nil?
|
|
55
|
+
logger.warn { "YAML executor definition for id \"" + id.to_s + "\" of type \"" + type + "\" does not have a size. Skipping" } unless logger.nil?
|
|
56
|
+
next
|
|
57
|
+
end
|
|
58
|
+
if size.to_i < 1
|
|
59
|
+
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?
|
|
60
|
+
next
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Create & set executor
|
|
65
|
+
executor = get_executor type, size
|
|
66
|
+
if executor.nil?
|
|
67
|
+
logger.error { "Unknown executor type \"" + type + "\". Unable to create executor" } unless logger.nil?
|
|
68
|
+
next
|
|
69
|
+
end
|
|
70
|
+
set id, executor
|
|
71
|
+
|
|
72
|
+
# Command definitions present?
|
|
73
|
+
if !y[YAML_COMMMAND_COMMANDS_KEY].nil?
|
|
74
|
+
# Each command definition
|
|
75
|
+
y[YAML_COMMMAND_COMMANDS_KEY].each do |c|
|
|
76
|
+
# Command commmand class
|
|
77
|
+
command = parse_string c[YAML_COMMMAND_COMMAND_KEY]
|
|
78
|
+
if command.nil?
|
|
79
|
+
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have a command attribute. Skipping" } unless logger.nil?
|
|
80
|
+
next
|
|
81
|
+
end
|
|
82
|
+
command = parse_object command
|
|
83
|
+
if command.nil?
|
|
84
|
+
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not reference a valid class. Skipping" } unless logger.nil?
|
|
85
|
+
next
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
case executor
|
|
89
|
+
when ScheduledExecutorService
|
|
90
|
+
# Command initial
|
|
91
|
+
initial = parse_string c[YAML_COMMMAND_INITIAL_KEY]
|
|
92
|
+
if initial.nil?
|
|
93
|
+
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have an initial. Skipping" } unless logger.nil?
|
|
94
|
+
next
|
|
95
|
+
end
|
|
96
|
+
if initial.to_i < 1
|
|
97
|
+
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?
|
|
98
|
+
next
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Command delay
|
|
102
|
+
delay = parse_string c[YAML_COMMMAND_DELAY_KEY]
|
|
103
|
+
if delay.nil?
|
|
104
|
+
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have a delay. Skipping" } unless logger.nil?
|
|
105
|
+
next
|
|
106
|
+
end
|
|
107
|
+
if delay.to_i < 1
|
|
108
|
+
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?
|
|
109
|
+
next
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Command units
|
|
113
|
+
units = parse_string c[YAML_COMMMAND_UNITS_KEY]
|
|
114
|
+
if units.nil?
|
|
115
|
+
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" does not have a units. Skipping" } unless logger.nil?
|
|
116
|
+
next
|
|
117
|
+
end
|
|
118
|
+
if !YAML_COMMAND_UNITS_VALID.include?(units.downcase)
|
|
119
|
+
logger.warn { "YAML command definition for executor id \"" + id.to_s + "\" with units of \"" + units + "\" is not valid. Skipping" } unless logger.nil?
|
|
120
|
+
next
|
|
121
|
+
end
|
|
122
|
+
units = TimeUnit.value_of units.upcase
|
|
123
|
+
|
|
124
|
+
executor.schedule_with_fixed_delay command, initial, delay, units
|
|
125
|
+
when ExecutorService
|
|
126
|
+
executor.submit command
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
private
|
|
134
|
+
def get_executor(type, size)
|
|
135
|
+
case type
|
|
136
|
+
when "cached"
|
|
137
|
+
return java.util.concurrent.Executors.new_cached_thread_pool
|
|
138
|
+
when "fixed"
|
|
139
|
+
return java.util.concurrent.Executors.new_fixed_thread_pool size
|
|
140
|
+
when "scheduled"
|
|
141
|
+
return java.util.concurrent.Executors.new_scheduled_thread_pool size
|
|
142
|
+
when "single"
|
|
143
|
+
return java.util.concurrent.Executors.new_single_thread_executor
|
|
144
|
+
else
|
|
145
|
+
return nil
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def parse_object(value)
|
|
150
|
+
if value.nil?
|
|
151
|
+
return nil
|
|
152
|
+
else
|
|
153
|
+
begin
|
|
154
|
+
return Object.const_get(value).new
|
|
155
|
+
rescue NameError
|
|
156
|
+
return nil
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def parse_string(value)
|
|
162
|
+
if value.nil?
|
|
163
|
+
return nil
|
|
164
|
+
else
|
|
165
|
+
return value
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def parse_symbol(value)
|
|
170
|
+
if value.nil?
|
|
171
|
+
return nil
|
|
172
|
+
else
|
|
173
|
+
return value.to_sym
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
metadata
CHANGED
|
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
|
5
5
|
segments:
|
|
6
6
|
- 0
|
|
7
7
|
- 0
|
|
8
|
-
-
|
|
9
|
-
version: 0.0.
|
|
8
|
+
- 2
|
|
9
|
+
version: 0.0.2
|
|
10
10
|
platform: ruby
|
|
11
11
|
authors:
|
|
12
12
|
- Phil Ostler
|
|
@@ -14,7 +14,7 @@ autorequire:
|
|
|
14
14
|
bindir: bin
|
|
15
15
|
cert_chain: []
|
|
16
16
|
|
|
17
|
-
date: 2011-01-
|
|
17
|
+
date: 2011-01-18 00:00:00 +00:00
|
|
18
18
|
default_executable:
|
|
19
19
|
dependencies: []
|
|
20
20
|
|
|
@@ -31,13 +31,14 @@ files:
|
|
|
31
31
|
- lib/executors.rb
|
|
32
32
|
- lib/executors/services.rb
|
|
33
33
|
- lib/executors/version.rb
|
|
34
|
+
- lib/executors/yaml_parser.rb
|
|
34
35
|
has_rdoc: true
|
|
35
36
|
homepage: https://github.com/philostler/executors
|
|
36
37
|
licenses: []
|
|
37
38
|
|
|
38
39
|
post_install_message:
|
|
39
|
-
rdoc_options:
|
|
40
|
-
|
|
40
|
+
rdoc_options:
|
|
41
|
+
- --line-numbers
|
|
41
42
|
require_paths:
|
|
42
43
|
- lib
|
|
43
44
|
required_ruby_version: !ruby/object:Gem::Requirement
|