Search code examples
ruby-on-railsrubyruby-on-rails-5mixpanel

Rails 5 app running background tasks


I've built a small rails app and have a ruby script that I would like to run in the background of my app. Currently this code is placed in my lib directory (lib/tweetstreamer.rb). What's the easiest way to accomplish this task while having the code continue to run in the background? Would I need to use some type of command inside my application controller? I found that when I use "rails runner path/to/tweetstreamer.rb" my script runs perfectly.

tweetstreamer.rb can be seen below

require 'tweetstream'
require 'twitter'
require 'mixpanel-ruby'

  TweetStream.configure do |conf|
  conf.consumer_key = "XXXXXXX"
  conf.consumer_secret = "XXXXXXX"
  conf.oauth_token = "XXXXXXX"
  conf.oauth_token_secret = "XXXXXXX"
  conf.auth_method = :oauth
  end

  @store_account = Twitter::REST::Client.new do |config|
  config.consumer_key = "XXXXXXX"
  config.consumer_secret = "XXXXXXX"
  config.access_token = "XXXXXXX"
  config.access_token_secret = "XXXXXXX"
  end

  client = TweetStream::Client.new

  client.track('mysampletweet') do |status|

  msg = status.text
  tag = "#mysampletweet"

  puts msg
  puts tag

  if msg.include? tag
  message = "@#{status.user.screen_name} Happy Holidays from @myaccountname!"
  sleep 5
  @store_account.update(message, in_reply_to_status_id: status.id)
  end

  @tracker = Mixpanel::Tracker.new("XXXXXXX")

  @tracker.track(status.id, 'Tweets Received', { 'Hashtag' => tag, 'Message' => msg })

  puts 'Your code ran'
  end

Solution

  • You could make this a rake task. Change the namespace myappname to the name of your app.

    Save this in myappname/lib/tasks/tweetstream.rake:

    namespace :myappname do
      task :tweetstream => :environment do
    
        require 'tweetstream'
        require 'twitter'
        require 'mixpanel-ruby'
    
        TweetStream.configure do |conf|
        conf.consumer_key = "XXXXXXX"
        conf.consumer_secret = "XXXXXXX"
        conf.oauth_token = "XXXXXXX"
        conf.oauth_token_secret = "XXXXXXX"
        conf.auth_method = :oauth
        end
    
        @store_account = Twitter::REST::Client.new do |config|
        config.consumer_key = "XXXXXXX"
        config.consumer_secret = "XXXXXXX"
        config.access_token = "XXXXXXX"
        config.access_token_secret = "XXXXXXX"
        end
    
        client = TweetStream::Client.new
    
        client.track('mysampletweet') do |status|
    
        msg = status.text
        tag = "#mysampletweet"
    
        puts msg
        puts tag
    
        if msg.include? tag
        message = "@#{status.user.screen_name} Happy Holidays from @myaccountname!"
        sleep 5
        @store_account.update(message, in_reply_to_status_id: status.id)
        end
    
        @tracker = Mixpanel::Tracker.new("XXXXXXX")
    
        @tracker.track(status.id, 'Tweets Received', { 'Hashtag' => tag, 'Message' => msg })
    
        puts 'Your code ran'
        end
      end
    end
    

    Then to boot in a separate process, from your command line you can run it with bundle exec rake myappname:tweetstream