external_resque_worker 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [Expected Behavior, LLC]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1 @@
1
+ we totally got the original code from somehwere, but I don't know where
@@ -0,0 +1,88 @@
1
+ class ExternalResqueWorker
2
+ DEFAULT_STARTUP_TIMEOUT = 1.minute
3
+
4
+ attr_accessor :pid, :startup_timeout
5
+
6
+ def initialize
7
+ raise "PEDANTICALLY CODED TO ONLY WORK IN TEST ENV" unless Rails.env.test?
8
+ install_hooks_on_startup
9
+ if self.pid = fork
10
+ Process.detach(pid)
11
+ wait_for_worker_to_start
12
+ else
13
+ STDOUT.reopen(File.open("#{Rails.root}/log/external_resque_worker.log", "a+")) # stops it from giving us the extra test output
14
+ start_child
15
+ end
16
+ end
17
+
18
+ def self.kill_all_existing_workers
19
+ while Resque::Worker.all.size > 0
20
+ Resque::Worker.all.each do |w|
21
+ Process.kill("TERM", w.pid) rescue nil
22
+ w.prune_dead_workers
23
+ end
24
+ end
25
+ end
26
+
27
+ def install_hooks_on_startup
28
+ install_pause_on_start_hook
29
+ end
30
+
31
+ def process_all
32
+ unpause
33
+ sleep 1 until done?
34
+ pause
35
+ end
36
+
37
+ def pause(pid = self.pid)
38
+ Process.kill("USR2", pid)
39
+ end
40
+
41
+ def unpause
42
+ Process.kill("CONT", pid)
43
+ end
44
+
45
+ private
46
+
47
+ def done?
48
+ all_queues_empty = Resque.queues.all? do |queue|
49
+ Resque.peek(queue).blank?
50
+ end
51
+ all_queues_empty and Resque::Worker.working.empty?
52
+ end
53
+
54
+ def start_parent
55
+ at_exit do
56
+ Process.kill("KILL", pid) if pid
57
+ end
58
+ end
59
+
60
+ def start_child
61
+ # Array form of exec() is required here, otherwise the worker is not a direct
62
+ # child process of test.
63
+ # If it's not the direct child process then the PID returned from fork() is
64
+ # wrong, which means we can't communicate with the worker.
65
+ exec('rake', "--silent", 'resque:work', "QUEUE=*", "INTERVAL=0.25", "RAILS_ENV=test")
66
+ end
67
+
68
+ def wait_for_worker_to_start
69
+ self.startup_timeout ||= DEFAULT_STARTUP_TIMEOUT
70
+ start = Time.now.to_i
71
+ while (Time.now.to_i - start) < startup_timeout
72
+ return if worker_started?
73
+ sleep 1
74
+ end
75
+
76
+ raise "Timeout while waiting for the worker to start. Waited #{startup_timeout} seconds."
77
+ end
78
+
79
+ def worker_started?
80
+ Resque.info[:workers].to_i > 0
81
+ end
82
+
83
+ def install_pause_on_start_hook
84
+ Resque.before_first_fork do
85
+ pause(Process.pid)
86
+ end unless Resque.before_first_fork
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: external_resque_worker
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Matt Gordon
14
+ - Joel Meador
15
+ - Chris Moore
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-12-15 00:00:00 -05:00
21
+ default_executable:
22
+ dependencies: []
23
+
24
+ description: Easy way to manage running one or more resque processes during testing
25
+ email: matt@expectedbehavior.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - README.md
34
+ - MIT-LICENSE
35
+ - lib/external_resque_worker.rb
36
+ has_rdoc: true
37
+ homepage: http://expectedbehavior.com
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.6.0
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Easy way to manage running one or more resque processes during testing
70
+ test_files: []
71
+