lifeline 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -17,5 +17,6 @@ tmtags
17
17
  coverage
18
18
  rdoc
19
19
  pkg
20
+ .yardoc
20
21
 
21
22
  ## PROJECT::SPECIFIC
@@ -40,6 +40,15 @@ More specifically, the lifeline method (and rake task that calls it) does the fo
40
40
  * If there is another process with the same command string, return and exit
41
41
  * Otherwise yield to run the passed block.
42
42
 
43
+ Note that you do not pass a command name to the lifeline, it uses the command
44
+ of the process invoking the Lifeline code. This means if you call lifeline
45
+ code simultaneously from a Rake task and a Rails process, it will be executed
46
+ in both cases (it's NOT a mutex or semaphore). Also, this mechanism uses a
47
+ global namespace. means you will want to give unique names to the processes
48
+ that ultimately invoke your lifeline code. If app1 and app2 both have a rake
49
+ task named "lifeline," they will interfere with each other ("app1:lifeline"
50
+ and "app2:lifeline" are much better).
51
+
43
52
  == Examples
44
53
 
45
54
  Lifeline.lifeline do
@@ -51,6 +60,9 @@ More specifically, the lifeline method (and rake task that calls it) does the fo
51
60
  end
52
61
 
53
62
  > rake -T appname
63
+ rake appname:lifeline # A lifeline task for executing only one process of twitter:daemon:run at a time
64
+ rake appname:run # Runs the twitter:daemon:run task
65
+ rake appname:terminate # Terminates any running twitter:daemon:lifeline tasks
54
66
 
55
67
  == Note on Patches/Pull Requests
56
68
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -0,0 +1,473 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta name="Content-Type" content="text/html; charset=utf-8" />
6
+ <title>Module: Lifeline</title>
7
+ <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
8
+ <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
9
+
10
+ <script type="text/javascript" charset="utf-8">
11
+ relpath = '';
12
+ if (relpath != '') relpath += '/';
13
+ </script>
14
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
15
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
16
+
17
+ </head>
18
+ <body>
19
+ <script type="text/javascript" charset="utf-8">
20
+ if (window.top.frames.main) document.body.className = 'frames';
21
+ </script>
22
+
23
+ <div id="header">
24
+ <div id="menu">
25
+
26
+ <a href="_index.html">Index (L)</a> &raquo;
27
+
28
+
29
+ <span class="title">Lifeline</span>
30
+
31
+
32
+ <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
33
+ </div>
34
+
35
+ <div id="search">
36
+ <a id="class_list_link" href="#">Class List</a>
37
+ <a id="method_list_link" href="#">Method List</a>
38
+ <a id ="file_list_link" href="#">File List</a>
39
+ </div>
40
+
41
+ <div class="clear"></div>
42
+ </div>
43
+
44
+ <iframe id="search_frame"></iframe>
45
+
46
+ <div id="content"><h1>Module: Lifeline
47
+
48
+
49
+ </h1>
50
+
51
+ <dl class="box">
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+ <dt class="r1 last">Defined in:</dt>
61
+ <dd class="r1 last">lib/lifeline.rb</dd>
62
+
63
+ </dl>
64
+ <div class="clear"></div>
65
+
66
+ <h2>Defined Under Namespace</h2>
67
+ <p class="children">
68
+
69
+
70
+
71
+
72
+ <strong class="classes">Classes:</strong> <a href="Lifeline/LifelineRakeTask.html" title="Lifeline::LifelineRakeTask (class)">LifelineRakeTask</a>
73
+
74
+
75
+ </p>
76
+
77
+
78
+
79
+
80
+ <h2>Instance Method Summary</h2>
81
+
82
+ <ul class="summary">
83
+
84
+ <li class="public ">
85
+ <span class="summary_signature">
86
+
87
+ <a href="#define_lifeline_tasks-instance_method" title="#define_lifeline_tasks (instance method)">- (Object) <strong>define_lifeline_tasks</strong>(namespace, &amp;block) </a>
88
+
89
+
90
+
91
+ </span>
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+ <span class="summary_desc"><div class='inline'><p>
100
+ A method that defines 3 rake tasks for doing lifelines:
101
+ </p>
102
+ <ul>
103
+ <li><tt>namespace:run</tt> runs the specified block
104
+
105
+ </li>
106
+ <li><tt>namespace:lifeline</tt> a lifeline for executing only a single copy of
107
+ <tt>namespace:run</tt> at a time
108
+
109
+ </li>
110
+ <li><tt>namespace:terminate</tt> a task for terminating the lifelines.
111
+
112
+ </li>
113
+ </ul>
114
+ </div></span>
115
+
116
+ </li>
117
+
118
+
119
+ <li class="public ">
120
+ <span class="summary_signature">
121
+
122
+ <a href="#get_process_list-instance_method" title="#get_process_list (instance method)">- (Object) <strong>get_process_list</strong> </a>
123
+
124
+
125
+
126
+ </span>
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+ <span class="summary_desc"><div class='inline'></div></span>
135
+
136
+ </li>
137
+
138
+
139
+ <li class="public ">
140
+ <span class="summary_signature">
141
+
142
+ <a href="#lifeline-instance_method" title="#lifeline (instance method)">- (Object) <strong>lifeline</strong> </a>
143
+
144
+
145
+
146
+ </span>
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+ <span class="summary_desc"><div class='inline'><p>
155
+ A method for executing a block of code only if there is no other process
156
+ with the same command running.
157
+ </p>
158
+ </div></span>
159
+
160
+ </li>
161
+
162
+
163
+ </ul>
164
+
165
+
166
+
167
+
168
+ <div id="instance_method_details" class="method_details_list">
169
+ <h2>Instance Method Details</h2>
170
+
171
+
172
+ <div class="method_details first">
173
+ <p class="signature first" id="define_lifeline_tasks-instance_method">
174
+
175
+ - (<tt>Object</tt>) <strong>define_lifeline_tasks</strong>(namespace, &amp;block)
176
+
177
+
178
+
179
+ </p><div class="docstring">
180
+ <div class="discussion">
181
+ <p>
182
+ A method that defines 3 rake tasks for doing lifelines:
183
+ </p>
184
+ <ul>
185
+ <li><tt>namespace:run</tt> runs the specified block
186
+
187
+ </li>
188
+ <li><tt>namespace:lifeline</tt> a lifeline for executing only a single copy of
189
+ <tt>namespace:run</tt> at a time
190
+
191
+ </li>
192
+ <li><tt>namespace:terminate</tt> a task for terminating the lifelines
193
+
194
+ </li>
195
+ </ul>
196
+
197
+
198
+ </div>
199
+ </div>
200
+ <div class="tags">
201
+ <h3>Parameters:</h3>
202
+ <ul class="param">
203
+
204
+ <li>
205
+
206
+ <span class='type'>(<tt>String</tt>, <tt>Symbol</tt>)</span>
207
+
208
+
209
+ <span class='name'>namespace</span>
210
+
211
+
212
+
213
+ &mdash;
214
+ <div class='inline'><p>
215
+ the namespace to define the 3 tasks in
216
+ </p>
217
+ </div>
218
+
219
+ </li>
220
+
221
+ <li>
222
+
223
+ <span class='type'></span>
224
+
225
+
226
+ <span class='name'>&amp;block</span>
227
+
228
+
229
+
230
+ &mdash;
231
+ <div class='inline'><p>
232
+ a block which defines the body of the namespace:run method
233
+ </p>
234
+ </div>
235
+
236
+ </li>
237
+
238
+ </ul>
239
+ <h3>Raises:</h3>
240
+ <ul class="raise">
241
+
242
+ <li>
243
+
244
+ <span class='type'>(<tt>ArgumentError</tt>)</span>
245
+
246
+
247
+
248
+
249
+ &mdash;
250
+ <div class='inline'><p>
251
+ if you do not pass in a block argument
252
+ </p>
253
+ </div>
254
+
255
+ </li>
256
+
257
+ </ul>
258
+
259
+ </div><table class="source_code">
260
+ <tr>
261
+ <td>
262
+ <pre class="lines">
263
+
264
+
265
+ 129
266
+ 130
267
+ 131</pre>
268
+ </td>
269
+ <td>
270
+ <pre class="code"><span class="info file"># File 'lib/lifeline.rb', line 129</span>
271
+
272
+ <span class='def def kw'>def</span> <span class='define_lifeline_tasks identifier id'>define_lifeline_tasks</span><span class='lparen token'>(</span><span class='namespace identifier id'>namespace</span><span class='comma token'>,</span> <span class='bitand op'>&amp;</span><span class='block identifier id'>block</span><span class='rparen token'>)</span>
273
+ <span class='LifelineRakeTask constant id'>LifelineRakeTask</span><span class='dot token'>.</span><span class='new identifier id'>new</span><span class='lparen token'>(</span><span class='namespace identifier id'>namespace</span><span class='comma token'>,</span> <span class='bitand op'>&amp;</span><span class='block identifier id'>block</span><span class='rparen token'>)</span>
274
+ <span class='end end kw'>end</span>
275
+ </pre>
276
+ </td>
277
+ </tr>
278
+ </table>
279
+ </div>
280
+
281
+ <div class="method_details ">
282
+ <p class="signature " id="get_process_list-instance_method">
283
+
284
+ - (<tt>Object</tt>) <strong>get_process_list</strong>
285
+
286
+
287
+
288
+ </p><div class="docstring">
289
+ <div class="discussion">
290
+
291
+
292
+ </div>
293
+ </div>
294
+ <div class="tags">
295
+
296
+ </div><table class="source_code">
297
+ <tr>
298
+ <td>
299
+ <pre class="lines">
300
+
301
+
302
+ 7
303
+ 8
304
+ 9
305
+ 10
306
+ 11
307
+ 12
308
+ 13
309
+ 14
310
+ 15
311
+ 16
312
+ 17</pre>
313
+ </td>
314
+ <td>
315
+ <pre class="code"><span class="info file"># File 'lib/lifeline.rb', line 7</span>
316
+
317
+ <span class='def def kw'>def</span> <span class='get_process_list identifier id'>get_process_list</span>
318
+ <span class='processes identifier id'>processes</span> <span class='assign token'>=</span> <span class='xstring val'>%x{ps ax -o pid,command}</span>
319
+
320
+ <span class='return return kw'>return</span> <span class='nil nil kw'>nil</span> <span class='if if_mod kw'>if</span> <span class='processes identifier id'>processes</span><span class='dot token'>.</span><span class='nil? fid id'>nil?</span>
321
+
322
+ <span class='processes identifier id'>processes</span><span class='dot token'>.</span><span class='split identifier id'>split</span><span class='lparen token'>(</span><span class='regexp val'>/\n/</span><span class='rparen token'>)</span><span class='dot token'>.</span><span class='map identifier id'>map</span> <span class='do do kw'>do</span> <span class='bitor op'>|</span><span class='p identifier id'>p</span><span class='bitor op'>|</span>
323
+ <span class='if if kw'>if</span> <span class='p identifier id'>p</span> <span class='match op'>=~</span> <span class='regexp val'>/^(\d+)\s(.+)$/</span>
324
+ <span class='lbrace token'>{</span><span class='symbol val'>:pid</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='$1 nth_ref id'>$1</span><span class='dot token'>.</span><span class='to_i identifier id'>to_i</span><span class='comma token'>,</span> <span class='symbol val'>:command</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='$2 nth_ref id'>$2</span><span class='dot token'>.</span><span class='strip identifier id'>strip</span><span class='rbrace token'>}</span>
325
+ <span class='end end kw'>end</span>
326
+ <span class='end end kw'>end</span><span class='dot token'>.</span><span class='compact identifier id'>compact</span>
327
+ <span class='end end kw'>end</span>
328
+ </pre>
329
+ </td>
330
+ </tr>
331
+ </table>
332
+ </div>
333
+
334
+ <div class="method_details ">
335
+ <p class="signature " id="lifeline-instance_method">
336
+
337
+ - (<tt>Object</tt>) <strong>lifeline</strong>
338
+
339
+
340
+
341
+ </p><div class="docstring">
342
+ <div class="discussion">
343
+ <p>
344
+ A method for executing a block of code only if there is no other process
345
+ with the same command running. This is useful if you want to have a
346
+ perpetually running daemon that only executes once at a time. It uses the
347
+ process name returned by ps ax to see if there is a process with the same
348
+ command name but a different PID already executing. If so, it terminates
349
+ without running the block. NOTE: since it uses the command name returned
350
+ from <tt>ps ax</tt>, it us up to to you to name the process containing this
351
+ code with a distinctly unique name. If two separate Rails projects both
352
+ have a <tt>rake lifeline</tt> task they WILL interfere with each other.
353
+ I&#8217;d suggest prefixing with the project name (ie,
354
+ <tt>doc_viewer:lifeline</tt>) to be sure
355
+ </p>
356
+
357
+
358
+ </div>
359
+ </div>
360
+ <div class="tags">
361
+ <h3>Parameters:</h3>
362
+ <ul class="param">
363
+
364
+ <li>
365
+
366
+ <span class='type'></span>
367
+
368
+
369
+ <span class='name'>&amp;block</span>
370
+
371
+
372
+
373
+ &mdash;
374
+ <div class='inline'><p>
375
+ a block which is executed if there is not already a lifeline running.
376
+ </p>
377
+ </div>
378
+
379
+ </li>
380
+
381
+ </ul>
382
+ <h3>Raises:</h3>
383
+ <ul class="raise">
384
+
385
+ <li>
386
+
387
+ <span class='type'>(<tt>ArgumentError</tt>)</span>
388
+
389
+
390
+
391
+
392
+ &mdash;
393
+ <div class='inline'><p>
394
+ if you do not pass in a block argument
395
+ </p>
396
+ </div>
397
+
398
+ </li>
399
+
400
+ </ul>
401
+
402
+ </div><table class="source_code">
403
+ <tr>
404
+ <td>
405
+ <pre class="lines">
406
+
407
+
408
+ 29
409
+ 30
410
+ 31
411
+ 32
412
+ 33
413
+ 34
414
+ 35
415
+ 36
416
+ 37
417
+ 38
418
+ 39
419
+ 40
420
+ 41
421
+ 42
422
+ 43
423
+ 44
424
+ 45
425
+ 46
426
+ 47
427
+ 48
428
+ 49
429
+ 50</pre>
430
+ </td>
431
+ <td>
432
+ <pre class="code"><span class="info file"># File 'lib/lifeline.rb', line 29</span>
433
+
434
+ <span class='def def kw'>def</span> <span class='lifeline identifier id'>lifeline</span>
435
+ <span class='if if kw'>if</span> <span class='notop op'>!</span><span class='block_given? fid id'>block_given?</span>
436
+ <span class='raise identifier id'>raise</span> <span class='ArgumentError constant id'>ArgumentError</span><span class='comma token'>,</span> <span class='string val'>&quot;You must pass in a block to be the body of the run rake task&quot;</span>
437
+ <span class='end end kw'>end</span>
438
+
439
+ <span class='my_pid identifier id'>my_pid</span> <span class='assign token'>=</span> <span class='$$ gvar id'>$$</span>
440
+ <span class='processes identifier id'>processes</span> <span class='assign token'>=</span> <span class='get_process_list identifier id'>get_process_list</span>
441
+
442
+ <span class='if if kw'>if</span> <span class='processes identifier id'>processes</span><span class='dot token'>.</span><span class='nil? fid id'>nil?</span> <span class='orop op'>||</span> <span class='processes identifier id'>processes</span><span class='dot token'>.</span><span class='empty? fid id'>empty?</span>
443
+ <span class='raise identifier id'>raise</span> <span class='string val'>&quot;No processes being returned by get_process_list. Aborting!&quot;</span>
444
+ <span class='end end kw'>end</span>
445
+
446
+ <span class='myself identifier id'>myself</span> <span class='assign token'>=</span> <span class='processes identifier id'>processes</span><span class='dot token'>.</span><span class='detect identifier id'>detect</span> <span class='lbrace token'>{</span><span class='bitor op'>|</span><span class='p identifier id'>p</span><span class='bitor op'>|</span> <span class='p identifier id'>p</span><span class='lbrack token'>[</span><span class='symbol val'>:pid</span><span class='rbrack token'>]</span> <span class='eq op'>==</span> <span class='my_pid identifier id'>my_pid</span><span class='rbrace token'>}</span>
447
+ <span class='if if kw'>if</span> <span class='myself identifier id'>myself</span><span class='dot token'>.</span><span class='nil? fid id'>nil?</span>
448
+ <span class='raise identifier id'>raise</span> <span class='string val'>&quot;Unable to find self in process list. This is bizarre to say the least. Exiting&quot;</span>
449
+ <span class='end end kw'>end</span>
450
+
451
+ <span class='comment val'># there isn't already another process running with the same command</span>
452
+ <span class='if if kw'>if</span> <span class='notop op'>!</span><span class='processes identifier id'>processes</span><span class='dot token'>.</span><span class='any? fid id'>any?</span> <span class='lbrace token'>{</span><span class='bitor op'>|</span><span class='p identifier id'>p</span><span class='bitor op'>|</span> <span class='p identifier id'>p</span><span class='lbrack token'>[</span><span class='symbol val'>:pid</span><span class='rbrack token'>]</span> <span class='neq op'>!=</span> <span class='my_pid identifier id'>my_pid</span> <span class='andop op'>&amp;&amp;</span> <span class='p identifier id'>p</span><span class='lbrack token'>[</span><span class='symbol val'>:command</span><span class='rbrack token'>]</span> <span class='eq op'>==</span> <span class='myself identifier id'>myself</span><span class='lbrack token'>[</span><span class='symbol val'>:command</span><span class='rbrack token'>]</span><span class='rbrace token'>}</span>
453
+ <span class='yield yield kw'>yield</span>
454
+ <span class='end end kw'>end</span>
455
+ <span class='end end kw'>end</span>
456
+ </pre>
457
+ </td>
458
+ </tr>
459
+ </table>
460
+ </div>
461
+
462
+ </div>
463
+
464
+ </div>
465
+
466
+ <div id="footer">
467
+ Generated on Tue Apr 13 12:07:29 2010 by
468
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool">yard</a>
469
+ 0.5.4 (ruby-1.8.7).
470
+ </div>
471
+
472
+ </body>
473
+ </html>