Class: Flor::Caller

Inherits:
Object
  • Object
show all
Defined in:
lib/flor/unit/caller.rb,
lib/flor/unit/caller_jruby.rb

Overview

The caller calls Ruby or other scripts.

Defined Under Namespace

Modules: CmdParser Classes: ProcessStatus, SpawnError, SpawnNonZeroExitError, TimeoutError, WrappedSpawnError

Instance Method Summary collapse

Constructor Details

#initialize(unit) ⇒ Caller

NB: tasker configuration entries start with “cal_”



11
12
13
14
# File 'lib/flor/unit/caller.rb', line 11

def initialize(unit)

  @unit = unit
end

Instance Method Details

#call(service, conf, message) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/flor/unit/caller.rb', line 19

def call(service, conf, message)

  return ruby_call(service, conf, message) \
    if conf['class'] || conf['module']
  return cmd_call(service, conf, message) \
    if conf['cmd']

  fail ArgumentError.new("don't know how to call item at #{conf['_path']}")

rescue => err

  [ Flor.to_error_message(message, err) ]
end

#shutdownObject



16
17
# File 'lib/flor/unit/caller.rb', line 16

def shutdown
end

#spawn(conf, data) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/flor/unit/caller.rb', line 198

def spawn(conf, data)

  t0 = Time.now

  cmd = conf['cmd']

  to = Fugit.parse(conf['timeout'] || '14s')
  to = to.is_a?(Fugit::Duration) ? to.to_sec : 14
  to = 0 if to < 0 # no timeout

  i, o = IO.pipe # _ / stdout
  f, e = IO.pipe # _ / stderr
  r, w = IO.pipe # stdin / _

  pid = Kernel.spawn(cmd, in: r, out: o, err: e)
  w.write(data)
  w.close
  o.close
  e.close

  _, status = timeout(to) { Process.wait2(pid) }

  fail SpawnNonZeroExitError.new(conf, { to: to, t0: t0 }, status, i, f) \
    if status.exitstatus != 0

  [ i.read, status ]

rescue => err

  Process.detach(pid) \
    if pid
  (Process.kill(9, pid) rescue nil) \
    unless Flor.no?(conf['on_error_kill'])

  raise err if err.is_a?(SpawnError)
  raise WrappedSpawnError.new(conf, { to: to, t0: t0, pid: pid }, err)

ensure

  [ i, o, f, e, r, w ].each { |x| x.close rescue nil }
end

#split_cmd(cmd) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/flor/unit/caller_jruby.rb', line 7

def split_cmd(cmd)

  #cmd.split(/ +/)
    # too naive

#Raabro.pp(Flor::Caller::CmdParser.parse(cmd, debug: 3), colours: true)
  Flor::Caller::CmdParser.parse(cmd)
end