Skip to content

Commit

Permalink
Added DelayedJob for background jobs.
Browse files Browse the repository at this point in the history
  • Loading branch information
pingu committed Mar 6, 2012
1 parent 04f48a3 commit c79ce73
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 249 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ gem 'bbcodeizer', '~> 0.2.0'
gem 'meta_search', '~> 1.1.1'
gem 'aws-s3', '~> 0.6.2'
gem 'thumbs_up', '~> 0.4.1'
gem 'delayed_job_active_record', '~> 0.3.2'

# TODO: Find alternative
gem 'prototype_legacy_helper', '0.0.0', :git => 'git://github.com/rails/prototype_legacy_helper.git'

# Omniauth OpenID for Steam Integration
gem "omniauth-openid", "~> 1.0.1"

gem "httparty", "~> 0.8.1"
gem "httparty", "~> 0.8.1"
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ GEM
execjs
coffee-script-source (1.1.2)
configuration (1.3.1)
delayed_job (3.0.1)
activesupport (~> 3.0)
delayed_job_active_record (0.3.2)
activerecord (> 2.1.0)
delayed_job (~> 3.0.0)
erubis (2.7.0)
execjs (1.2.4)
multi_json (~> 1.0)
Expand Down Expand Up @@ -179,6 +184,7 @@ DEPENDENCIES
aws-s3 (~> 0.6.2)
bbcodeizer (~> 0.2.0)
coffee-rails (~> 3.1.0)
delayed_job_active_record (~> 0.3.2)
faker (~> 0.9.5)
heroku
httparty (~> 0.8.1)
Expand Down
9 changes: 6 additions & 3 deletions app/models/stream.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
class Stream < ActiveRecord::Base

attr_accessible :title, :provider, :identifier, :viewers, :live, :description, :status
belongs_to :user
validates_presence_of :title, :provider, :identifier

PROVIDERS = { "TwitchTV" => "justintv", "own3D.tv" => "own3dtv" }

validates_inclusion_of :provider, :in => PROVIDERS.values

def update_status
def perform
current_viewers = nil
current_status = nil
is_live = false

if self.provider == "justintv"
response = HTTParty.get("http://api.justin.tv/api/stream/list.xml?channel=#{identifier}")

begin
current_viewers = response["streams"]["stream"]["channel_count"]
current_status = response["streams"]["stream"]["channel"]["status"]
Expand All @@ -24,6 +26,7 @@ def update_status
end
elsif self.provider == "own3dtv"
response = HTTParty.get("http://api.own3d.tv/liveCheck.php?live_id=#{identifier}")

begin
current_viewers = response["own3dReply"]["liveEvent"]["liveViewers"]
is_live = response["own3dReply"]["liveEvent"]["isLive"]
Expand Down
22 changes: 22 additions & 0 deletions db/migrate/20120306201736_create_delayed_jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class CreateDelayedJobs < ActiveRecord::Migration
def self.up
create_table :delayed_jobs, :force => true do |table|
table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
table.text :handler # YAML-encoded string of the object that will do work
table.text :last_error # reason for last failure (See Note below)
table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future.
table.datetime :locked_at # Set when a client is working on this object
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
table.string :locked_by # Who is working on this object (if locked)
table.string :queue # The name of the queue this job is in
table.timestamps
end

add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
end

def self.down
drop_table :delayed_jobs
end
end
Loading

0 comments on commit c79ce73

Please sign in to comment.