Permalink
Browse files

Added DelayedJob for background jobs.

  • Loading branch information...
1 parent 04f48a3 commit c79ce73c0e23a2e269d7bef34d75c1608298bf16 @kixelated committed Mar 6, 2012
Showing with 299 additions and 249 deletions.
  1. +2 −1 Gemfile
  2. +6 −0 Gemfile.lock
  3. +6 −3 app/models/stream.rb
  4. +22 −0 db/migrate/20120306201736_create_delayed_jobs.rb
  5. +256 −240 db/schema.rb
  6. +2 −5 lib/tasks/stream.rake
  7. +5 −0 script/delayed_job
View
@@ -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"
View
@@ -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)
@@ -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)
View
@@ -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"]
@@ -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"]
@@ -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
Oops, something went wrong.

0 comments on commit c79ce73

Please sign in to comment.