Class: Ocular::DSL::EventBase

Inherits:
Object
  • Object
show all
Defined in:
lib/ocular/event/eventbase.rb

Defined Under Namespace

Classes: Results

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy, &block) ⇒ EventBase



17
18
19
20
# File 'lib/ocular/event/eventbase.rb', line 17

def initialize(proxy, &block)
    @callback = block
    @proxy = proxy
end

Instance Attribute Details

#last_runObject

Returns the value of attribute last_run.



15
16
17
# File 'lib/ocular/event/eventbase.rb', line 15

def last_run
  @last_run
end

#proxyObject

Returns the value of attribute proxy.



15
16
17
# File 'lib/ocular/event/eventbase.rb', line 15

def proxy
  @proxy
end

Instance Method Details

#__call(context, callback) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ocular/event/eventbase.rb', line 69

def __call(context, callback)
    # we use this trickery to workaround the LocalJumpError so that we can use return
    context.define_singleton_method(:_, &callback)
    p = context.method(:_).to_proc

    reply = p.call()
    if context.respond_to?(:exec_wrapper)
        return context.exec_wrapper(reply)
    else
        return reply
    end
end

#exec(context, do_fork = self.proxy.do_fork) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ocular/event/eventbase.rb', line 22

def exec(context, do_fork = self.proxy.do_fork)
    start = Time.now
    begin
        context.proxy = self.proxy
        if do_fork
            return exec_fork(context)
        else
            return exec_nofork(context)
        end
    ensure
        context.log_timing("execution_time", start)
        @last_run = context
    end
end

#exec_fork(context) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ocular/event/eventbase.rb', line 37

def exec_fork(context)
    reader, writer = IO::pipe
    child_pid = fork do
        reader.close
        r = Results.new

        begin
            r.response = __call(context, @callback)
        rescue Exception => error
            r.error = error
        end

        Marshal.dump(r, writer)
        writer.close
    end
    writer.close

    data = reader.read
    r = Marshal.load(data)
    reader.close
    Process.wait(child_pid)

    if r.error
        raise r.error
    end
    return r.response
end

#exec_nofork(context) ⇒ Object



65
66
67
# File 'lib/ocular/event/eventbase.rb', line 65

def exec_nofork(context)
    return __call(context, @callback)
end