Module: Resque::Plugins::Telework::Server

Defined in:
lib/resque-telework/server.rb

Constant Summary collapse

VIEW_PATH =
File.join(File.dirname(__FILE__), 'server', 'views')
PUBLIC_PATH =
File.join(File.dirname(__FILE__), 'server', 'public')

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/resque-telework/server.rb', line 11

def self.registered( app )
  appn= 'Telework'
  
  # This helpers adds stuff to the app closure
  app.helpers do
    @@myredis= TeleworkRedis.new
    def redis
      @@myredis
    end
    def my_substabs
      ["Overview", "Start", "Misc"]
    end
    def my_show(page, layout = true)
      response["Cache-Control"] = "max-age=0, private, must-revalidate"
      begin
        erb(File.read(File.join(VIEW_PATH, "#{page}.erb")), {:layout => layout}, :resque => Resque)
      rescue Errno::ECONNREFUSED
        erb :error, {:layout => false}, :error => "Can't connect to Redis! (#{Resque.redis_id})"
      end
    end
    def generic_filter(id, name, list, more= "")
      html = "<select id=\"#{id}\" name=\"#{name}\" #{more}>"
      value= list[0]
      list.each do |k|
        selected = k == value ? 'selected="selected"' : ''
        html += "<option #{selected} value=\"#{k}\">#{k}</option>"
      end
      html += "</select>"
    end
    def generic_filter_with_dis(id, name, list, more= "")
      html = "<select id=\"#{id}\" name=\"#{name}\" #{more}>"
      value= list[0][0]
      list.each do |k,dis|
        selected = k == value ? 'selected="selected"' : ''
        html += "<option #{selected} value=\"#{k}\">#{dis}</option>"
      end
      html += "</select>"
    end
    def task_default
      { 'auto_max_waiting_job_per_worker' => 1,'auto_worker_min' => 0, 'auto_delay' => 15,
        'log_snapshot_period' => 30, 'log_snapshot_lines' => 40, 'exec' => "bundle exec rake resque:work --trace"
      }
    end
  end

  app.get "/#{appn.downcase}" do
    redirect "/resque/#{appn.downcase}/Overview"
  end
  
  app.get "/#{appn.downcase}/Overview" do
    @refresh= 10
    if params[:refresh]
      @refresh= params[:refresh].to_i
      @refresh= nil if @refresh==0
    end
    @status_messages= 100
    @scheduling= nil
    my_show appn.downcase
  end

  app.get "/#{appn.downcase}/Start" do
    @status_messages= 100
    @scheduling= true
    my_show appn.downcase
  end
  
  app.get "/#{appn.downcase}/Misc" do
    my_show 'misc'
  end          
  
  app.get "/#{appn.downcase}/revision/:revision" do
    @revision= params[:revision]
    my_show 'revision' 
  end

  app.get "/#{appn.downcase}/worker/:host/:worker" do
    @worker= params[:worker]
    @host= params[:host]
    my_show 'worker' 
  end

  app.get "/#{appn.downcase}/task/:host/:task_id" do
    @task_id= params[:task_id]
    @host= params[:host]
    my_show 'task' 
  end

  app.get "/#{appn.downcase}/host/:host" do
    @host= params[:host]
    my_show 'host' 
  end
  
  app.get "/#{appn.downcase}/config" do
    content_type :json
    redis.configuration
  end
  
  app.post "/#{appn.downcase}_stopit/:worker" do
    @worker= params[:worker]
    @host= nil
    @daemon= nil
    redis.hosts.each do |h|
      redis.workers(h).each do |id, info|
        @host= h if id==@worker # TODO: break nested loops
      end
    end
    redis.cmds_push( @host, { 'command' => 'stop_worker', 'worker_id'=> @worker } ) if @host
    my_show 'stopit'
  end

  app.post "/#{appn.downcase}_stopitd/:host" do
    # Todo - check that the host indeed exists
    @host= params[:host]
    @daemon= true
    redis.cmds_push( @host, { 'command' => 'stop_daemon' } )
    my_show 'stopit'
  end

  app.post "/#{appn.downcase}_killitd/:host" do
    # Todo - check that the host indeed exists
    @host= params[:host]
    @daemon= true
    @kill= true
    redis.cmds_push( @host, { 'command' => 'kill_daemon' } )
    my_show 'stopit'
  end

  app.post "/#{appn.downcase}_del_host/:host" do
    host= params[:host]
    redis.hosts_rem( host )
    redis.aliases_rem( host )
    redirect "/resque/#{appn.downcase}"            
  end          

  app.post "/#{appn.downcase}_mod_host/:host" do
    host= params[:host]
    ahost= params[:alias]
    comment= params[:comment]
    if ahost.blank? || ahost==host
      redis.aliases_rem( host )
    else
      redis.aliases_add( host, ahost )
    end
    if comment.blank?
      redis.comments_rem( host )
    else
      redis.comments_add( host, comment )
    end
    redirect "/resque/#{appn.downcase}"            
  end

  app.post "/#{appn.downcase}_mod_task/:task" do
    @task_id= params[:task]
    @host= nil
    redis.hosts.each do |h|
      redis.tasks(h).each do |id, info|
        @host= h if id==@task_id # TODO: break nested loops
      end
    end
    @task= redis.tasks_by_id( @host, @task_id )
    all= ['log_snapshot_period', 'log_snapshot_lines', 'exec', 'worker_count',
          'auto_delay', 'auto_max_waiting_job_per_worker', 'auto_worker_min' ]
    all.each do |a|
      @task[a]= params[a]
    end
    redis.tasks_add( @host , @task_id, @task )
    redirect "/resque/#{appn.downcase}"
  end

  app.post "/#{appn.downcase}_killit/:worker" do
    @worker= params[:worker]
    @host= nil
    @kill= true
    redis.hosts.each do |h|
      redis.workers(h).each do |id, info|
        @host= h if id==@worker # TODO: break nested loops
      end
    end
    redis.cmds_push( @host, { 'command' => 'kill_worker', 'worker_id'=> @worker } ) if @host
    my_show 'stopit'
  end
  
  app.post "/#{appn.downcase}/add_note" do
    @user= params[:note_user]
    @date= Time.now
    @note= params[:note_text]
    redis.notes_push({ 'user'=> @user, 'date'=> @date, 'note' => @note })
    redirect "/resque/#{appn.downcase}"
  end
  
  app.post "/#{appn.downcase}_del_note/:note" do
    @note_id= params[:note]
    redis.notes_del(@note_id)
    redirect "/resque/#{appn.downcase}"
  end
  
  # Start a task
  app.post "/telework/start_task" do
    @host= params[:h]
    @queue= params[:q]
    @qmanual= params[:qmanual]
    @count= params[:c]
    #@rev= params[:r].split(' ')
    @envv= params[:e]
    @q= @qmanual.blank? ? @queue : @qmanual
    id= redis.unique_id.to_s
    t= task_default
    redis.tasks_add( @host , id, t.merge( { 'task_id' => id, 'worker_count' => @count,
                                            'rails_env' => @envv, 'queue' => @q,
                                            'worker_id' => [], 'worker_status' => 'Stopped'} ) )
    redirect "/resque/#{appn.downcase}"          
  end
  
  app.post "/#{appn.downcase}/delete" do
    @task_id= params[:task]
    @host= params[:host]
    redis.tasks_rem( @host, @task_id )
    redirect "/resque/#{appn.downcase}"            
  end
  
  # Start workers
  app.post "/#{appn.downcase}/start" do
    @task_id= params[:task]
    @host= params[:host]
    @rev= params[:rev].split(',')
    @task= redis.tasks_by_id(@host, @task_id)
    count= params[:count]
    id= []
    for i in 1..count.to_i do
      w= @task
      w['worker_id']= redis.unique_id.to_s
      id << w['worker_id']
      w['worker_status']= 'Starting'
      w['revision']= @rev[0]
      w['revision_small']= @rev[1]
      w['command']= 'start_worker'
      w['task_id']= @task_id
      redis.cmds_push( @host, w )
    end
    @task['worker_id']= id
    @task['worker_count']= count
    redis.tasks_add( @host, @task_id, @task )
    redirect "/resque/#{appn.downcase}"
  end

  app.post "/#{appn.downcase}/start_auto" do
    @task_id= params[:task]
    @host= params[:host]
    @rev= params[:rev].split(',')
    @task= redis.tasks_by_id(@host, @task_id)
    count= params[:count]        
    wid= []
    for i in 1..count.to_i do
      wid << redis.unique_id.to_s
      #redis.cmds_push( @host, w )
    end
    @task['worker_id']= wid
    @task['worker_count']= count
    @task['mode']= 'auto'
    cmd= @task
    cmd['task_id']= @task_id
    cmd['revision']= @rev[0]
    cmd['revision_small']= @rev[1]
    cmd['command']= 'start_auto'
    redis.cmds_push( @host, cmd )
    redis.tasks_add( @host, @task_id, @task )
    redirect "/resque/#{appn.downcase}"
  end

  app.post "/#{appn.downcase}/stop_auto" do
    @task_id= params[:task]
    @host= params[:host]
    @task= redis.tasks_by_id(@host, @task_id)
    cmd= @task
    cmd['command']= 'stop_auto' 
    redis.cmds_push( @host, cmd )
    redirect "/resque/#{appn.downcase}"                
  end


  app.post "/#{appn.downcase}/pause" do
    @task_id= params[:task]
    @host= params[:host]
    @cont= params[:cont]=="true"
    @task= redis.tasks_by_id(@host, @task_id)
    @task['worker_id'].each do |id|
      redis.cmds_push( @host, { 'command' => 'signal_worker', 'worker_id'=> id, 'action' => @cont ? 'CONT' : 'PAUSE' } ) 
    end
    redirect "/resque/#{appn.downcase}"
  end

  app.post "/#{appn.downcase}/stop" do
    @task_id= params[:task]
    @host= params[:host]
    @kill= params[:kill]=="true"
    @task= redis.tasks_by_id(@host, @task_id)
    @task['worker_id'].each do |id|
      redis.cmds_push( @host, { 'command' => 'signal_worker', 'worker_id'=> id, 'action' => @kill ? 'KILL' : 'QUIT' } ) 
    end
    redirect "/resque/#{appn.downcase}"
  end

  app.post "/#{appn.downcase}/stop_all" do
    @kill= params[:mode]=="Kill"
    hl= [ params[:h] ]
    hl= redis.hosts if params[:h]=="[All hosts]"
    hl.each do |h|
      redis.workers(h).each do |id, info|
        unless info['worker_status']=='Stopped'
          redis.cmds_push( h, { 'command' => 'signal_worker', 'worker_id'=> id, 'action' => @kill ? 'KILL' : 'QUIT' } ) 
        end
      end
    end
    redirect "/resque/#{appn.downcase}"
  end

                      
  app.tabs << appn
  
end