Module: ResqueScheduler::Server

Defined in:
lib/resque_scheduler/server.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/resque_scheduler/server.rb', line 8

def self.included(base)

  base.class_eval do

    helpers do
      def format_time(t)
        t.strftime("%Y-%m-%d %H:%M:%S %z")
      end

      def queue_from_class_name(class_name)
        Resque.queue_from_class(ResqueScheduler::Util.constantize(class_name))
      end
    end

    get "/schedule" do
      Resque.reload_schedule! if Resque::Scheduler.dynamic
      # Is there a better way to specify alternate template locations with sinatra?
      erb File.read(File.join(File.dirname(__FILE__), 'server/views/scheduler.erb'))
    end

    post "/schedule/requeue" do
      @job_name = params['job_name'] || params[:job_name]
      config = Resque.schedule[@job_name]
      @parameters = config['parameters'] || config[:parameters]
      if @parameters
        erb File.read(File.join(File.dirname(__FILE__), 'server/views/requeue-params.erb'))
      else
        Resque::Scheduler.enqueue_from_config(config)
        redirect u("/overview")
      end
    end

    post "/schedule/requeue_with_params" do
      job_name = params['job_name'] || params[:job_name]
      config = Resque.schedule[job_name]
      # Build args hash from post data (removing the job name)
       = params.reject {|key, value| key == 'job_name' || key == :job_name}

      # Merge constructed args hash with existing args hash for
      # the job, if it exists
      config_args = config['args'] || config[:args] || {}
      config_args = config_args.merge()

      # Insert the args hash into config and queue the resque job
      config = config.merge('args' => config_args)
      Resque::Scheduler.enqueue_from_config(config)
      redirect u("/overview")
    end

    get "/delayed" do
      # Is there a better way to specify alternate template locations with sinatra?
      erb File.read(File.join(File.dirname(__FILE__), 'server/views/delayed.erb'))
    end

    get "/delayed/:timestamp" do
      # Is there a better way to specify alternate template locations with sinatra?
      erb File.read(File.join(File.dirname(__FILE__), 'server/views/delayed_timestamp.erb'))
    end

    post "/delayed/queue_now" do
      timestamp = params['timestamp']
      Resque::Scheduler.enqueue_delayed_items_for_timestamp(timestamp.to_i) if timestamp.to_i > 0
      redirect u("/overview")
    end

    post "/delayed/clear" do
      Resque.reset_delayed_queue
      redirect u('delayed')
    end

  end

end