Skip to content
This repository
  • 3 commits
  • 9 files changed
  • 0 comments
  • 3 contributors
19  app/controllers/admin/jobs_controller.rb
... ...
@@ -0,0 +1,19 @@
  1
+class Admin::JobsController < AdminController
  2
+  layout false
  3
+  def do
  4
+    begin
  5
+      jobtype = params[:job_type].camelcase.constantize
  6
+      id = params[:id]
  7
+    rescue Exception => e
  8
+      render :text => "Did not understand parameters", :status => 500 and return
  9
+    end
  10
+    
  11
+    begin
  12
+      jobtype.process(id)
  13
+      render :text => "Job successful", :status => 200 and return
  14
+    rescue Exception => e
  15
+      render :text => "Job threw an error", :status => 500 and return
  16
+    end
  17
+    
  18
+  end
  19
+end
5  app/models/event.rb
@@ -82,5 +82,10 @@ def self.create_from_zvent_event(zvent, user)
82 82
       :url => "http://www.zvents.com/"+zvent.zurl)
83 83
     end
84 84
   end
  85
+  
  86
+  def self.process(param)
  87
+   
  88
+  end
  89
+  
85 90
 end
86 91
 
19  config/initializers/load_queue.rb
... ...
@@ -0,0 +1,19 @@
  1
+queue = APP_CONFIG['jobqueue'] || 'noqueue'
  2
+
  3
+if queue and File.exists?("#{RAILS_ROOT}/lib/jobs/#{queue.downcase}.rb")
  4
+	require "#{RAILS_ROOT}/lib/jobs/#{queue.downcase}"
  5
+
  6
+	jobqueue = N2::JobQueues.const_get(queue.camelize).const_get('Job')
  7
+
  8
+	Rails.class_eval do
  9
+	  @@jobqueue = nil
  10
+	  #cattr_reader :jobqueue
  11
+	  class << self; attr_accessor :jobqueue; end
  12
+
  13
+	  #def self.jobqueue=(jobqueue)
  14
+	    #@@jobqueue = jobqueue
  15
+	  #end
  16
+	end
  17
+
  18
+	Rails.jobqueue = jobqueue
  19
+end
1  config/routes.rb
@@ -71,6 +71,7 @@
71 71
   map.root :controller => "home", :action => "index"
72 72
   map.admin 'admin', :controller => :admin, :action => :index
73 73
   map.namespace(:admin) do |admin|
  74
+    admin.do_job '/jobs/do/:job_type/:id', :controller => 'jobs', :action => 'do'
74 75
     admin.block '/block.:format', :controller => 'misc', :action => 'block'
75 76
     admin.flag_item '/flag_item.:format', :controller => 'misc', :action => 'flag'
76 77
     admin.feature '/feature.:format', :controller => 'misc', :action => 'feature'
26  lib/jobs/noqueue.rb
... ...
@@ -0,0 +1,26 @@
  1
+require 'lib/jobs/workers/noqueue'
  2
+
  3
+module N2
  4
+	module JobQueues
  5
+
  6
+		module Noqueue
  7
+
  8
+			class Job
  9
+
  10
+				def self.enqueue klass, params
  11
+					klass.send(:perform, params)
  12
+				end
  13
+
  14
+				def self.dequeue klass, params
  15
+				end
  16
+
  17
+				def self.workers
  18
+				  N2::JobQueues::Noqueue::Workers
  19
+				end
  20
+
  21
+			end
  22
+
  23
+		end
  24
+
  25
+	end
  26
+end
21  lib/jobs/resque.rb
... ...
@@ -0,0 +1,21 @@
  1
+require 'resque'
  2
+
  3
+module N2
  4
+	module JobQueues
  5
+
  6
+		module Resque
  7
+			class Job
  8
+				
  9
+				def self.enqueue klass, params
  10
+					Resque.enqueue klass, params
  11
+				end
  12
+
  13
+				def self.dequeue klass, params
  14
+					Resque.dequeue klass, params
  15
+				end
  16
+
  17
+			end
  18
+		end
  19
+
  20
+	end
  21
+end
30  lib/jobs/workers/noqueue.rb
... ...
@@ -0,0 +1,30 @@
  1
+module N2
  2
+	module JobQueues
  3
+
  4
+		module Noqueue
  5
+			module Workers
  6
+
  7
+				module RakeJob
  8
+					@queue = :rake_tasks
  9
+
  10
+					def self.perform(command)
  11
+						# TODO:: make this work with params
  12
+						Rake::Task[command].invoke
  13
+					end
  14
+				end
  15
+
  16
+				module ImageJob
  17
+					@queue = :image_processing
  18
+
  19
+					def self.perform(url, pid = nil, socket = nil)
  20
+						# bump unicorn worker count if pid
  21
+						# curl url to socket if socket
  22
+						# drop unicorn worker count if pid
  23
+					end
  24
+				end
  25
+
  26
+			end
  27
+		end
  28
+
  29
+	end
  30
+end
34  lib/jobs/workers/resque.rb
... ...
@@ -0,0 +1,34 @@
  1
+require 'open3'
  2
+
  3
+module N2
  4
+	module JobQueues
  5
+
  6
+		module Resque
  7
+			module Workers
  8
+
  9
+				module RakeJob
  10
+					@queue = :rake_tasks
  11
+
  12
+					def self.perform(path, command)
  13
+						Open3.popen(%{cd #{path} && #{command}}) do |stdin, stdout, stderr|
  14
+							raise stderr and return if stderr
  15
+							puts stdout.get
  16
+						end
  17
+					end
  18
+				end
  19
+
  20
+				module ImageJob
  21
+					@queue = :image_processing
  22
+
  23
+					def self.perform(url, pid = nil, socket = nil)
  24
+						# bump unicorn worker count if pid
  25
+						# curl url to socket if socket
  26
+						# drop unicorn worker count if pid
  27
+					end
  28
+				end
  29
+
  30
+			end
  31
+		end
  32
+
  33
+	end
  34
+end
16  lib/tasks/jobs.rake
... ...
@@ -0,0 +1,16 @@
  1
+require 'lib/jobs/noqueue'
  2
+
  3
+namespace :n2 do
  4
+  namespace :jobs do
  5
+    desc "Run job"
  6
+    task :run do
  7
+      @queue = N2::JobQueues::Noqueue::Job
  8
+
  9
+      @queue.enqueue @queue.workers::RakeJob, 'n2:jobs:test'
  10
+    end
  11
+
  12
+    task :test do
  13
+      puts "test rake task!!"
  14
+    end
  15
+  end
  16
+end

No commit comments for this range

Something went wrong with that request. Please try again.