Skip to content

Deploy using `daemons` gem

printercu edited this page Nov 18, 2017 · 1 revision

bin/telegram_bot:

#!/usr/bin/env ruby

begin
  ENV['BOT_POLLER_MODE'] = 'true'
  require_relative '../config/environment'
  Telegram::Bot::UpdatesPoller.start(:default)
rescue Exception => e
  Rollbar.report_exception(e) if defined?(Rollbar) && !e.is_a?(SystemExit)
  raise
end

bin/telegram_bot_ctl:

#!/usr/bin/env ruby

require 'daemons'
Daemons.run File.join(__dir__, 'telegram_bot'),
  monitor: true,
  dir: '../tmp/pids'

rails_template/lib/capistrano/tasks/telegram_bot.rake:

namespace :telegram do
  namespace :bot do
    namespace :poller do
      {
        start:         'start an instance of the application',
        stop:          'stop all instances of the application',
        restart:       'stop all instances and restart them afterwards',
        reload:        'send a SIGHUP to all instances of the application',
        # run:           'start the application and stay on top',
        zap:           'set the application to a stopped state',
        status:        'show status (PID) of application instances',
      }.each do |action, description|
        desc description
        task action do
          on roles(:app) do
            within current_path do
              with rails_env: fetch(:rails_env) do
                execute :bundle, :exec, 'bin/telegram_bot_ctl', action
              end
            end
          end
        end
      end
    end
  end
end

after 'deploy:finished', 'telegram:bot:poller:restart'

For multiple pollers run either multiple processes or each poller in separate thread.

Clone this wiki locally