Class: Rust::Jobs::Task

Inherits:
Object show all
Defined in:
lib/rust/jobs/jobs.rb

Instance Method Summary collapse

Constructor Details

#initialize(title, &block) ⇒ Task

Returns a new instance of Task.



23
24
25
26
27
28
29
30
31
# File 'lib/rust/jobs/jobs.rb', line 23

def initialize(title, &block)
    raise "Expected block to describe the task" unless block_given?
    @title = title
    @todo  = block
    @done  = false

    @complete_hook = proc {}
    @error_hook = proc {}
end

Instance Method Details

#commitObject



49
50
# File 'lib/rust/jobs/jobs.rb', line 49

def commit
end

#notifyObject



39
40
41
# File 'lib/rust/jobs/jobs.rb', line 39

def notify
    @done = true
end

#on_complete(&block) ⇒ Object



52
53
54
55
# File 'lib/rust/jobs/jobs.rb', line 52

def on_complete(&block)
    raise "Block expected" unless block_given?
    @complete_hook = block
end

#on_error(&block) ⇒ Object



57
58
59
60
# File 'lib/rust/jobs/jobs.rb', line 57

def on_error(&block)
    raise "Block expected" unless block_given?
    @error_hook = block
end

#startObject



33
34
35
36
37
# File 'lib/rust/jobs/jobs.rb', line 33

def start
    @thread = Thread.start do
        @todo.call(TaskHook.new(@complete_hook, @error_hook))
    end
end

#waitfor(granularity = 0.1) ⇒ Object



43
44
45
46
47
# File 'lib/rust/jobs/jobs.rb', line 43

def waitfor(granularity=0.1)
    while !@done
        sleep granularity
    end
end