Skip to content

Commit 2274ca4

Browse files
add before_started, after_completed and after_failed functionality
1 parent 9003c7c commit 2274ca4

File tree

5 files changed

+116
-74
lines changed

5 files changed

+116
-74
lines changed

lib/taskinator/persistence.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ def cleanup(expire_in=EXPIRE_IN)
222222

223223
end
224224
end
225-
226225
end
227226

228227
class RedisSerializationVisitor < Taskinator::Visitor::Base
@@ -750,7 +749,6 @@ def __getobj__
750749
# and memoize for subsequent calls
751750
@instance ||= @type.fetch(@uuid, @instance_cache)
752751
end
753-
754752
end
755753

756754
class << self

lib/taskinator/task.rb

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ def define_job_task(process, job, args, options={})
1515
Job.new(process, job, args, options)
1616
end
1717

18-
def define_hook_task(process, method, args, options={})
19-
Hook.new(process, method, args, options)
20-
end
21-
2218
def define_sub_process_task(process, sub_process, options={})
2319
SubProcess.new(process, sub_process, options)
2420
end
21+
22+
def define_hook_task(process, method, args, options={})
23+
Hook.new(process, method, args, options)
24+
end
2525
end
2626

2727
attr_reader :process
@@ -214,65 +214,6 @@ def inspect
214214

215215
#--------------------------------------------------
216216

217-
# a task which invokes the specified method on the definition
218-
# the task is executed independently of the process, so there isn't any further
219-
# processing once it completes (or fails)
220-
# the args must be intrinsic types, since they are serialized to YAML
221-
class Hook < Task
222-
attr_reader :method
223-
attr_reader :args
224-
225-
def initialize(process, method, args, options={})
226-
super(process, options)
227-
228-
raise ArgumentError, 'method' if method.nil?
229-
raise NoMethodError, method unless executor.respond_to?(method)
230-
231-
@method = method
232-
@args = args
233-
end
234-
235-
def enqueue
236-
Taskinator.queue.enqueue_task(self)
237-
end
238-
239-
def start
240-
executor.send(method, *args)
241-
# ASSUMPTION: when the method returns, the task is considered to be complete
242-
complete!
243-
244-
rescue => e
245-
Taskinator.logger.error(e)
246-
Taskinator.logger.debug(e.backtrace)
247-
fail!(e)
248-
raise e
249-
end
250-
251-
def accept(visitor)
252-
super
253-
visitor.visit_attribute(:method)
254-
visitor.visit_args(:args)
255-
end
256-
257-
def executor
258-
@executor ||= Taskinator::Executor.new(definition, self)
259-
end
260-
261-
def incr_count?
262-
false
263-
end
264-
265-
def notify_process?
266-
false
267-
end
268-
269-
def inspect
270-
%(#<#{self.class.name}:0x#{self.__id__.to_s(16)} uuid="#{uuid}", definition=:#{definition}, method=:#{method}, args=#{args}, current_state=:#{current_state}>)
271-
end
272-
end
273-
274-
#--------------------------------------------------
275-
276217
# a task which invokes the specified background job
277218
# the args must be intrinsic types, since they are serialized to YAML
278219
class Job < Task
@@ -370,5 +311,64 @@ def inspect
370311
%(#<#{self.class.name}:0x#{self.__id__.to_s(16)} uuid="#{uuid}", definition=:#{definition}, sub_process=#{sub_process.inspect}, current_state=:#{current_state}>)
371312
end
372313
end
314+
315+
#--------------------------------------------------
316+
317+
# a task which invokes the specified method on the definition
318+
# the task is executed independently of the process, so there isn't any further
319+
# processing once it completes (or fails)
320+
# the args must be intrinsic types, since they are serialized to YAML
321+
class Hook < Task
322+
attr_reader :method
323+
attr_reader :args
324+
325+
def initialize(process, method, args, options={})
326+
super(process, options)
327+
328+
raise ArgumentError, 'method' if method.nil?
329+
raise NoMethodError, method unless executor.respond_to?(method)
330+
331+
@method = method
332+
@args = args
333+
end
334+
335+
def enqueue
336+
Taskinator.queue.enqueue_task(self)
337+
end
338+
339+
def start
340+
executor.send(method, *args)
341+
# ASSUMPTION: when the method returns, the task is considered to be complete
342+
complete!
343+
344+
rescue => e
345+
Taskinator.logger.error(e)
346+
Taskinator.logger.debug(e.backtrace)
347+
fail!(e)
348+
raise e
349+
end
350+
351+
def accept(visitor)
352+
super
353+
visitor.visit_attribute(:method)
354+
visitor.visit_args(:args)
355+
end
356+
357+
def executor
358+
@executor ||= Taskinator::Executor.new(definition, self)
359+
end
360+
361+
def incr_count?
362+
false
363+
end
364+
365+
def notify_process?
366+
false
367+
end
368+
369+
def inspect
370+
%(#<#{self.class.name}:0x#{self.__id__.to_s(16)} uuid="#{uuid}", definition=:#{definition}, method=:#{method}, args=#{args}, current_state=:#{current_state}>)
371+
end
372+
end
373373
end
374374
end

spec/examples/visitor_examples.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'spec_helper'
2+
3+
shared_examples_for "a visitor" do |visitor|
4+
5+
visitor_methods = visitor.instance_methods
6+
7+
# visit_process(attribute)
8+
it { expect(visitor_methods.include?(:visit_process)).to be }
9+
10+
# visit_tasks(tasks)
11+
it { expect(visitor_methods.include?(:visit_tasks)).to be }
12+
13+
# visit_before_started_tasks(tasks)
14+
it { expect(visitor_methods.include?(:visit_before_started_tasks)).to be }
15+
16+
# visit_after_completed_tasks(tasks)
17+
it { expect(visitor_methods.include?(:visit_after_completed_tasks)).to be }
18+
19+
# visit_after_failed_tasks(tasks)
20+
it { expect(visitor_methods.include?(:visit_after_failed_tasks)).to be }
21+
22+
# visit_attribute(attribute)
23+
it { expect(visitor_methods.include?(:visit_attribute)).to be }
24+
25+
# visit_attribute_time(attribute)
26+
it { expect(visitor_methods.include?(:visit_attribute_time)).to be }
27+
28+
# visit_attribute_enum(attribute, type)
29+
it { expect(visitor_methods.include?(:visit_attribute_enum)).to be }
30+
31+
# visit_process_reference(attribute)
32+
it { expect(visitor_methods.include?(:visit_process_reference)).to be }
33+
34+
# visit_task_reference(attribute)
35+
it { expect(visitor_methods.include?(:visit_task_reference)).to be }
36+
37+
# visit_type(attribute)
38+
it { expect(visitor_methods.include?(:visit_type)).to be }
39+
40+
# visit_args(attribute)
41+
it { expect(visitor_methods.include?(:visit_args)).to be }
42+
43+
# task_count
44+
it { expect(visitor_methods.include?(:task_count)).to be }
45+
46+
end

spec/taskinator/persistence_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,4 +554,5 @@ def initialize
554554

555555
end
556556
end
557+
557558
end

spec/taskinator/visitor_spec.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
require 'spec_helper'
22

3-
describe Taskinator::Visitor::Base do
3+
describe "Visitors" do
44

5-
it { respond_to(:visit_process) }
6-
it { respond_to(:visit_tasks) }
7-
it { respond_to(:visit_attribute) }
8-
it { respond_to(:visit_process_reference) }
9-
it { respond_to(:visit_task_reference) }
10-
it { respond_to(:visit_type) }
11-
it { respond_to(:visit_args) }
12-
it { respond_to(:task_count) }
5+
it_should_behave_like "a visitor", Taskinator::Visitor::Base
6+
it_should_behave_like "a visitor", Taskinator::Persistence::RedisSerializationVisitor
7+
it_should_behave_like "a visitor", Taskinator::Persistence::XmlSerializationVisitor
8+
it_should_behave_like "a visitor", Taskinator::Persistence::RedisDeserializationVisitor
9+
it_should_behave_like "a visitor", Taskinator::Persistence::RedisCleanupVisitor
1310

1411
end

0 commit comments

Comments
 (0)