Skip to content

Instantly share code, notes, and snippets.

@printercu
Created September 5, 2017 12:05
Show Gist options
  • Save printercu/a15c4cab09368d70454c6b1e59c0d7ef to your computer and use it in GitHub Desktop.
Save printercu/a15c4cab09368d70454c6b1e59c0d7ef to your computer and use it in GitHub Desktop.
Telegram::Bot poller with daemons gem
# 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'
#!/usr/bin/env ruby
# to be placed in bin/telegram_bot
begin
ENV['BOT_POLLER_MODE'] = 'true'
require_relative '../config/environment'
Telegram::Bot::UpdatesPoller.start(ENV['BOT'].try!(:to_sym) || :default)
rescue Exception => e
Rollbar.report_exception(e) if defined?(Rollbar) && !e.is_a?(SystemExit)
raise
end
#!/usr/bin/env ruby
# to be placed in bin/telegram_bot_ctl
require 'daemons'
Daemons.run File.join(__dir__, 'telegram_bot'),
monitor: true,
dir: '../tmp/pids'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment