Module: Monus::Engine::Pure

Extended by:
Pure
Included in:
Pure
Defined in:
lib/monus/engines/pure.rb

Instance Method Summary collapse

Instance Method Details

#add_error_handler(handler) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/monus/engines/pure.rb', line 18

def add_error_handler(handler)
  initialize_defaults
  @on_error.push case handler
  when Proc
    handler
  when :crash
    -> * { exit 1 }
  when :ignore
    -> * {}
  when :break
    -> error, * { raise error }
  when :log, nil
    -> error, type, * { Monus.logger.fatal "error in Monus #{type} - #{error.class}: #{error.message}\n#{error.backtrace * "\n"}" }
  else
    raise Monus::ConfigurationError, 'unknown error handler given in `pure_engine_options/on_async_error` configuration; it should be a block (exception, type and binding arguments will passed) or one of `:crash`, `:ignore`, `:break` or `:log` (default) values, or array of them'
  end
end

#every(interval, fire_immediately: true, on_error: nil, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/monus/engines/pure.rb', line 67

def every(interval, fire_immediately: true, on_error: nil, &block)
  prepare unless @prepared

  Thread.new do
    sleep interval unless fire_immediately

    loop do
      begin
        block.call
      rescue => error
        interval_binding = binding
        Array(on_error || @on_error).each do |handler|
          handler.call error, :interval, interval_binding
        end
      end
      sleep interval
    end
  end
end

#initialize_defaultsObject



2
3
4
# File 'lib/monus/engines/pure.rb', line 2

def initialize_defaults
  @on_error ||= []
end

#load_httpObject



51
52
53
54
55
56
57
# File 'lib/monus/engines/pure.rb', line 51

def load_http
  @http_loaded ||= begin
    require 'net/http'
    require 'uri'
    true
  end
end

#make_http_connection(host, port) ⇒ Object



36
37
38
39
# File 'lib/monus/engines/pure.rb', line 36

def make_http_connection(host, port)
  load_http
  Net::HTTP.start host, port
end

#prepareObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/monus/engines/pure.rb', line 6

def prepare
  initialize_defaults
  option = Monus.options.dig(:pure_engine_options, :on_async_error)
  case option
  when Array
    option.each &method(:add_error_handler)
  else
    add_error_handler option
  end
  @prepared = true
end

#send_http_request(uri, method: :get, body: nil, connection: Net::HTTP) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/monus/engines/pure.rb', line 41

def send_http_request(uri, method: :get, body: nil, connection: Net::HTTP)
  load_http
  uri = URI.parse uri if connection == Net::HTTP and not uri.kind_of?(URI)
  case method
  when :get then connection.get uri
  when :post then connection.post uri, body
  else raise NotImplementedError, "unknown HTTP method #{method.inspect}"
  end
end

#send_udp_datagram(message, host, port) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/monus/engines/pure.rb', line 59

def send_udp_datagram(message, host, port)
  @udp_socket ||= begin
    require 'socket'
    UDPSocket.new
  end
  @udp_socket.send message, 0, host, port
end