dynarex_cron 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c96a52e108c3548cfdd0ace9597459948c3f3f0d
4
- data.tar.gz: 80b2145ba25bc05a262f12d51afa7ac0c54597f0
3
+ metadata.gz: 50ddabd424bdebc70f64092c80431839151e5ba5
4
+ data.tar.gz: 6cd42774d6718f4cf3528dcdd0be63347e212754
5
5
  SHA512:
6
- metadata.gz: 571ebc4a746d47f3985fbd4885570be16c82351eee94f0cec70611861eceb80577677586b65cc00500bb0b34f8ae625fad505c80c312d732023d74059675a054
7
- data.tar.gz: e20579ba72f4ea5bfbe8cc630e2c497dd47aa63944762015e6f28b6546b73b11b39ce9955ed3eb754828bba89529a95a9c471f2a193751c4bf61d9f82cb32756
6
+ metadata.gz: ebb728bb6a28f3f38db4b7cdcf5932dea5d50c6c0e6f7ea8dac5454c18c3abf134863d078d41a6d4b9640d2e5734407faf1410e7114f89ecea3dd9281df24b53
7
+ data.tar.gz: 477bc7f6d63da17edca61dc6df30bfee7aac48daf32142923776dc804117805ca3a434f29ce8c2d67a9e2690c585ade8f4b5bf5516a20ef9440cb92c5a93c69f
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/dynarex_cron.rb CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  require 'dynarex'
6
6
  require 'chronic_cron'
7
- require 'simplepubsub'
7
+ require 'websocket-eventmachine-client'
8
8
  require 'rscript'
9
9
  require 'chronic_duration'
10
10
  require 'logger'
@@ -13,11 +13,12 @@ DF = "%Y-%m-%d %H:%M"
13
13
 
14
14
  class DynarexCron
15
15
 
16
- # options: e.g. sps_address, 'sps', drb_server: 57000
16
+ # options: e.g. sps_address: 'sps', sps_port: '59000'
17
17
  #
18
18
  def initialize(dynarex_file=nil, options={})
19
19
 
20
- opt = {sps_address: nil, drb_server: nil, log: nil}.merge options
20
+ opt = {sps_address: nil, sps_port='59000', drb_port: nil, log: nil}\
21
+ .merge options
21
22
  @logger = Logger.new(opt[:log],'weekly') if opt[:log]
22
23
 
23
24
  @cron_entries, @cron_events = [], []
@@ -26,35 +27,46 @@ class DynarexCron
26
27
  load_entries() if @dynarex_file
27
28
  load_events() if @include_url
28
29
 
29
- @sps_address = opt[:sps_address]
30
-
31
- if opt[:drb_server] then
32
-
30
+ @sps_address, @sps_port = opt[:sps_address], opt[:sps_port]
31
+
32
+ if opt[:drb_port] then
33
+
33
34
  Thread.new {
34
35
 
35
36
  # start up the DRb service
36
- DRb.start_service 'druby://:' + opt[:drb_server], self
37
+ DRb.start_service 'druby://:' + opt[:drb_port], self
37
38
 
38
39
  # wait for the DRb service to finish before exiting
39
40
  DRb.thread.join
40
41
  }
41
- end
42
+ end
43
+
42
44
  end
43
45
 
44
46
  def start
47
+
45
48
  @running = true
46
- puts '[' + Time.now.strftime(DF) + '] DynarexCron started'
47
-
48
- while @running == true
49
+ puts '[' + Time.now.strftime(DF) + '] DynarexCron started'
50
+ params = {uri: "ws://%s:%s" % [@sps_address, @sps_port]}
51
+
52
+ @ws = WebSocket::EventMachine::Client
53
+
54
+ EventMachine.run do
49
55
 
50
- iterate @cron_entries
51
- iterate @cron_events
52
- sleep 60 # wait for 60 seconds
56
+ @ws.connect(params)
57
+
58
+ EM.add_periodic_timer(60) do
59
+ iterate @cron_entries
60
+ iterate @cron_events
61
+ end
53
62
  end
63
+
54
64
  end
55
65
 
56
66
  def stop()
57
67
  @running = false
68
+ @ws.close
69
+ EM.stop_event_loop
58
70
  end
59
71
 
60
72
  def load_entries()
@@ -84,7 +96,9 @@ class DynarexCron
84
96
 
85
97
  if h[:cron].to_time.strftime(DF) == Time.now.strftime(DF) then
86
98
 
87
- Thread.new { run(h[:job]) }
99
+ topic, msg = h[:job].match(/^pub(?:lish)\s+([^:]+):(.*)/).captures
100
+ @ws.send "%s: %s" % [topic, msg]
101
+
88
102
  begin
89
103
  h[:cron].next # advances the time
90
104
  rescue
@@ -94,107 +108,66 @@ class DynarexCron
94
108
 
95
109
  end
96
110
  end
97
- end
111
+ end
98
112
 
99
- def run(job)
100
-
101
- case job
102
-
103
- when /^(run )/
104
- code2, args = RScript.new.read ($').scan(/[^'"]+/).map(&:strip)
105
- eval code2
106
-
107
- when /^(pub\s*(?:lish)?\s+)/
108
-
109
- return unless @sps_address
110
- SimplePubSub::Client.connect(@sps_address) do |client|
111
- topic, msg = ($').split(/:\s*/,2)
112
- client.publish(topic, msg)
113
- end
114
-
115
- when /^`([^`]+)/
116
- `#{$1}`
117
-
118
- when %r{http://}
119
- open(s, 'UserAgent' => 'DynarexCron v0.1')
120
-
121
- else
122
- eval(s)
123
- end
124
-
125
- end
126
-
127
113
  end
128
114
 
129
-
130
115
  class DynarexEvents < DynarexCron
131
116
 
132
117
  attr_reader :to_a
133
118
 
134
- # options: e.g. sps_address, 'sps', drb_server: 58000
135
- #
136
119
  def initialize(dynarex_file=nil, options={})
137
120
 
138
- opt = {sps_address: nil, drb_server: nil}.merge options
121
+ opt = {sps_address: nil}.merge options
122
+
123
+ @cron_events = []
139
124
 
140
- @entries, @cron_events = [], []
141
-
142
125
  @dynarex_file = dynarex_file
143
126
  load_events()
144
127
 
145
128
  @sps_address = opt[:sps_address]
146
129
 
147
- if opt[:drb_server] then
148
-
149
- Thread.new {
150
-
151
- # start up the DRb service
152
- DRb.start_service 'druby://:' + opt[:drb_server], self
153
-
154
- # wait for the DRb service to finish before exiting
155
- DRb.thread.join
156
- }
157
- end
158
130
  end
159
131
 
160
- def add_entry(h)
161
- # if the entry already exists delete it
162
- @entries.delete @entries.find {|x| x[:job] == h[:job]}
163
- @entries << h
164
- end
165
-
166
132
  def load_events()
167
133
 
168
134
  dynarex = Dynarex.new @dynarex_file
169
- @entries = dynarex.to_h || @entries
170
- @cron_events = self.to_a if @entries
171
- 'events loaded and refreshed'
135
+ @entries = dynarex.to_h
136
+ @cron_events = self.to_a
172
137
  end
173
138
 
174
139
  alias refresh load_events
175
140
 
176
141
  def start
142
+
177
143
  @running = true
178
144
  puts '[' + Time.now.strftime(DF) + '] DynarexEvents started'
179
-
180
- while @running == true
181
- iterate @cron_events
182
- sleep 60 # wait for 60 seconds
145
+ params = {uri: "ws://%s:%s" % [@sps_address, @sps_port]}
146
+
147
+ @ws = WebSocket::EventMachine::Client
148
+
149
+ EventMachine.run do
150
+
151
+ @ws.connect(params)
152
+
153
+ EM.add_periodic_timer(60) do
154
+ iterate @cron_events
155
+ end
156
+
183
157
  end
184
158
  end
185
159
 
186
160
  def to_a()
187
-
188
- return unless @entries
189
-
161
+
190
162
  @entries.inject([]) do |r,h|
191
163
 
192
- h[:cron] = ChronicCron.new(h[:date] + ' ' + h[:recurring].to_s )
193
- h[:job] ||= 'pub event: ' + h[:title]
164
+ h[:cron] = ChronicCron.new(h[:date])
165
+ h[:job] = 'pub event: ' + h[:title]
194
166
 
195
- if h[:reminder].to_s.length > 0 then
167
+ if h[:reminder].length > 0 then
196
168
  rmndr = {}
197
- rmndr[:cron] = ChronicCron.new((Chronic.parse(h[:date]) - ChronicDuration.parse(h[:reminder])).to_s)
169
+ rmndr[:cron] = ChronicCron.new((Chronic.parse(h[:date]) -
170
+ ChronicDuration.parse(h[:reminder])).to_s)
198
171
  rmndr[:job] = 'pub event: reminder ' + h[:title]
199
172
  r << rmndr
200
173
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynarex_cron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -31,7 +31,7 @@ cert_chain:
31
31
  y6cNz+NnybgoQfG28NcwV1e/31NTJk5VqdKHKnTfPcp/y35T9YoElNn5cSs93qc5
32
32
  U5VVkvsVQudaZw==
33
33
  -----END CERTIFICATE-----
34
- date: 2013-10-26 00:00:00.000000000 Z
34
+ date: 2013-11-22 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: dynarex
@@ -62,7 +62,7 @@ dependencies:
62
62
  - !ruby/object:Gem::Version
63
63
  version: '0'
64
64
  - !ruby/object:Gem::Dependency
65
- name: simplepubsub
65
+ name: websocket-eventmachine-client
66
66
  requirement: !ruby/object:Gem::Requirement
67
67
  requirements:
68
68
  - - ">="
metadata.gz.sig CHANGED
Binary file