executors 0.0.1

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.
@@ -0,0 +1,4 @@
1
+ require "executors/services"
2
+
3
+ module Executors
4
+ end
@@ -0,0 +1,183 @@
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
+ class Services
7
+ class << self
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
+ attr_accessor :logger
22
+ @@executors = {}
23
+
24
+ def get(id)
25
+ @@executors[id]
26
+ end
27
+
28
+ def get_executor(type, size)
29
+ case type
30
+ when "cached"
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
148
+ end
149
+
150
+ def parse_object(value)
151
+ if value.nil?
152
+ return nil
153
+ else
154
+ begin
155
+ return Object.const_get(value).new
156
+ rescue NameError
157
+ return nil
158
+ end
159
+ end
160
+ end
161
+
162
+ def parse_string(value)
163
+ if value.nil?
164
+ return nil
165
+ else
166
+ return value
167
+ end
168
+ end
169
+
170
+ def parse_symbol(value)
171
+ if value.nil?
172
+ return nil
173
+ else
174
+ return value.to_sym
175
+ end
176
+ end
177
+
178
+ def set(id, executor)
179
+ @@executors[id] = executor
180
+ end
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,3 @@
1
+ module Executors
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: executors
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Phil Ostler
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-11 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Wrapper for Java's Executor Service classes allowing seamless integration with JRuby on Rails
22
+ email:
23
+ - philostler@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/executors.rb
32
+ - lib/executors/services.rb
33
+ - lib/executors/version.rb
34
+ has_rdoc: true
35
+ homepage: https://github.com/philostler/executors
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.7
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Wrapper for Java's Executor Service classes
66
+ test_files: []
67
+