terminate children call controller

17 views
Skip to first unread message

Andrius Kairiukstis

unread,
Feb 4, 2017, 4:32:24 PM2/4/17
to Adhearsion
It is possible to terminate execution of SomeController somehow? (I have adhearsion with micro-api that listens for external requests)
I've experimented already with form of join/unjoin and  on_unjoined callback

class Loop < Adhearsion::CallController
  def run
    while call.active?
      cmd = request_next_api_command
      if cmd
        invoke some-controller-from-cmd
      end
    end
  end
end

class SomeController < Adhearsion::CallController
  def run
    # long-time task... 
  end
end

Ben Klang

unread,
Feb 5, 2017, 1:30:29 PM2/5/17
to adhea...@googlegroups.com
Hi Andrius,

Depending on what your long running task is doing, it may or may not be possible to terminate execution. Can you show the entire controller? Or perhaps describe what you’re trying to do/what you’re trying to terminate?

/BAK/

-- 
Ben Klang
Founder, Mojo Lingo
bkl...@mojolingo.com
+1.404.475.4841

Mojo Lingo -- Voice applications that work like magic
http://mojolingo.com
Twitter: @MojoLingo



--
You received this message because you are subscribed to the Google Groups "Adhearsion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adhearsion+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Andrius Kairiukstis

unread,
Feb 5, 2017, 1:50:34 PM2/5/17
to Adhearsion
I have tried execute_controller, but get error about missing bg_exec method (it were there!)

So:
Now I building fully managed through API app – ant that including some in-call processing. API might request to "cancel" any active command (invoked Controller) and execute next one.

Below is POC controller that works. I just unjoin calls or originate them or split dial - and sending both of them there to controller Loop with following command 'join_command'

Everything works, bot for example pin command blocking one call leg and I can't interrupt it anytime, have to wait

Screenshot:

Controller:
class Loop < Adhearsion::CallController
  attr_accessor :audio, :request, :valid_pin
  
  LOOP_TIMEOUT = 1
  
  def run
    # just set metadata and @variables
    set_call_data

    # instant loop
    while call.active?
      
      # API could populate command to the CallRegistry, if it there, process it!
      request = CallRegistry.request.pop call.id
      if request
        # update metadata based on API request (say it's Redis store or like that)
        update_call_data

        command = request.delete(:command)

        # stop any audio if playing - prior executing received command
        play_stop!
        execute_command command, request

      else
        sleep LOOP_TIMEOUT
        logger.info "#{self.class}... in loop"
      end
    end
  end

  # execute any dynamic command through eval
  def execute_command(command, request)
    begin
      @script_name = command.to_s.gsub(/[^0-9a-z]/i, '').downcase
      @request = request
      eval "#{@script_name}_command"
    rescue NameError => e
      logger.info "#{self.class} unknown command #{@script_name}_command (#{command})"
    rescue => e
      logger.error "#{self.class} error #{e} #{e.backtrace}"
    end
  end

  def safe(&blk)
    begin
      yield blk

    rescue Adhearsion::Call::Hangup
      # nothing to do
    rescue => e
      logger.error "#{self.class}.#{@script_name}_command error: #{e}: #{e.backtrace}"
    end
  end

  def play_start!(audio_file)
    @audio = play! audio_file rescue nil
  end

  def play_stop!
    @audio.stop! rescue nil
  end

  #################################################################################################################
  def join_command
    safe do
      call.join request.target_call_id
    end
  end

  #################################################################################################################
  def unjoin_command
    safe do
      if request.target_call_id
        call.unjoin request.target_call_id
      else
        call.unjoin
      end
    end
  end

  #################################################################################################################
  def musiconhold_command
   play_start! t(:moh, locale: 'en')
  end

  #################################################################################################################
  def pinlogin_command
    safe do
      call.unjoin call_data.agent_call_id
      CallRegistry.request.push call_data.agent_call_id, command: 'musiconhold'
      request_pin
      CallRegistry.request.push call.id, command: 'join',
                                         target_call_id: call_data.agent_call_id
    end
  end

  def request_pin
    call_data.language = get_variable('language') || 'en'

    prompt = [t(:login_prompt, locale: call_data.language)]

    1.upto(PIN_ATTEMPTS) do |attempt_no|
      pin = ask_pin(prompt)
      # ...
    end
end

Ben Klang

unread,
Feb 5, 2017, 4:52:01 PM2/5/17
to adhea...@googlegroups.com
Have you looked at Matrioska? https://github.com/adhearsion/matrioska It sounds like you could build using that. 
Reply all
Reply to author
Forward
0 new messages