mobilize-base 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mobilize-base.rb CHANGED
@@ -70,7 +70,7 @@ if File.exists?(mongoid_config_path)
70
70
  end
71
71
  require 'google_drive'
72
72
  require 'resque'
73
- require "mobilize-base/extensions/resque"
73
+ require "mobilize-base/handlers/resque"
74
74
  #specify appropriate redis port per resque.yml
75
75
  Resque.redis = "127.0.0.1:#{Mobilize::Resque.config['redis_port']}"
76
76
  require 'popen4'
@@ -24,6 +24,8 @@ module Mobilize
24
24
  return working_workers if state == 'working'
25
25
  idle_workers = workers.select{|w| w.job['queue'].nil?}
26
26
  return idle_workers if state == 'idle'
27
+ stale_workers = workers.select{|w| Time.parse(w.started) < Jobtracker.deployed_at}
28
+ return stale_workers if state == 'stale'
27
29
  timeout_workers = workers.select{|w| w.job['payload'] and w.job['payload']['class']!='Jobtracker' and w.job['runat'] < (Time.now.utc - Jobtracker.max_run_time)}
28
30
  return timeout_workers if state == 'timeout'
29
31
  end
@@ -139,6 +141,18 @@ module Mobilize
139
141
  return true
140
142
  end
141
143
 
144
+ def Resque.kill_idle_stale_workers
145
+ idle_pids = Resque.workers('idle').select{|w| w.job=={}}.map{|w| w.to_s.split(":").second}
146
+ stale_pids = Resque.workers('stale').select{|w| w.job=={}}.map{|w| w.to_s.split(":").second}
147
+ idle_stale_pids = (idle_pids & stale_pids)
148
+ if idle_stale_pids.length == 0
149
+ return false
150
+ else
151
+ "kill #{idle_stale_pids.join(" ")}".bash
152
+ end
153
+ return true
154
+ end
155
+
142
156
  def Resque.kill_workers(count=nil)
143
157
  pids = Resque.workers.map{|w| w.to_s.split(":").second}
144
158
  if count.to_i > pids.length or count == 0
@@ -200,6 +200,19 @@ module Mobilize
200
200
  return true
201
201
  end
202
202
 
203
+ def Jobtracker.deployed_at
204
+ #assumes deploy is as of last commit, or as of last deploy time
205
+ #as given by the least recently updated file in the root folder
206
+ deploy_time = begin
207
+ %{git log -1 --format="%cd"}.bash
208
+ rescue
209
+ ls_string = "ls -l #{ENV['PWD']}/*".bash
210
+ ls_rows = ls_string.split("\n").map{|lss| lss.strip.split(" ")}
211
+ mod_times = ls_rows.select{|lsr| lsr.length == 8}.map{|lsr| lsr[5..6].join(" ")}
212
+ mod_times.min
213
+ end
214
+ Time.parse(deploy_time)
215
+ end
203
216
 
204
217
  #test methods
205
218
  def Jobtracker.restart_test_redis
@@ -30,16 +30,7 @@ module Mobilize
30
30
  def runner_path
31
31
  u = self
32
32
  prefix = "Runner_"
33
- suffix = ""
34
- if Base.env == 'development'
35
- suffix = "(dev)"
36
- elsif Base.env == 'test'
37
- suffix = "(test)"
38
- elsif Base.env == 'production'
39
- suffix = ""
40
- else
41
- raise "Invalid environment"
42
- end
33
+ suffix = (Base.env == 'production' ? "" : "(#{Base.env})")
43
34
  title = [prefix,u.name,suffix,"/jobs"].join
44
35
  return title
45
36
  end
@@ -2,11 +2,10 @@
2
2
  # will give you the resque tasks
3
3
 
4
4
  namespace :mobilize do
5
+ require 'mobilize-base'
5
6
  desc "Start a Resque worker"
6
7
  task :work do
7
- require 'resque'
8
8
  begin
9
- require 'mobilize-base'
10
9
  #require specified mobilize gems
11
10
  Mobilize::Base.config('jobtracker')['extensions'].each do |e|
12
11
  require e
@@ -24,6 +23,14 @@ namespace :mobilize do
24
23
 
25
24
  worker.work(ENV['INTERVAL'] || 5) # interval, will block
26
25
  end
26
+ desc "Kill idle workers not in sync with repo"
27
+ task :kill_idle_stale_workers do
28
+ Mobilize::Jobtracker.kill_idle_stale_workers
29
+ end
30
+ desc "Make sure workers are prepped"
31
+ task :prep_workers do
32
+ Mobilize::Jobtracker.prep_workers
33
+ end
27
34
  end
28
35
  namespace :mobilize_base do
29
36
  desc "Set up config and log folders and files"
@@ -1,5 +1,5 @@
1
1
  module Mobilize
2
2
  module Base
3
- VERSION = "1.0.4"
3
+ VERSION = "1.0.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobilize-base
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-07 00:00:00.000000000 Z
12
+ date: 2012-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -211,7 +211,6 @@ files:
211
211
  - lib/mobilize-base/extensions/google_drive/worksheet.rb
212
212
  - lib/mobilize-base/extensions/hash.rb
213
213
  - lib/mobilize-base/extensions/object.rb
214
- - lib/mobilize-base/extensions/resque.rb
215
214
  - lib/mobilize-base/extensions/string.rb
216
215
  - lib/mobilize-base/handlers/email.rb
217
216
  - lib/mobilize-base/handlers/gbook.rb
@@ -219,6 +218,7 @@ files:
219
218
  - lib/mobilize-base/handlers/gfile.rb
220
219
  - lib/mobilize-base/handlers/gridfs.rb
221
220
  - lib/mobilize-base/handlers/gsheet.rb
221
+ - lib/mobilize-base/handlers/resque.rb
222
222
  - lib/mobilize-base/jobtracker.rb
223
223
  - lib/mobilize-base/models/dataset.rb
224
224
  - lib/mobilize-base/models/job.rb
@@ -252,12 +252,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
252
252
  - - ! '>='
253
253
  - !ruby/object:Gem::Version
254
254
  version: '0'
255
+ segments:
256
+ - 0
257
+ hash: 64363452051803879
255
258
  required_rubygems_version: !ruby/object:Gem::Requirement
256
259
  none: false
257
260
  requirements:
258
261
  - - ! '>='
259
262
  - !ruby/object:Gem::Version
260
263
  version: '0'
264
+ segments:
265
+ - 0
266
+ hash: 64363452051803879
261
267
  requirements: []
262
268
  rubyforge_project: mobilize-base
263
269
  rubygems_version: 1.8.24
@@ -271,4 +277,3 @@ test_files:
271
277
  - test/mobilize-base_test.rb
272
278
  - test/redis-test.conf
273
279
  - test/test_helper.rb
274
- has_rdoc: