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

Returns a new instance of 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



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ocular/event/eventbase.rb', line 80

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
36
# 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
        ::Ocular.logger.debug "exec for context #{context} took #{start}"
        context.log_timing("execution_time", start)
        @last_run = context
    end
end

#exec_fork(context) ⇒ Object



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
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ocular/event/eventbase.rb', line 38

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

        context.after_fork()

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

        begin
            Marshal.dump(r, writer)
        rescue TypeError => e
            ::Ocular.logger.error "TypeError when trying to marshal event handler return value. Reason: #{e}"
            ::Ocular.logger.error "This is usually because you forgot that the last function return value is returned from the handler function. The current type of the returned value is #{r.response.class}. Please make sure your handler returns a proper value which doesn't reference any sockets or otherwise complex unserializable objects."
            ::Ocular.logger.error "The value of the returned value is :#{r.response.pretty_inspect}"
            ::Ocular.logger.error "R value was :#{r.pretty_inspect}"
        end
        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



76
77
78
# File 'lib/ocular/event/eventbase.rb', line 76

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