rask 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/doc/Rask.html +164 -87
- data/doc/created.rid +1 -1
- data/doc/index.html +17 -13
- data/doc/lib/rask_rb.html +1 -1
- data/lib/rask.rb +60 -57
- metadata +2 -2
data/History.txt
CHANGED
data/doc/Rask.html
CHANGED
@@ -61,33 +61,37 @@
|
|
61
61
|
<h3 class="section-header">Methods</h3>
|
62
62
|
<ul class="link-list">
|
63
63
|
|
64
|
+
<li><a href="#M000008">::base_directory</a></li>
|
65
|
+
|
64
66
|
<li><a href="#M000007">::base_directory=</a></li>
|
65
67
|
|
66
|
-
<li><a href="#
|
68
|
+
<li><a href="#M000018">::daemon</a></li>
|
69
|
+
|
70
|
+
<li><a href="#M000017">::destroy</a></li>
|
67
71
|
|
68
|
-
<li><a href="#
|
72
|
+
<li><a href="#M000019">::initialize_storage</a></li>
|
69
73
|
|
70
|
-
<li><a href="#
|
74
|
+
<li><a href="#M000012">::insert</a></li>
|
71
75
|
|
72
|
-
<li><a href="#M000011">::
|
76
|
+
<li><a href="#M000011">::pid_path</a></li>
|
73
77
|
|
74
|
-
<li><a href="#
|
78
|
+
<li><a href="#M000015">::read</a></li>
|
75
79
|
|
76
|
-
<li><a href="#
|
80
|
+
<li><a href="#M000013">::run</a></li>
|
77
81
|
|
78
|
-
<li><a href="#
|
82
|
+
<li><a href="#M000014">::run_all</a></li>
|
79
83
|
|
80
|
-
<li><a href="#
|
84
|
+
<li><a href="#M000020">::safe_class_name</a></li>
|
81
85
|
|
82
|
-
<li><a href="#
|
86
|
+
<li><a href="#M000022">::safe_exit</a></li>
|
83
87
|
|
84
|
-
<li><a href="#
|
88
|
+
<li><a href="#M000016">::task_ids</a></li>
|
85
89
|
|
86
|
-
<li><a href="#
|
90
|
+
<li><a href="#M000010">::task_path</a></li>
|
87
91
|
|
88
|
-
<li><a href="#M000009">::
|
92
|
+
<li><a href="#M000009">::thread_max_count=</a></li>
|
89
93
|
|
90
|
-
<li><a href="#
|
94
|
+
<li><a href="#M000021">::thread_proc</a></li>
|
91
95
|
|
92
96
|
</ul>
|
93
97
|
</div>
|
@@ -192,6 +196,38 @@
|
|
192
196
|
<h3 class="section-header">Public Class Methods</h3>
|
193
197
|
|
194
198
|
|
199
|
+
<div id="base-directory-method" class="method-detail ">
|
200
|
+
<a name="M000008"></a>
|
201
|
+
|
202
|
+
<div class="method-heading">
|
203
|
+
|
204
|
+
<span class="method-name">base_directory</span><span
|
205
|
+
class="method-args">()</span>
|
206
|
+
<span class="method-click-advice">click to toggle source</span>
|
207
|
+
|
208
|
+
</div>
|
209
|
+
|
210
|
+
<div class="method-description">
|
211
|
+
|
212
|
+
<p class="missing-docs">(Not documented)</p>
|
213
|
+
|
214
|
+
|
215
|
+
|
216
|
+
<div class="method-source-code"
|
217
|
+
id="base-directory-source">
|
218
|
+
<pre>
|
219
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 148</span>
|
220
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">base_directory</span>
|
221
|
+
<span class="ruby-ivar">@@base_dir</span>
|
222
|
+
<span class="ruby-keyword kw">end</span></pre>
|
223
|
+
</div>
|
224
|
+
|
225
|
+
</div>
|
226
|
+
|
227
|
+
|
228
|
+
</div>
|
229
|
+
|
230
|
+
|
195
231
|
<div id="base-directory--method" class="method-detail ">
|
196
232
|
<a name="M000007"></a>
|
197
233
|
|
@@ -205,7 +241,7 @@
|
|
205
241
|
|
206
242
|
<div class="method-description">
|
207
243
|
|
208
|
-
<h3>Set base storage directory</h3>
|
244
|
+
<h3>Set/Get base storage directory</h3>
|
209
245
|
<table>
|
210
246
|
<tr><td valign="top">default :</td><td>/tmp/rask
|
211
247
|
|
@@ -230,12 +266,12 @@
|
|
230
266
|
|
231
267
|
|
232
268
|
<div id="daemon-method" class="method-detail ">
|
233
|
-
<a name="
|
269
|
+
<a name="M000018"></a>
|
234
270
|
|
235
271
|
<div class="method-heading">
|
236
272
|
|
237
273
|
<span class="method-name">daemon</span><span
|
238
|
-
class="method-args">(options = {:class=>nil, :group=>nil, :sleep=>0.1})</span>
|
274
|
+
class="method-args">(options = {:class=>nil, :group=>nil, :sleep=>0.1, :worker_sleep=>0.1, :process_name=>nil})</span>
|
239
275
|
<span class="method-click-advice">click to toggle source</span>
|
240
276
|
|
241
277
|
</div>
|
@@ -262,45 +298,27 @@
|
|
262
298
|
<div class="method-source-code"
|
263
299
|
id="daemon-source">
|
264
300
|
<pre>
|
265
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
266
|
-
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">daemon</span>(<span class="ruby-identifier">options</span> = {<span class="ruby-identifier">:class</span>=<span class="ruby-operator">></span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:group=</span><span class="ruby-operator">></span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:sleep=</span><span class="ruby-operator">></span><span class="ruby-value">0</span><span class="ruby-value">.1</span>})
|
267
|
-
<span class="ruby-identifier">options</span> = { <span class="ruby-identifier">:sleep=</span><span class="ruby-operator">></span><span class="ruby-value">0</span><span class="ruby-value">.1</span> }.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
|
301
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 273</span>
|
302
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">daemon</span>(<span class="ruby-identifier">options</span> = {<span class="ruby-identifier">:class</span>=<span class="ruby-operator">></span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:group=</span><span class="ruby-operator">></span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:sleep=</span><span class="ruby-operator">></span><span class="ruby-value">0</span><span class="ruby-value">.1</span>, <span class="ruby-identifier">:worker_sleep=</span><span class="ruby-operator">></span><span class="ruby-value">0</span><span class="ruby-value">.1</span>, <span class="ruby-identifier">:process_name=</span><span class="ruby-operator">></span><span class="ruby-keyword kw">nil</span>})
|
303
|
+
<span class="ruby-identifier">options</span> = { <span class="ruby-identifier">:sleep=</span><span class="ruby-operator">></span><span class="ruby-value">0</span><span class="ruby-value">.1</span>, <span class="ruby-identifier">:worker_sleep=</span><span class="ruby-operator">></span><span class="ruby-value">0</span><span class="ruby-value">.1</span>, <span class="ruby-identifier">:process_name=</span><span class="ruby-operator">></span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:threading=</span><span class="ruby-operator">></span><span class="ruby-keyword kw">true</span> }.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
|
268
304
|
<span class="ruby-identifier">print</span> <span class="ruby-value str">"daemon start\n"</span>
|
269
305
|
<span class="ruby-identifier">exit</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">fork</span>
|
270
306
|
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">setsid</span>
|
271
|
-
|
307
|
+
|
308
|
+
<span class="ruby-identifier">initialize_storage</span>
|
309
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span> <span class="ruby-identifier">pid_path</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:process_name</span>])
|
272
310
|
<span class="ruby-identifier">print</span> <span class="ruby-node">"already running rask process. #{File.basename($0)}"</span>
|
273
311
|
<span class="ruby-keyword kw">return</span>
|
274
312
|
<span class="ruby-keyword kw">end</span>
|
275
|
-
<span class="ruby-identifier">open</span>(<span class="ruby-identifier">pid_path</span
|
313
|
+
<span class="ruby-identifier">open</span>(<span class="ruby-identifier">pid_path</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:process_name</span>]),<span class="ruby-value str">"w"</span>){<span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">write</span> <span class="ruby-constant">Process</span>.<span class="ruby-identifier">pid</span>}
|
276
314
|
|
277
315
|
<span class="ruby-comment cmt"># create worker threads</span>
|
278
316
|
<span class="ruby-identifier">threads</span> = []
|
279
317
|
<span class="ruby-keyword kw">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword kw">in</span> <span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-ivar">@@thread_max_count</span> <span class="ruby-keyword kw">do</span>
|
280
|
-
<span class="ruby-identifier">threads</span> <span class="ruby-operator"><<</span> <span class="ruby-constant">Thread</span><span class="ruby-operator">::</span><span class="ruby-identifier">new</span>(<span class="ruby-identifier">i</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">thread_id</span><span class="ruby-operator">|</span>
|
281
|
-
<span class="ruby-ivar">@@thread_count</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
|
282
|
-
<span class="ruby-keyword kw">while</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@@terminated</span>
|
283
|
-
<span class="ruby-identifier">d</span> = <span class="ruby-keyword kw">nil</span>
|
284
|
-
<span class="ruby-ivar">@@locker</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span>
|
285
|
-
<span class="ruby-identifier">d</span> = <span class="ruby-ivar">@@queue</span>.<span class="ruby-identifier">pop</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@@queue</span>.<span class="ruby-identifier">empty?</span>
|
286
|
-
<span class="ruby-keyword kw">end</span>
|
287
|
-
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">d</span> <span class="ruby-operator">!=</span> <span class="ruby-keyword kw">nil</span>
|
288
|
-
<span class="ruby-comment cmt"># print "#{d}\n"</span>
|
289
|
-
<span class="ruby-identifier">run</span>(<span class="ruby-identifier">d</span>)
|
290
|
-
<span class="ruby-ivar">@@locker</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span>
|
291
|
-
<span class="ruby-ivar">@@processing</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">d</span>)
|
292
|
-
<span class="ruby-keyword kw">end</span>
|
293
|
-
<span class="ruby-keyword kw">else</span>
|
294
|
-
<span class="ruby-comment cmt"># print "no data in queue\n"</span>
|
295
|
-
<span class="ruby-identifier">sleep</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:sleep</span>])
|
296
|
-
<span class="ruby-keyword kw">end</span>
|
297
|
-
<span class="ruby-keyword kw">end</span>
|
298
|
-
<span class="ruby-identifier">print</span> <span class="ruby-node">"#{thread_id}"</span>
|
299
|
-
<span class="ruby-ivar">@@thread_count</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span>
|
300
|
-
}
|
318
|
+
<span class="ruby-identifier">threads</span> <span class="ruby-operator"><<</span> <span class="ruby-constant">Thread</span><span class="ruby-operator">::</span><span class="ruby-identifier">new</span>(<span class="ruby-identifier">i</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">thread_id</span><span class="ruby-operator">|</span> <span class="ruby-identifier">thread_proc</span>(<span class="ruby-identifier">thread_id</span>, <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:worker_sleep</span>]) }
|
301
319
|
<span class="ruby-keyword kw">end</span>
|
302
320
|
|
303
|
-
<span class="ruby-constant">Signal</span>.<span class="ruby-identifier">trap</span>(<span class="ruby-identifier">:TERM</span>) {<span class="ruby-identifier">safe_exit</span>}
|
321
|
+
<span class="ruby-constant">Signal</span>.<span class="ruby-identifier">trap</span>(<span class="ruby-identifier">:TERM</span>) {<span class="ruby-identifier">safe_exit</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:process_name</span>])}
|
304
322
|
|
305
323
|
<span class="ruby-keyword kw">while</span> <span class="ruby-keyword kw">true</span>
|
306
324
|
<span class="ruby-identifier">task_list</span> = <span class="ruby-constant">Rask</span>.<span class="ruby-identifier">task_ids</span>(<span class="ruby-identifier">options</span>)
|
@@ -324,7 +342,7 @@
|
|
324
342
|
|
325
343
|
|
326
344
|
<div id="destroy-method" class="method-detail ">
|
327
|
-
<a name="
|
345
|
+
<a name="M000017"></a>
|
328
346
|
|
329
347
|
<div class="method-heading">
|
330
348
|
|
@@ -343,7 +361,7 @@
|
|
343
361
|
<div class="method-source-code"
|
344
362
|
id="destroy-source">
|
345
363
|
<pre>
|
346
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
364
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 262</span>
|
347
365
|
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">destroy</span>(<span class="ruby-identifier">task</span>)
|
348
366
|
<span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">rm</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task</span>.<span class="ruby-identifier">task_id</span>)) <span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span> <span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task</span>.<span class="ruby-identifier">task_id</span>)
|
349
367
|
<span class="ruby-keyword kw">end</span></pre>
|
@@ -356,7 +374,7 @@
|
|
356
374
|
|
357
375
|
|
358
376
|
<div id="insert-method" class="method-detail ">
|
359
|
-
<a name="
|
377
|
+
<a name="M000012"></a>
|
360
378
|
|
361
379
|
<div class="method-heading">
|
362
380
|
|
@@ -379,7 +397,7 @@
|
|
379
397
|
<div class="method-source-code"
|
380
398
|
id="insert-source">
|
381
399
|
<pre>
|
382
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
400
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 176</span>
|
383
401
|
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">insert</span>(<span class="ruby-identifier">task</span>)
|
384
402
|
<span class="ruby-identifier">initialize_storage</span>
|
385
403
|
<span class="ruby-identifier">task_id</span> = <span class="ruby-node">"#{safe_class_name(task.class.name)}-#{task.group.to_s}-#{Time.now.to_i}-#{Time.now.usec}"</span>
|
@@ -400,12 +418,12 @@
|
|
400
418
|
|
401
419
|
|
402
420
|
<div id="pid-path-method" class="method-detail ">
|
403
|
-
<a name="
|
421
|
+
<a name="M000011"></a>
|
404
422
|
|
405
423
|
<div class="method-heading">
|
406
424
|
|
407
425
|
<span class="method-name">pid_path</span><span
|
408
|
-
class="method-args">()</span>
|
426
|
+
class="method-args">(process_name = nil)</span>
|
409
427
|
<span class="method-click-advice">click to toggle source</span>
|
410
428
|
|
411
429
|
</div>
|
@@ -419,9 +437,10 @@
|
|
419
437
|
<div class="method-source-code"
|
420
438
|
id="pid-path-source">
|
421
439
|
<pre>
|
422
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
423
|
-
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">pid_path</span>
|
424
|
-
<span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-node">"/#{File.basename($0)}.pid"</span>
|
440
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 166</span>
|
441
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">pid_path</span>(<span class="ruby-identifier">process_name</span> = <span class="ruby-keyword kw">nil</span>)
|
442
|
+
<span class="ruby-keyword kw">return</span> <span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-node">"/#{File.basename($0)}.pid"</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">process_name</span><span class="ruby-operator">==</span><span class="ruby-keyword kw">nil</span>
|
443
|
+
<span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-node">"/#{process_name}.pid"</span>
|
425
444
|
<span class="ruby-keyword kw">end</span></pre>
|
426
445
|
</div>
|
427
446
|
|
@@ -432,7 +451,7 @@
|
|
432
451
|
|
433
452
|
|
434
453
|
<div id="read-method" class="method-detail ">
|
435
|
-
<a name="
|
454
|
+
<a name="M000015"></a>
|
436
455
|
|
437
456
|
<div class="method-heading">
|
438
457
|
|
@@ -454,7 +473,7 @@ You can use the instance for only the purpose of observation.
|
|
454
473
|
<div class="method-source-code"
|
455
474
|
id="read-source">
|
456
475
|
<pre>
|
457
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
476
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 224</span>
|
458
477
|
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">task_id</span>)
|
459
478
|
<span class="ruby-identifier">f</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>), <span class="ruby-value str">'r+'</span>) <span class="ruby-keyword kw">rescue</span> <span class="ruby-keyword kw">return</span>
|
460
479
|
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_EX</span>)
|
@@ -473,7 +492,7 @@ You can use the instance for only the purpose of observation.
|
|
473
492
|
|
474
493
|
|
475
494
|
<div id="run-method" class="method-detail ">
|
476
|
-
<a name="
|
495
|
+
<a name="M000013"></a>
|
477
496
|
|
478
497
|
<div class="method-heading">
|
479
498
|
|
@@ -492,22 +511,30 @@ You can use the instance for only the purpose of observation.
|
|
492
511
|
<div class="method-source-code"
|
493
512
|
id="run-source">
|
494
513
|
<pre>
|
495
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
514
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 190</span>
|
496
515
|
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">run</span>(<span class="ruby-identifier">task_id</span>)
|
497
516
|
<span class="ruby-identifier">f</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>), <span class="ruby-value str">'r+'</span>) <span class="ruby-keyword kw">rescue</span> <span class="ruby-keyword kw">return</span>
|
498
517
|
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_EX</span>)
|
499
518
|
<span class="ruby-identifier">task</span> = <span class="ruby-constant">Marshal</span>.<span class="ruby-identifier">restore</span>(<span class="ruby-identifier">f</span>)
|
500
|
-
<span class="ruby-keyword kw">
|
501
|
-
<span class="ruby-keyword kw">
|
502
|
-
|
503
|
-
<span class="ruby-
|
519
|
+
<span class="ruby-keyword kw">begin</span>
|
520
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
|
521
|
+
<span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">task</span>
|
522
|
+
<span class="ruby-keyword kw">else</span>
|
523
|
+
<span class="ruby-identifier">task</span>.<span class="ruby-identifier">run</span>
|
524
|
+
<span class="ruby-keyword kw">end</span>
|
525
|
+
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">truncate</span>(<span class="ruby-value">0</span>)
|
526
|
+
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">pos</span> = <span class="ruby-value">0</span>
|
527
|
+
<span class="ruby-constant">Marshal</span>.<span class="ruby-identifier">dump</span>(<span class="ruby-identifier">task</span>, <span class="ruby-identifier">f</span>)
|
528
|
+
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_UN</span>)
|
529
|
+
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">close</span>
|
530
|
+
<span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">rm</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>)) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">task</span>.<span class="ruby-identifier">destroy?</span>
|
531
|
+
<span class="ruby-keyword kw">rescue</span>
|
532
|
+
<span class="ruby-identifier">p</span> <span class="ruby-identifier">$!</span>
|
533
|
+
<span class="ruby-identifier">print</span> <span class="ruby-identifier">$@</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">"\n"</span>) <span class="ruby-operator">+</span> <span class="ruby-value str">"\n--------------------------------------------\n"</span>
|
534
|
+
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_UN</span>)
|
535
|
+
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">close</span>
|
536
|
+
<span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">mv</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>), <span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-value str">"/suspended/"</span>)
|
504
537
|
<span class="ruby-keyword kw">end</span>
|
505
|
-
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">truncate</span>(<span class="ruby-value">0</span>)
|
506
|
-
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">pos</span> = <span class="ruby-value">0</span>
|
507
|
-
<span class="ruby-constant">Marshal</span>.<span class="ruby-identifier">dump</span>(<span class="ruby-identifier">task</span>, <span class="ruby-identifier">f</span>)
|
508
|
-
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">LOCK_UN</span>)
|
509
|
-
<span class="ruby-identifier">f</span>.<span class="ruby-identifier">close</span>
|
510
|
-
<span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">rm</span>(<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>)) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">task</span>.<span class="ruby-identifier">destroy?</span>
|
511
538
|
<span class="ruby-keyword kw">end</span></pre>
|
512
539
|
</div>
|
513
540
|
|
@@ -518,7 +545,7 @@ You can use the instance for only the purpose of observation.
|
|
518
545
|
|
519
546
|
|
520
547
|
<div id="run-all-method" class="method-detail ">
|
521
|
-
<a name="
|
548
|
+
<a name="M000014"></a>
|
522
549
|
|
523
550
|
<div class="method-heading">
|
524
551
|
|
@@ -537,7 +564,7 @@ You can use the instance for only the purpose of observation.
|
|
537
564
|
<div class="method-source-code"
|
538
565
|
id="run-all-source">
|
539
566
|
<pre>
|
540
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
567
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 216</span>
|
541
568
|
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">run_all</span>(<span class="ruby-identifier">options</span> = { <span class="ruby-identifier">:class</span>=<span class="ruby-operator">></span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:group=</span><span class="ruby-operator">></span><span class="ruby-keyword kw">nil</span> })
|
542
569
|
<span class="ruby-constant">Rask</span>.<span class="ruby-identifier">task_ids</span>(<span class="ruby-identifier">options</span>).<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">task_id</span><span class="ruby-operator">|</span> <span class="ruby-identifier">run</span>(<span class="ruby-identifier">task_id</span>) }
|
543
570
|
<span class="ruby-keyword kw">end</span></pre>
|
@@ -550,7 +577,7 @@ You can use the instance for only the purpose of observation.
|
|
550
577
|
|
551
578
|
|
552
579
|
<div id="task-ids-method" class="method-detail ">
|
553
|
-
<a name="
|
580
|
+
<a name="M000016"></a>
|
554
581
|
|
555
582
|
<div class="method-heading">
|
556
583
|
|
@@ -579,7 +606,7 @@ You can use the instance for only the purpose of observation.
|
|
579
606
|
<div class="method-source-code"
|
580
607
|
id="task-ids-source">
|
581
608
|
<pre>
|
582
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
609
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 240</span>
|
583
610
|
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">task_ids</span>(<span class="ruby-identifier">options</span> = { <span class="ruby-identifier">:class</span>=<span class="ruby-operator">></span><span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">:group=</span><span class="ruby-operator">></span><span class="ruby-keyword kw">nil</span> })
|
584
611
|
<span class="ruby-identifier">target</span> = <span class="ruby-ivar">@@base_dir</span>
|
585
612
|
<span class="ruby-identifier">target</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">'/'</span>
|
@@ -607,7 +634,7 @@ You can use the instance for only the purpose of observation.
|
|
607
634
|
|
608
635
|
|
609
636
|
<div id="task-path-method" class="method-detail ">
|
610
|
-
<a name="
|
637
|
+
<a name="M000010"></a>
|
611
638
|
|
612
639
|
<div class="method-heading">
|
613
640
|
|
@@ -626,7 +653,7 @@ You can use the instance for only the purpose of observation.
|
|
626
653
|
<div class="method-source-code"
|
627
654
|
id="task-path-source">
|
628
655
|
<pre>
|
629
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
656
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 161</span>
|
630
657
|
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">task_path</span>(<span class="ruby-identifier">task_id</span>)
|
631
658
|
<span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-node">"/#{task_id}.task"</span>
|
632
659
|
<span class="ruby-keyword kw">end</span></pre>
|
@@ -639,7 +666,7 @@ You can use the instance for only the purpose of observation.
|
|
639
666
|
|
640
667
|
|
641
668
|
<div id="thread-max-count--method" class="method-detail ">
|
642
|
-
<a name="
|
669
|
+
<a name="M000009"></a>
|
643
670
|
|
644
671
|
<div class="method-heading">
|
645
672
|
|
@@ -663,7 +690,7 @@ You can use the instance for only the purpose of observation.
|
|
663
690
|
<div class="method-source-code"
|
664
691
|
id="thread-max-count--source">
|
665
692
|
<pre>
|
666
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
693
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 156</span>
|
667
694
|
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">thread_max_count=</span>(<span class="ruby-identifier">count</span>)
|
668
695
|
<span class="ruby-ivar">@@thread_max_count</span> = <span class="ruby-identifier">count</span>
|
669
696
|
<span class="ruby-keyword kw">end</span></pre>
|
@@ -682,7 +709,7 @@ You can use the instance for only the purpose of observation.
|
|
682
709
|
|
683
710
|
|
684
711
|
<div id="initialize-storage-method" class="method-detail ">
|
685
|
-
<a name="
|
712
|
+
<a name="M000019"></a>
|
686
713
|
|
687
714
|
<div class="method-heading">
|
688
715
|
|
@@ -701,11 +728,10 @@ You can use the instance for only the purpose of observation.
|
|
701
728
|
<div class="method-source-code"
|
702
729
|
id="initialize-storage-source">
|
703
730
|
<pre>
|
704
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
731
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 310</span>
|
705
732
|
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">initialize_storage</span>
|
706
|
-
<span class="ruby-keyword kw">unless</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span> <span class="ruby-ivar">@@base_dir</span>
|
707
|
-
|
708
|
-
<span class="ruby-keyword kw">end</span>
|
733
|
+
<span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">makedirs</span> <span class="ruby-ivar">@@base_dir</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span> <span class="ruby-ivar">@@base_dir</span>
|
734
|
+
<span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">makedirs</span> <span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-value str">"/suspended"</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span> <span class="ruby-ivar">@@base_dir</span><span class="ruby-operator">+</span><span class="ruby-value str">"/suspended"</span>
|
709
735
|
<span class="ruby-keyword kw">end</span></pre>
|
710
736
|
</div>
|
711
737
|
|
@@ -716,7 +742,7 @@ You can use the instance for only the purpose of observation.
|
|
716
742
|
|
717
743
|
|
718
744
|
<div id="safe-class-name-method" class="method-detail ">
|
719
|
-
<a name="
|
745
|
+
<a name="M000020"></a>
|
720
746
|
|
721
747
|
<div class="method-heading">
|
722
748
|
|
@@ -735,7 +761,7 @@ You can use the instance for only the purpose of observation.
|
|
735
761
|
<div class="method-source-code"
|
736
762
|
id="safe-class-name-source">
|
737
763
|
<pre>
|
738
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
764
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 316</span>
|
739
765
|
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">safe_class_name</span>(<span class="ruby-identifier">c</span>)
|
740
766
|
<span class="ruby-identifier">c</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/[:]/</span>,<span class="ruby-value str">'@'</span>)
|
741
767
|
<span class="ruby-keyword kw">end</span></pre>
|
@@ -748,12 +774,12 @@ You can use the instance for only the purpose of observation.
|
|
748
774
|
|
749
775
|
|
750
776
|
<div id="safe-exit-method" class="method-detail ">
|
751
|
-
<a name="
|
777
|
+
<a name="M000022"></a>
|
752
778
|
|
753
779
|
<div class="method-heading">
|
754
780
|
|
755
781
|
<span class="method-name">safe_exit</span><span
|
756
|
-
class="method-args">()</span>
|
782
|
+
class="method-args">(process_name)</span>
|
757
783
|
<span class="method-click-advice">click to toggle source</span>
|
758
784
|
|
759
785
|
</div>
|
@@ -767,14 +793,14 @@ You can use the instance for only the purpose of observation.
|
|
767
793
|
<div class="method-source-code"
|
768
794
|
id="safe-exit-source">
|
769
795
|
<pre>
|
770
|
-
<span class="ruby-comment cmt"># File lib/rask.rb, line
|
771
|
-
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">safe_exit</span>
|
796
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 344</span>
|
797
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">safe_exit</span>(<span class="ruby-identifier">process_name</span>)
|
772
798
|
<span class="ruby-ivar">@@terminated</span> = <span class="ruby-keyword kw">true</span>
|
773
799
|
<span class="ruby-keyword kw">while</span> <span class="ruby-ivar">@@thread_count</span> <span class="ruby-operator">></span> <span class="ruby-value">0</span>
|
774
800
|
<span class="ruby-identifier">sleep</span>(<span class="ruby-value">0</span><span class="ruby-value">.1</span>)
|
775
801
|
<span class="ruby-keyword kw">end</span>
|
776
|
-
<span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">rm</span>(<span class="ruby-identifier">pid_path</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">pid_path</span>)
|
777
|
-
<span class="ruby-identifier">print</span> <span class="ruby-value str">"safely daemon terminated. \n"</span>
|
802
|
+
<span class="ruby-constant">FileUtils</span>.<span class="ruby-identifier">rm</span>(<span class="ruby-identifier">pid_path</span>(<span class="ruby-identifier">process_name</span>)) <span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">pid_path</span>(<span class="ruby-identifier">process_name</span>))
|
803
|
+
<span class="ruby-identifier">print</span> <span class="ruby-value str">"[Rask] safely daemon terminated. \n"</span>
|
778
804
|
<span class="ruby-identifier">exit</span>
|
779
805
|
<span class="ruby-keyword kw">end</span></pre>
|
780
806
|
</div>
|
@@ -785,6 +811,57 @@ You can use the instance for only the purpose of observation.
|
|
785
811
|
</div>
|
786
812
|
|
787
813
|
|
814
|
+
<div id="thread-proc-method" class="method-detail ">
|
815
|
+
<a name="M000021"></a>
|
816
|
+
|
817
|
+
<div class="method-heading">
|
818
|
+
|
819
|
+
<span class="method-name">thread_proc</span><span
|
820
|
+
class="method-args">(thread_id, worker_sleep = 0.1)</span>
|
821
|
+
<span class="method-click-advice">click to toggle source</span>
|
822
|
+
|
823
|
+
</div>
|
824
|
+
|
825
|
+
<div class="method-description">
|
826
|
+
|
827
|
+
|
828
|
+
|
829
|
+
|
830
|
+
|
831
|
+
<div class="method-source-code"
|
832
|
+
id="thread-proc-source">
|
833
|
+
<pre>
|
834
|
+
<span class="ruby-comment cmt"># File lib/rask.rb, line 321</span>
|
835
|
+
<span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">thread_proc</span>(<span class="ruby-identifier">thread_id</span>, <span class="ruby-identifier">worker_sleep</span> = <span class="ruby-value">0</span><span class="ruby-value">.1</span>)
|
836
|
+
<span class="ruby-ivar">@@thread_count</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
|
837
|
+
<span class="ruby-identifier">print</span> <span class="ruby-node">"[Rask] Thread Start ID:#{thread_id}\n"</span>
|
838
|
+
<span class="ruby-keyword kw">while</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@@terminated</span>
|
839
|
+
<span class="ruby-identifier">d</span> = <span class="ruby-keyword kw">nil</span>
|
840
|
+
<span class="ruby-ivar">@@locker</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span>
|
841
|
+
<span class="ruby-identifier">d</span> = <span class="ruby-ivar">@@queue</span>.<span class="ruby-identifier">pop</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@@queue</span>.<span class="ruby-identifier">empty?</span>
|
842
|
+
<span class="ruby-keyword kw">end</span>
|
843
|
+
<span class="ruby-keyword kw">if</span> <span class="ruby-identifier">d</span> <span class="ruby-operator">!=</span> <span class="ruby-keyword kw">nil</span>
|
844
|
+
<span class="ruby-comment cmt">#print "task #{d}\n"</span>
|
845
|
+
<span class="ruby-identifier">run</span>(<span class="ruby-identifier">d</span>)
|
846
|
+
<span class="ruby-ivar">@@locker</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span>
|
847
|
+
<span class="ruby-ivar">@@processing</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">d</span>)
|
848
|
+
<span class="ruby-keyword kw">end</span>
|
849
|
+
<span class="ruby-keyword kw">else</span>
|
850
|
+
<span class="ruby-comment cmt"># print "no data in queue\n"</span>
|
851
|
+
<span class="ruby-keyword kw">end</span>
|
852
|
+
<span class="ruby-identifier">sleep</span>(<span class="ruby-identifier">worker_sleep</span>)
|
853
|
+
<span class="ruby-keyword kw">end</span>
|
854
|
+
<span class="ruby-identifier">print</span> <span class="ruby-node">"[Rask] Thread Exit ID:#{thread_id}\n"</span>
|
855
|
+
<span class="ruby-ivar">@@thread_count</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span>
|
856
|
+
<span class="ruby-keyword kw">end</span></pre>
|
857
|
+
</div>
|
858
|
+
|
859
|
+
</div>
|
860
|
+
|
861
|
+
|
862
|
+
</div>
|
863
|
+
|
864
|
+
|
788
865
|
</div>
|
789
866
|
|
790
867
|
|
data/doc/created.rid
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Thu, 04 Mar 2010 23:07:04 +0900
|
data/doc/index.html
CHANGED
@@ -40,35 +40,39 @@
|
|
40
40
|
<h2>Methods</h2>
|
41
41
|
<ul>
|
42
42
|
|
43
|
+
<li><a href="Rask.html#M000008">::base_directory — Rask</a></li>
|
44
|
+
|
43
45
|
<li><a href="Rask.html#M000007">::base_directory= — Rask</a></li>
|
44
46
|
|
45
|
-
<li><a href="Rask.html#
|
47
|
+
<li><a href="Rask.html#M000018">::daemon — Rask</a></li>
|
46
48
|
|
47
|
-
<li><a href="Rask.html#
|
49
|
+
<li><a href="Rask.html#M000017">::destroy — Rask</a></li>
|
48
50
|
|
49
|
-
<li><a href="Rask.html#
|
51
|
+
<li><a href="Rask.html#M000019">::initialize_storage — Rask</a></li>
|
50
52
|
|
51
|
-
<li><a href="Rask.html#
|
53
|
+
<li><a href="Rask.html#M000012">::insert — Rask</a></li>
|
52
54
|
|
53
55
|
<li><a href="Rask/Task.html#M000000">::new — Rask::Task</a></li>
|
54
56
|
|
55
|
-
<li><a href="Rask.html#
|
57
|
+
<li><a href="Rask.html#M000011">::pid_path — Rask</a></li>
|
58
|
+
|
59
|
+
<li><a href="Rask.html#M000015">::read — Rask</a></li>
|
56
60
|
|
57
|
-
<li><a href="Rask.html#
|
61
|
+
<li><a href="Rask.html#M000013">::run — Rask</a></li>
|
58
62
|
|
59
|
-
<li><a href="Rask.html#
|
63
|
+
<li><a href="Rask.html#M000014">::run_all — Rask</a></li>
|
60
64
|
|
61
|
-
<li><a href="Rask.html#
|
65
|
+
<li><a href="Rask.html#M000020">::safe_class_name — Rask</a></li>
|
62
66
|
|
63
|
-
<li><a href="Rask.html#
|
67
|
+
<li><a href="Rask.html#M000022">::safe_exit — Rask</a></li>
|
64
68
|
|
65
|
-
<li><a href="Rask.html#
|
69
|
+
<li><a href="Rask.html#M000016">::task_ids — Rask</a></li>
|
66
70
|
|
67
|
-
<li><a href="Rask.html#
|
71
|
+
<li><a href="Rask.html#M000010">::task_path — Rask</a></li>
|
68
72
|
|
69
|
-
<li><a href="Rask.html#M000009">::
|
73
|
+
<li><a href="Rask.html#M000009">::thread_max_count= — Rask</a></li>
|
70
74
|
|
71
|
-
<li><a href="Rask.html#
|
75
|
+
<li><a href="Rask.html#M000021">::thread_proc — Rask</a></li>
|
72
76
|
|
73
77
|
<li><a href="Rask/Task.html#M000005">#destroy — Rask::Task</a></li>
|
74
78
|
|
data/doc/lib/rask_rb.html
CHANGED
data/lib/rask.rb
CHANGED
@@ -136,16 +136,19 @@ module Rask
|
|
136
136
|
@@queue = Queue.new
|
137
137
|
@@processing = []
|
138
138
|
@@locker = Mutex::new
|
139
|
-
@@process_name = nil
|
140
139
|
|
141
140
|
#
|
142
|
-
# === Set base storage directory
|
141
|
+
# === Set/Get base storage directory
|
143
142
|
# default :: /tmp/rask
|
144
143
|
#
|
145
144
|
def self.base_directory=(new_directory)
|
146
145
|
@@base_dir = new_directory
|
147
146
|
end
|
148
147
|
|
148
|
+
def self.base_directory
|
149
|
+
@@base_dir
|
150
|
+
end
|
151
|
+
|
149
152
|
#
|
150
153
|
# === Set max count of worker thread
|
151
154
|
# default :: 5
|
@@ -154,23 +157,15 @@ module Rask
|
|
154
157
|
@@thread_max_count = count
|
155
158
|
end
|
156
159
|
|
157
|
-
#
|
158
|
-
# === Set process name manually
|
159
|
-
# default :: $0
|
160
|
-
#
|
161
|
-
def self.process_name=(name)
|
162
|
-
@@process_name = name
|
163
|
-
end
|
164
|
-
|
165
160
|
#
|
166
161
|
def self.task_path(task_id)
|
167
162
|
@@base_dir+"/#{task_id}.task"
|
168
163
|
end
|
169
164
|
|
170
165
|
#
|
171
|
-
def self.pid_path
|
172
|
-
return @@base_dir+"/#{File.basename($0)}.pid" if
|
173
|
-
@@base_dir+"/#{
|
166
|
+
def self.pid_path(process_name = nil)
|
167
|
+
return @@base_dir+"/#{File.basename($0)}.pid" if process_name==nil
|
168
|
+
@@base_dir+"/#{process_name}.pid"
|
174
169
|
end
|
175
170
|
|
176
171
|
#
|
@@ -196,17 +191,25 @@ module Rask
|
|
196
191
|
f = File.open(task_path(task_id), 'r+') rescue return
|
197
192
|
f.flock(File::LOCK_EX)
|
198
193
|
task = Marshal.restore(f)
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
194
|
+
begin
|
195
|
+
if block_given?
|
196
|
+
yield task
|
197
|
+
else
|
198
|
+
task.run
|
199
|
+
end
|
200
|
+
f.truncate(0)
|
201
|
+
f.pos = 0
|
202
|
+
Marshal.dump(task, f)
|
203
|
+
f.flock(File::LOCK_UN)
|
204
|
+
f.close
|
205
|
+
FileUtils.rm(task_path(task_id)) if task.destroy?
|
206
|
+
rescue
|
207
|
+
p $!
|
208
|
+
print $@.join("\n") + "\n--------------------------------------------\n"
|
209
|
+
f.flock(File::LOCK_UN)
|
210
|
+
f.close
|
211
|
+
FileUtils.mv(task_path(task_id), @@base_dir+"/suspended/")
|
203
212
|
end
|
204
|
-
f.truncate(0)
|
205
|
-
f.pos = 0
|
206
|
-
Marshal.dump(task, f)
|
207
|
-
f.flock(File::LOCK_UN)
|
208
|
-
f.close
|
209
|
-
FileUtils.rm(task_path(task_id)) if task.destroy?
|
210
213
|
end
|
211
214
|
|
212
215
|
#
|
@@ -267,46 +270,26 @@ module Rask
|
|
267
270
|
# group :: Only the instance of specified group. see also Task::initialize.
|
268
271
|
# sleep :: Polling interval daemon process.
|
269
272
|
#
|
270
|
-
def self.daemon(options = {:class=>nil, :group=>nil, :sleep=>0.1})
|
271
|
-
options = { :sleep=>0.1 }.merge(options)
|
273
|
+
def self.daemon(options = {:class=>nil, :group=>nil, :sleep=>0.1, :worker_sleep=>0.1, :process_name=>nil})
|
274
|
+
options = { :sleep=>0.1, :worker_sleep=>0.1, :process_name=>nil, :threading=>true }.merge(options)
|
272
275
|
print "daemon start\n"
|
273
276
|
exit if fork
|
274
277
|
Process.setsid
|
275
278
|
|
276
279
|
initialize_storage
|
277
|
-
if File.exist? pid_path
|
280
|
+
if File.exist? pid_path(options[:process_name])
|
278
281
|
print "already running rask process. #{File.basename($0)}"
|
279
282
|
return
|
280
283
|
end
|
281
|
-
open(pid_path,"w"){|f| f.write Process.pid}
|
284
|
+
open(pid_path(options[:process_name]),"w"){|f| f.write Process.pid}
|
282
285
|
|
283
286
|
# create worker threads
|
284
287
|
threads = []
|
285
288
|
for i in 1..@@thread_max_count do
|
286
|
-
threads << Thread::new(i) { |thread_id|
|
287
|
-
@@thread_count += 1
|
288
|
-
while !@@terminated
|
289
|
-
d = nil
|
290
|
-
@@locker.synchronize do
|
291
|
-
d = @@queue.pop unless @@queue.empty?
|
292
|
-
end
|
293
|
-
if d != nil
|
294
|
-
# print "#{d}\n"
|
295
|
-
run(d)
|
296
|
-
@@locker.synchronize do
|
297
|
-
@@processing.delete(d)
|
298
|
-
end
|
299
|
-
else
|
300
|
-
# print "no data in queue\n"
|
301
|
-
sleep(options[:sleep])
|
302
|
-
end
|
303
|
-
end
|
304
|
-
print "#{thread_id}"
|
305
|
-
@@thread_count -= 1
|
306
|
-
}
|
289
|
+
threads << Thread::new(i) { |thread_id| thread_proc(thread_id, options[:worker_sleep]) }
|
307
290
|
end
|
308
291
|
|
309
|
-
Signal.trap(:TERM) {safe_exit}
|
292
|
+
Signal.trap(:TERM) {safe_exit(options[:process_name])}
|
310
293
|
|
311
294
|
while true
|
312
295
|
task_list = Rask.task_ids(options)
|
@@ -323,12 +306,10 @@ module Rask
|
|
323
306
|
end
|
324
307
|
|
325
308
|
private
|
326
|
-
|
327
309
|
#
|
328
310
|
def self.initialize_storage
|
329
|
-
unless File.exists? @@base_dir
|
330
|
-
|
331
|
-
end
|
311
|
+
FileUtils.makedirs @@base_dir unless File.exists? @@base_dir
|
312
|
+
FileUtils.makedirs @@base_dir+"/suspended" unless File.exists? @@base_dir+"/suspended"
|
332
313
|
end
|
333
314
|
|
334
315
|
#
|
@@ -337,15 +318,37 @@ private
|
|
337
318
|
end
|
338
319
|
|
339
320
|
#
|
340
|
-
def self.
|
321
|
+
def self.thread_proc(thread_id, worker_sleep = 0.1)
|
322
|
+
@@thread_count += 1
|
323
|
+
print "[Rask] Thread Start ID:#{thread_id}\n"
|
324
|
+
while !@@terminated
|
325
|
+
d = nil
|
326
|
+
@@locker.synchronize do
|
327
|
+
d = @@queue.pop unless @@queue.empty?
|
328
|
+
end
|
329
|
+
if d != nil
|
330
|
+
#print "task #{d}\n"
|
331
|
+
run(d)
|
332
|
+
@@locker.synchronize do
|
333
|
+
@@processing.delete(d)
|
334
|
+
end
|
335
|
+
else
|
336
|
+
# print "no data in queue\n"
|
337
|
+
end
|
338
|
+
sleep(worker_sleep)
|
339
|
+
end
|
340
|
+
print "[Rask] Thread Exit ID:#{thread_id}\n"
|
341
|
+
@@thread_count -= 1
|
342
|
+
end
|
343
|
+
#
|
344
|
+
def self.safe_exit(process_name)
|
341
345
|
@@terminated = true
|
342
346
|
while @@thread_count > 0
|
343
347
|
sleep(0.1)
|
344
348
|
end
|
345
|
-
FileUtils.rm(pid_path) if File.exist?(pid_path)
|
346
|
-
print "safely daemon terminated. \n"
|
349
|
+
FileUtils.rm(pid_path(process_name)) if File.exist?(pid_path(process_name))
|
350
|
+
print "[Rask] safely daemon terminated. \n"
|
347
351
|
exit
|
348
352
|
end
|
349
353
|
|
350
354
|
end
|
351
|
-
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rask
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mewlist / Hidenori Doi
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-03-
|
12
|
+
date: 2010-03-04 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|