vidibus-recording 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -44,8 +44,7 @@ module Vidibus::Recording
44
44
  if time == :now
45
45
  self.started_at = Time.now
46
46
  self.active = true
47
- start_worker
48
- save!
47
+ start!
49
48
  else
50
49
  schedule(time)
51
50
  end
@@ -57,8 +56,7 @@ module Vidibus::Recording
57
56
  self.stopped_at = nil
58
57
  self.failed_at = nil
59
58
  self.active = true
60
- start_worker
61
- save!
59
+ start!
62
60
  end
63
61
 
64
62
  # Resets data and starts anew.
@@ -71,34 +69,37 @@ module Vidibus::Recording
71
69
  # Stops the recording worker and starts postprocessing.
72
70
  def stop
73
71
  return false if done? || !started?
74
- stop_worker
75
- self.pid = nil
76
- self.stopped_at = Time.now
77
- self.running = false
78
- self.active = false
79
- postprocess
72
+ stop_worker do
73
+ self.pid = nil
74
+ self.stopped_at = Time.now
75
+ self.running = false
76
+ self.active = false
77
+ postprocess
78
+ end
80
79
  end
81
80
 
82
81
  # Gets called from recording worker if it receives no more data.
83
- def halt(msg = nil)
82
+ def halt
84
83
  return false unless running?
85
- stop_worker
86
- self.pid = nil
87
- self.running = false
88
- postprocess
84
+ stop_worker do
85
+ self.pid = nil
86
+ self.running = false
87
+ postprocess
88
+ end
89
89
  end
90
90
 
91
91
  # Receives an error from recording worker and stores it.
92
92
  # The worker gets stopped and postprocessing is started.
93
93
  def fail(msg)
94
94
  return false unless running?
95
- stop_worker
96
- self.pid = nil
97
- self.error = msg
98
- self.failed_at = Time.now
99
- self.running = false
100
- self.active = false
101
- postprocess
95
+ stop_worker do
96
+ self.pid = nil
97
+ self.error = msg
98
+ self.failed_at = Time.now
99
+ self.running = false
100
+ self.active = false
101
+ postprocess
102
+ end
102
103
  end
103
104
 
104
105
  # TODO: really a public method?
@@ -161,11 +162,15 @@ module Vidibus::Recording
161
162
  # Returns true if recording worker is still running.
162
163
  # Persists attributes accordingly.
163
164
  def worker_running?
164
- if worker.running?
165
- update_attributes(:running => true) unless running?
165
+ if fresh_worker.running?
166
+ unless running?
167
+ self.update_attributes!(:running => true)
168
+ end
166
169
  true
167
170
  else
168
- update_attributes(:pid => nil, :running => false)
171
+ if running?
172
+ self.update_attributes!(:pid => nil, :running => false)
173
+ end
169
174
  false
170
175
  end
171
176
  end
@@ -225,13 +230,43 @@ module Vidibus::Recording
225
230
  def start_worker
226
231
  return if worker_running?
227
232
  setup_next_part
228
- worker.start
233
+ fresh_worker.start
234
+ end
235
+
236
+ def ensure_pid
237
+ unless worker.pid
238
+ fail('Worker did not return a PID!') and return
239
+ end
240
+ unless self.reload.pid == worker.pid
241
+ fail('Worker PID could not be stored!') and return
242
+ end
243
+ end
244
+
245
+ def start!
246
+ start_worker
229
247
  self.running = true
230
248
  self.pid = worker.pid
249
+ save!
250
+ ensure_pid
251
+ end
252
+
253
+ # Stop worker and then call block.
254
+ # If this method is invoked (indirectly) by a running worker process
255
+ # the block is called before exiting the process.
256
+ def stop_worker(&block)
257
+ worker = fresh_worker
258
+ if worker.pid = Process.pid
259
+ block.call
260
+ worker.stop
261
+ else
262
+ worker.stop
263
+ block.call
264
+ end
231
265
  end
232
266
 
233
- def stop_worker
234
- worker.stop
267
+ def fresh_worker
268
+ @worker = nil
269
+ worker
235
270
  end
236
271
 
237
272
  def setup_next_part
@@ -1,5 +1,5 @@
1
1
  module Vidibus
2
2
  module Recording
3
- VERSION = '2.0.1'
3
+ VERSION = '2.0.2'
4
4
  end
5
5
  end
@@ -113,12 +113,33 @@ module Vidibus::Recording
113
113
 
114
114
  def fail(msg)
115
115
  log("ERROR: #{msg}", true)
116
- recording.reload.fail(msg)
116
+ with_fresh_recording do |recording|
117
+ recording.fail(msg)
118
+ exit!
119
+ end
117
120
  end
118
121
 
119
122
  def halt(msg)
120
123
  log("HALT: #{msg}", true)
121
- recording.reload.halt(msg)
124
+ with_fresh_recording do |recording|
125
+ recording.halt
126
+ exit!
127
+ end
128
+ end
129
+
130
+ def with_fresh_recording(&block)
131
+ rec = recording.reload # reload to get fresh object
132
+ if rec.pid == Process.pid
133
+ block.call(rec)
134
+ else
135
+ exit!
136
+ end
137
+ end
138
+
139
+ def exit!
140
+ self.pid = Process.pid
141
+ stop
142
+ exit
122
143
  end
123
144
 
124
145
  def extract_metadata(string)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-recording
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-13 00:00:00.000000000 Z
12
+ date: 2013-08-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -197,7 +197,8 @@ files:
197
197
  - README.md
198
198
  - Rakefile
199
199
  homepage: https://github.com/vidibus/vidibus-recording
200
- licenses: []
200
+ licenses:
201
+ - MIT
201
202
  post_install_message:
202
203
  rdoc_options: []
203
204
  require_paths:
@@ -210,7 +211,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
210
211
  version: '0'
211
212
  segments:
212
213
  - 0
213
- hash: 432343217845410671
214
+ hash: 876306069205502720
214
215
  required_rubygems_version: !ruby/object:Gem::Requirement
215
216
  none: false
216
217
  requirements: