rask 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
- def self.enable_thread
62
- @@threading = true
63
- end
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
- def self.task_path(task_id)
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
- def self.run(task_path)
99
- f = File.open(task_path, 'r+') rescue return
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
- def self.each(options = { :class=>nil, :group=>nil }, &blk)
114
- threads = []
115
- tasks(options).each { |d|
116
- if @@threading
117
- threads << Thread::new(d) { |task_path| run(task_path, &blk) }
118
- else
119
- run(d, &blk)
120
- end
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
- def self.tasks(options = { :class=>nil, :group=>nil })
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 += '/' if options[:class] || options[:group]
128
- target += "#{safe_class_name(options[:class])}" if options[:class]
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
- task_list = []
166
+ task_id_list = []
132
167
  Dir.glob(@@base_dir+"/*.task") { |d|
133
168
  if target.empty? || /#{target}/ =~ d
134
- task_list.push d
169
+ task_id_list.push File.basename(d, ".*")
135
170
  end
136
171
  }
137
- task_list
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
- def self.daemon(options = { :class=>nil, :group=>nil, :sleep=>0.1 })
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.tasks(options)
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.1
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-26 00:00:00 +09:00
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