terminate children call controller

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

terminate children call controller

Andrius Kairiukstis-2
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

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: terminate children call controller

Ben Klang-2
Il giorno 04 feb 2017, alle ore 16:32, Andrius Kairiukstis <[hidden email]> ha scritto:


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



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
[hidden email]
+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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

signature.asc (858 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: terminate children call controller

Andrius Kairiukstis-2
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




On Sunday, 5 February 2017 19:30:29 UTC+1, Ben Klang wrote:
Il giorno 04 feb 2017, alle ore 16:32, Andrius Kairiukstis <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="fKX81sB_CAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">and...@...> ha scritto:


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



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
<a href="javascript:" target="_blank" gdf-obfuscated-mailto="fKX81sB_CAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">bkl...@...
+1.404.475.4841

Mojo Lingo -- Voice applications that work like magic
<a href="http://mojolingo.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmojolingo.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFAEbyCarHA3eZ3l-4MZTzTsvka5A&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmojolingo.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFAEbyCarHA3eZ3l-4MZTzTsvka5A&#39;;return true;">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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="fKX81sB_CAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">adhearsion+...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: terminate children call controller

Ben Klang-2
Have you looked at Matrioska? https://github.com/adhearsion/matrioska It sounds like you could build using that. 

Il giorno 05 feb 2017, alle ore 13:50, Andrius Kairiukstis <[hidden email]> ha scritto:

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




On Sunday, 5 February 2017 19:30:29 UTC+1, Ben Klang wrote:
Il giorno 04 feb 2017, alle ore 16:32, Andrius Kairiukstis <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="fKX81sB_CAAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">and...@...> ha scritto:


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



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
<a href="javascript:" target="_blank" gdf-obfuscated-mailto="fKX81sB_CAAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">bkl...@...
+1.404.475.4841

Mojo Lingo -- Voice applications that work like magic
<a href="http://mojolingo.com" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fmojolingo.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFAEbyCarHA3eZ3l-4MZTzTsvka5A';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fmojolingo.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFAEbyCarHA3eZ3l-4MZTzTsvka5A';return true;">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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="fKX81sB_CAAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">adhearsion+...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.