rask 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +31 -1
- data/doc/Rask.html +670 -0
- data/doc/Rask/StateMachine.html +379 -0
- data/doc/Rask/StateMachine/ClassMethods.html +190 -0
- data/doc/Rask/Task.html +402 -0
- data/doc/created.rid +1 -0
- data/doc/images/brick.png +0 -0
- data/doc/images/brick_link.png +0 -0
- data/doc/images/bug.png +0 -0
- data/doc/images/bullet_black.png +0 -0
- data/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/images/date.png +0 -0
- data/doc/images/find.png +0 -0
- data/doc/images/loadingAnimation.gif +0 -0
- data/doc/images/macFFBgHack.png +0 -0
- data/doc/images/package.png +0 -0
- data/doc/images/page_green.png +0 -0
- data/doc/images/page_white_text.png +0 -0
- data/doc/images/page_white_width.png +0 -0
- data/doc/images/plugin.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tag_green.png +0 -0
- data/doc/images/wrench.png +0 -0
- data/doc/images/wrench_orange.png +0 -0
- data/doc/images/zoom.png +0 -0
- data/doc/index.html +103 -0
- data/doc/js/darkfish.js +116 -0
- data/doc/js/jquery.js +32 -0
- data/doc/js/quicksearch.js +114 -0
- data/doc/js/thickbox-compressed.js +10 -0
- data/doc/lib/rask/state_machine_rb.html +57 -0
- data/doc/lib/rask_rb.html +59 -0
- data/doc/rdoc.css +696 -0
- data/lib/rask.rb +88 -41
- metadata +35 -2
data/lib/rask.rb
CHANGED
@@ -7,37 +7,67 @@ require 'thread'
|
|
7
7
|
|
8
8
|
require File.dirname(__FILE__) + '/rask/state_machine'
|
9
9
|
|
10
|
-
|
10
|
+
#Authors:: mewlist / Hidenori Doi
|
11
|
+
#Copyright:: Copyright (C) 2010 mewlist / Hidenori Doi
|
12
|
+
#License:: The MIT License
|
13
|
+
#
|
14
|
+
#== Rask is terminatable task engine
|
15
|
+
#
|
11
16
|
module Rask
|
12
17
|
|
18
|
+
#
|
19
|
+
#===Task base class
|
20
|
+
#To define new Task you must inherit this base-class
|
21
|
+
#* sample code
|
22
|
+
# class NewTask < Rask::Task
|
23
|
+
# define_state :initial, :initial => true
|
24
|
+
# define_state :finish
|
25
|
+
# def initial
|
26
|
+
# transition :finish
|
27
|
+
# end
|
28
|
+
# def finish
|
29
|
+
# destroy
|
30
|
+
# end
|
31
|
+
# end
|
13
32
|
class Task
|
14
33
|
include StateMachine
|
15
34
|
attr_accessor :task_id
|
16
35
|
attr_accessor :group
|
36
|
+
attr_reader :state
|
17
37
|
|
38
|
+
#
|
39
|
+
#====If group option is given, the task is classified by group name.
|
40
|
+
#
|
41
|
+
#==== _group
|
42
|
+
# group name to classify
|
18
43
|
def initialize(_group=nil)
|
19
|
-
group = _group
|
44
|
+
self.group = _group
|
20
45
|
super()
|
21
46
|
end
|
22
47
|
|
23
|
-
|
48
|
+
#
|
49
|
+
#====automatically callbacked from task engine.
|
50
|
+
#
|
24
51
|
def run
|
25
52
|
if @state
|
26
53
|
eval @state.to_s
|
27
54
|
end
|
28
55
|
end
|
29
56
|
|
30
|
-
|
57
|
+
#
|
58
|
+
#====Transition to new state. In the state function.
|
59
|
+
# Usually you should call generated transition_to_[state name] function
|
31
60
|
def transition(to)
|
32
61
|
@state = to
|
33
62
|
end
|
34
63
|
|
35
64
|
|
65
|
+
#
|
36
66
|
def destroy
|
37
67
|
transition nil
|
38
68
|
end
|
39
69
|
|
40
|
-
|
70
|
+
#
|
41
71
|
def destroy?
|
42
72
|
@state == nil
|
43
73
|
end
|
@@ -46,42 +76,41 @@ module Rask
|
|
46
76
|
|
47
77
|
|
48
78
|
@@base_dir = '/tmp/rask'
|
49
|
-
@@threading = false
|
50
79
|
@@thread_max_count = 5
|
51
80
|
@@thread_count = 0
|
52
81
|
@@terminated = false
|
53
82
|
@@queue = Queue.new
|
54
83
|
@@processing = []
|
55
84
|
@@locker = Mutex::new
|
56
|
-
|
85
|
+
|
86
|
+
#
|
87
|
+
#====Set base storage directory
|
88
|
+
#default is '/tmp/rask'
|
89
|
+
#
|
57
90
|
def self.base_directory=(new_directory)
|
58
91
|
@@base_dir = new_directory
|
59
92
|
end
|
60
93
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
def self.disable_thread
|
66
|
-
@@threading = false
|
67
|
-
end
|
68
|
-
|
94
|
+
#
|
95
|
+
#====Set max count of worker thread
|
96
|
+
#
|
69
97
|
def self.thread_max_count=(count)
|
70
98
|
@@thread_max_count = count
|
71
99
|
end
|
72
100
|
|
101
|
+
#
|
73
102
|
def self.task_path(task_id)
|
74
103
|
@@base_dir+"/#{task_id}.task"
|
75
104
|
end
|
76
105
|
|
77
|
-
|
78
|
-
@@base_dir+"/#{task_id}.task"
|
79
|
-
end
|
80
|
-
|
106
|
+
#
|
81
107
|
def self.pid_path
|
82
108
|
@@base_dir+"/#{File.basename($0)}.pid"
|
83
109
|
end
|
84
110
|
|
111
|
+
#
|
112
|
+
#====Insert new task
|
113
|
+
# Rask::insert NewTask.new
|
85
114
|
def self.insert(task)
|
86
115
|
initialize_storage
|
87
116
|
task_id = "#{safe_class_name(task.class.name)}-#{task.group.to_s}-#{Time.now.to_i}-#{Time.now.usec}"
|
@@ -95,8 +124,9 @@ module Rask
|
|
95
124
|
end
|
96
125
|
|
97
126
|
|
98
|
-
|
99
|
-
|
127
|
+
#
|
128
|
+
def self.run(task_id)
|
129
|
+
f = File.open(task_path(task_id), 'r+') rescue return
|
100
130
|
f.flock(File::LOCK_EX)
|
101
131
|
|
102
132
|
task = Marshal.restore(f)
|
@@ -107,51 +137,67 @@ module Rask
|
|
107
137
|
Marshal.dump(task, f)
|
108
138
|
f.flock(File::LOCK_UN)
|
109
139
|
f.close
|
110
|
-
FileUtils.rm(task_path) if task.destroy?
|
140
|
+
FileUtils.rm(task_path(task_id)) if task.destroy?
|
111
141
|
end
|
112
142
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
}
|
122
|
-
threads.each { |t| t.join } if @@threading
|
143
|
+
#
|
144
|
+
def self.read(task_id)
|
145
|
+
f = File.open(task_path(task_id), 'r+') rescue return
|
146
|
+
f.flock(File::LOCK_EX)
|
147
|
+
task = Marshal.restore(f)
|
148
|
+
f.flock(File::LOCK_UN)
|
149
|
+
f.close
|
150
|
+
task
|
123
151
|
end
|
124
152
|
|
125
|
-
|
153
|
+
#
|
154
|
+
#====Get all file path of task list
|
155
|
+
#
|
156
|
+
def self.task_ids(options = { :class=>nil, :group=>nil })
|
126
157
|
target = @@base_dir
|
127
|
-
target += '/'
|
128
|
-
|
158
|
+
target += '/'
|
159
|
+
if options[:class]
|
160
|
+
target += "#{safe_class_name(options[:class])}"
|
161
|
+
else
|
162
|
+
target += "[^-]+"
|
163
|
+
end
|
129
164
|
target += "-#{options[:group]}-" if options[:group]
|
130
165
|
|
131
|
-
|
166
|
+
task_id_list = []
|
132
167
|
Dir.glob(@@base_dir+"/*.task") { |d|
|
133
168
|
if target.empty? || /#{target}/ =~ d
|
134
|
-
|
169
|
+
task_id_list.push File.basename(d, ".*")
|
135
170
|
end
|
136
171
|
}
|
137
|
-
|
172
|
+
task_id_list
|
138
173
|
end
|
139
174
|
|
175
|
+
#
|
140
176
|
def self.initialize_storage
|
141
177
|
unless File.exists? @@base_dir
|
142
178
|
FileUtils.makedirs @@base_dir
|
143
179
|
end
|
144
180
|
end
|
145
181
|
|
182
|
+
#
|
183
|
+
#====force destroy the task
|
184
|
+
#
|
146
185
|
def self.destroy(task)
|
147
186
|
FileUtils.rm(task_path(task.task_id)) if File.exists? task_path(task.task_id)
|
148
187
|
end
|
149
188
|
|
189
|
+
#
|
150
190
|
def self.safe_class_name(c)
|
151
191
|
c.gsub(/[:]/,'@')
|
152
192
|
end
|
153
|
-
|
154
|
-
|
193
|
+
|
194
|
+
#
|
195
|
+
#====Start daemon process
|
196
|
+
#It is possible to specify the the task group.
|
197
|
+
# Rask.daemon(:group=>'TaskGroup')
|
198
|
+
#
|
199
|
+
def self.daemon(options = {})
|
200
|
+
options = { :sleep=>0.1 }.merge(options)
|
155
201
|
print "daemon start\n"
|
156
202
|
exit if fork
|
157
203
|
Process.setsid
|
@@ -190,7 +236,7 @@ module Rask
|
|
190
236
|
Signal.trap(:TERM) {safe_exit}
|
191
237
|
|
192
238
|
while true
|
193
|
-
task_list = Rask.
|
239
|
+
task_list = Rask.task_ids(options)
|
194
240
|
task_list.each { |d|
|
195
241
|
@@locker.synchronize do
|
196
242
|
unless @@processing.include?(d)
|
@@ -203,6 +249,7 @@ module Rask
|
|
203
249
|
end
|
204
250
|
end
|
205
251
|
|
252
|
+
#
|
206
253
|
def self.safe_exit
|
207
254
|
print "daemon terminated"
|
208
255
|
@@terminated = true
|
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.2
|
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-02-
|
12
|
+
date: 2010-02-28 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -26,6 +26,39 @@ files:
|
|
26
26
|
- test/test_rask.rb
|
27
27
|
- lib/rask.rb
|
28
28
|
- lib/rask/state_machine.rb
|
29
|
+
- doc/rdoc.css
|
30
|
+
- doc/js/darkfish.js
|
31
|
+
- doc/js/jquery.js
|
32
|
+
- doc/js/quicksearch.js
|
33
|
+
- doc/js/thickbox-compressed.js
|
34
|
+
- doc/images/brick.png
|
35
|
+
- doc/images/brick_link.png
|
36
|
+
- doc/images/bug.png
|
37
|
+
- doc/images/bullet_black.png
|
38
|
+
- doc/images/bullet_toggle_minus.png
|
39
|
+
- doc/images/bullet_toggle_plus.png
|
40
|
+
- doc/images/date.png
|
41
|
+
- doc/images/find.png
|
42
|
+
- doc/images/loadingAnimation.gif
|
43
|
+
- doc/images/macFFBgHack.png
|
44
|
+
- doc/images/package.png
|
45
|
+
- doc/images/page_green.png
|
46
|
+
- doc/images/page_white_text.png
|
47
|
+
- doc/images/page_white_width.png
|
48
|
+
- doc/images/plugin.png
|
49
|
+
- doc/images/ruby.png
|
50
|
+
- doc/images/tag_green.png
|
51
|
+
- doc/images/wrench.png
|
52
|
+
- doc/images/wrench_orange.png
|
53
|
+
- doc/images/zoom.png
|
54
|
+
- doc/index.html
|
55
|
+
- doc/Rask.html
|
56
|
+
- doc/Rask/StateMachine.html
|
57
|
+
- doc/Rask/StateMachine/ClassMethods.html
|
58
|
+
- doc/Rask/Task.html
|
59
|
+
- doc/lib/rask_rb.html
|
60
|
+
- doc/lib/rask/state_machine_rb.html
|
61
|
+
- doc/created.rid
|
29
62
|
- README.rdoc
|
30
63
|
- History.txt
|
31
64
|
has_rdoc: true
|