background_queue 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/.rvmrc +48 -0
  4. data/Gemfile +19 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.md +69 -0
  7. data/Rakefile +42 -0
  8. data/TODO +2 -0
  9. data/VERSION +1 -0
  10. data/background_queue.gemspec +158 -0
  11. data/bin/bg_queue +26 -0
  12. data/lib/background_queue.rb +8 -0
  13. data/lib/background_queue/client.rb +96 -0
  14. data/lib/background_queue/client_lib/command.rb +36 -0
  15. data/lib/background_queue/client_lib/config.rb +109 -0
  16. data/lib/background_queue/client_lib/connection.rb +105 -0
  17. data/lib/background_queue/client_lib/job_handle.rb +19 -0
  18. data/lib/background_queue/command.rb +49 -0
  19. data/lib/background_queue/config.rb +118 -0
  20. data/lib/background_queue/server_lib/balanced_queue.rb +108 -0
  21. data/lib/background_queue/server_lib/config.rb +339 -0
  22. data/lib/background_queue/server_lib/event_connection.rb +133 -0
  23. data/lib/background_queue/server_lib/event_server.rb +35 -0
  24. data/lib/background_queue/server_lib/job.rb +252 -0
  25. data/lib/background_queue/server_lib/job_registry.rb +30 -0
  26. data/lib/background_queue/server_lib/lru.rb +193 -0
  27. data/lib/background_queue/server_lib/owner.rb +54 -0
  28. data/lib/background_queue/server_lib/priority_queue.rb +156 -0
  29. data/lib/background_queue/server_lib/queue_registry.rb +123 -0
  30. data/lib/background_queue/server_lib/server.rb +314 -0
  31. data/lib/background_queue/server_lib/sorted_workers.rb +52 -0
  32. data/lib/background_queue/server_lib/task.rb +79 -0
  33. data/lib/background_queue/server_lib/task_registry.rb +51 -0
  34. data/lib/background_queue/server_lib/thread_manager.rb +121 -0
  35. data/lib/background_queue/server_lib/worker.rb +18 -0
  36. data/lib/background_queue/server_lib/worker_balancer.rb +97 -0
  37. data/lib/background_queue/server_lib/worker_client.rb +94 -0
  38. data/lib/background_queue/server_lib/worker_thread.rb +70 -0
  39. data/lib/background_queue/utils.rb +40 -0
  40. data/lib/background_queue/worker/base.rb +46 -0
  41. data/lib/background_queue/worker/calling.rb +59 -0
  42. data/lib/background_queue/worker/config.rb +35 -0
  43. data/lib/background_queue/worker/environment.rb +70 -0
  44. data/lib/background_queue/worker/worker_loader.rb +94 -0
  45. data/lib/background_queue_server.rb +21 -0
  46. data/lib/background_queue_worker.rb +5 -0
  47. data/spec/background_queue/client_lib/command_spec.rb +75 -0
  48. data/spec/background_queue/client_lib/config_spec.rb +156 -0
  49. data/spec/background_queue/client_lib/connection_spec.rb +170 -0
  50. data/spec/background_queue/client_spec.rb +95 -0
  51. data/spec/background_queue/command_spec.rb +34 -0
  52. data/spec/background_queue/config_spec.rb +134 -0
  53. data/spec/background_queue/server_lib/balanced_queue_spec.rb +122 -0
  54. data/spec/background_queue/server_lib/config_spec.rb +443 -0
  55. data/spec/background_queue/server_lib/event_connection_spec.rb +190 -0
  56. data/spec/background_queue/server_lib/event_server_spec.rb +48 -0
  57. data/spec/background_queue/server_lib/integration/full_test_spec.rb +247 -0
  58. data/spec/background_queue/server_lib/integration/queue_integration_spec.rb +98 -0
  59. data/spec/background_queue/server_lib/integration/serialize_spec.rb +127 -0
  60. data/spec/background_queue/server_lib/job_registry_spec.rb +65 -0
  61. data/spec/background_queue/server_lib/job_spec.rb +525 -0
  62. data/spec/background_queue/server_lib/owner_spec.rb +33 -0
  63. data/spec/background_queue/server_lib/priority_queue_spec.rb +182 -0
  64. data/spec/background_queue/server_lib/server_spec.rb +353 -0
  65. data/spec/background_queue/server_lib/sorted_workers_spec.rb +122 -0
  66. data/spec/background_queue/server_lib/task_registry_spec.rb +69 -0
  67. data/spec/background_queue/server_lib/task_spec.rb +20 -0
  68. data/spec/background_queue/server_lib/thread_manager_spec.rb +106 -0
  69. data/spec/background_queue/server_lib/worker_balancer_spec.rb +127 -0
  70. data/spec/background_queue/server_lib/worker_client_spec.rb +196 -0
  71. data/spec/background_queue/server_lib/worker_thread_spec.rb +125 -0
  72. data/spec/background_queue/utils_spec.rb +27 -0
  73. data/spec/background_queue/worker/base_spec.rb +35 -0
  74. data/spec/background_queue/worker/calling_spec.rb +103 -0
  75. data/spec/background_queue/worker/environment_spec.rb +67 -0
  76. data/spec/background_queue/worker/worker_loader_spec.rb +103 -0
  77. data/spec/background_queue_spec.rb +7 -0
  78. data/spec/resources/config-client.yml +7 -0
  79. data/spec/resources/config-serialize.yml +12 -0
  80. data/spec/resources/config.yml +12 -0
  81. data/spec/resources/example_worker.rb +4 -0
  82. data/spec/resources/example_worker_with_error.rb +4 -0
  83. data/spec/resources/test_worker.rb +8 -0
  84. data/spec/shared/queue_registry_shared.rb +216 -0
  85. data/spec/spec_helper.rb +15 -0
  86. data/spec/support/default_task.rb +9 -0
  87. data/spec/support/private.rb +23 -0
  88. data/spec/support/simple_server.rb +28 -0
  89. data/spec/support/simple_task.rb +58 -0
  90. data/spec/support/test_worker_server.rb +205 -0
  91. metadata +254 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p125@bg_q"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.10.3" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem "json"
6
+ gem "rufus-scheduler"
7
+ gem "eventmachine", "~>0.12.10"
8
+ gem "ipaddress", "~> 0.8.0"
9
+
10
+ # Add dependencies to develop your gem here.
11
+ # Include everything needed to run rake, tests, features, etc.
12
+ group :development do
13
+ gem "rspec", ">= 2.9.0"
14
+ gem "jeweler", "~> 1.8.3"
15
+ gem "yard", "~> 0.7"
16
+ gem "rdoc", "~> 3.12"
17
+ gem "bundler", "~> 1.0.0"
18
+ gem "redcarpet", "~>2.1.1"
19
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 MarkPent
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,69 @@
1
+ # background_queue
2
+
3
+ This is a gem to manage background tasks. The primary focus is organising the tasks so they will not overload the machine(s) running the tasks, while
4
+ still giving a fair, balanced allocation of running time to members in the queue.
5
+
6
+
7
+ ## Usage
8
+
9
+ ### Initializing
10
+ In your environment, initialize a constant BackgroundQueue::Client
11
+
12
+ BG_QUEUE = BackgroundQueue::Client.new(_PATH_TO_CONFIG_)
13
+
14
+ ### Starting a single task
15
+ Now you can use your BG\_QUEUE to add a single task. Note: the "task_id" should be unique, even between owners/jobs. If there is an existing task with the same id in any queue,
16
+ even if its a different owner/job, the existing task will be removed before this task is added the the ownerjob queue.
17
+
18
+ job_handle = BG_QUEUE.add_task(:worker_name, "owner identifier", "job identifier", "task_id", priority, {:some_task=>:params}, {:priority=>1})
19
+
20
+
21
+ ### Starting multiple tasks
22
+ Or you can queue multiple tasks at once.
23
+
24
+ job_handle = BG_QUEUE.add_tasks(:worker_name, "owner identifier", "job identifier", [["task1_id" , {:some_task=>:params}], ["task2_id" , {:some_task=>:params}]], priority, {:shared=>:params}, {:priority=>1})
25
+
26
+ ### Job Status
27
+ Get the status of the job
28
+
29
+ status = BG_QUEUE.get_status(job_handle)
30
+
31
+
32
+ ## Queue Management
33
+ * Each task on the queue is associated with an 'owner', 'job' and 'id'
34
+ * Each job has a priority (0=highest).
35
+ * An 'owner' has a queue of 'job' which is a list of tasks.
36
+ * Status is tracked per job.
37
+ * Tasks within a job can be weighted.
38
+
39
+ ### Finding the next task to run
40
+ 1. Get the next owner with the highest priority subject
41
+ 2. Get the next job within the above owner with the highest priority
42
+ 3. Pop the next task off the job queue.
43
+ 4. Push the job to the end of the owners job queue.
44
+ 5. Push the owner to the end of the queue.
45
+
46
+ ## Worker Managment
47
+ * The actual workers are implimented using passenger.
48
+ * The queue calls the passenger server(s) through HTTP.
49
+ * The queue limits the number of simultaneous workers at any one time.
50
+ * Using passenger to manage the workers means workers are efficiently re-used, with a full Rails enviroment loaded and ready to go.
51
+ * The status of the task is streamed to the queue manager.
52
+
53
+
54
+
55
+ ## Contributing to background_queue
56
+
57
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
58
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
59
+ * Fork the project.
60
+ * Start a feature/bugfix branch.
61
+ * Commit and push until you are happy with your contribution.
62
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
63
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
64
+
65
+ ## Copyright
66
+
67
+ Copyright (c) 2012 MarkPent. See LICENSE.txt for
68
+ further details.
69
+
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "background_queue"
18
+ gem.homepage = "http://github.com/markpent/background_queue"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Background processing}
21
+ gem.description = %Q{Organise background tasks so they will not overload the machine(s) running the tasks, while still giving a fair, balanced allocation of running time to members in the queue}
22
+ gem.email = "mark.pent@gmail.com"
23
+ gem.authors = ["MarkPent"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ Server
2
+ Raise priority of tasks that have been waiting a long time
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,158 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "background_queue"
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["MarkPent"]
12
+ s.date = "2012-10-09"
13
+ s.description = "Organise background tasks so they will not overload the machine(s) running the tasks, while still giving a fair, balanced allocation of running time to members in the queue"
14
+ s.email = "mark.pent@gmail.com"
15
+ s.executables = ["bg_queue"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.md",
19
+ "TODO"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".rspec",
24
+ ".rvmrc",
25
+ "Gemfile",
26
+ "LICENSE.txt",
27
+ "README.md",
28
+ "Rakefile",
29
+ "TODO",
30
+ "VERSION",
31
+ "background_queue.gemspec",
32
+ "bin/bg_queue",
33
+ "lib/background_queue.rb",
34
+ "lib/background_queue/client.rb",
35
+ "lib/background_queue/client_lib/command.rb",
36
+ "lib/background_queue/client_lib/config.rb",
37
+ "lib/background_queue/client_lib/connection.rb",
38
+ "lib/background_queue/client_lib/job_handle.rb",
39
+ "lib/background_queue/command.rb",
40
+ "lib/background_queue/config.rb",
41
+ "lib/background_queue/server_lib/balanced_queue.rb",
42
+ "lib/background_queue/server_lib/config.rb",
43
+ "lib/background_queue/server_lib/event_connection.rb",
44
+ "lib/background_queue/server_lib/event_server.rb",
45
+ "lib/background_queue/server_lib/job.rb",
46
+ "lib/background_queue/server_lib/job_registry.rb",
47
+ "lib/background_queue/server_lib/lru.rb",
48
+ "lib/background_queue/server_lib/owner.rb",
49
+ "lib/background_queue/server_lib/priority_queue.rb",
50
+ "lib/background_queue/server_lib/queue_registry.rb",
51
+ "lib/background_queue/server_lib/server.rb",
52
+ "lib/background_queue/server_lib/sorted_workers.rb",
53
+ "lib/background_queue/server_lib/task.rb",
54
+ "lib/background_queue/server_lib/task_registry.rb",
55
+ "lib/background_queue/server_lib/thread_manager.rb",
56
+ "lib/background_queue/server_lib/worker.rb",
57
+ "lib/background_queue/server_lib/worker_balancer.rb",
58
+ "lib/background_queue/server_lib/worker_client.rb",
59
+ "lib/background_queue/server_lib/worker_thread.rb",
60
+ "lib/background_queue/utils.rb",
61
+ "lib/background_queue/worker/base.rb",
62
+ "lib/background_queue/worker/calling.rb",
63
+ "lib/background_queue/worker/config.rb",
64
+ "lib/background_queue/worker/environment.rb",
65
+ "lib/background_queue/worker/worker_loader.rb",
66
+ "lib/background_queue_server.rb",
67
+ "lib/background_queue_worker.rb",
68
+ "spec/background_queue/client_lib/command_spec.rb",
69
+ "spec/background_queue/client_lib/config_spec.rb",
70
+ "spec/background_queue/client_lib/connection_spec.rb",
71
+ "spec/background_queue/client_spec.rb",
72
+ "spec/background_queue/command_spec.rb",
73
+ "spec/background_queue/config_spec.rb",
74
+ "spec/background_queue/server_lib/balanced_queue_spec.rb",
75
+ "spec/background_queue/server_lib/config_spec.rb",
76
+ "spec/background_queue/server_lib/event_connection_spec.rb",
77
+ "spec/background_queue/server_lib/event_server_spec.rb",
78
+ "spec/background_queue/server_lib/integration/full_test_spec.rb",
79
+ "spec/background_queue/server_lib/integration/queue_integration_spec.rb",
80
+ "spec/background_queue/server_lib/integration/serialize_spec.rb",
81
+ "spec/background_queue/server_lib/job_registry_spec.rb",
82
+ "spec/background_queue/server_lib/job_spec.rb",
83
+ "spec/background_queue/server_lib/owner_spec.rb",
84
+ "spec/background_queue/server_lib/priority_queue_spec.rb",
85
+ "spec/background_queue/server_lib/server_spec.rb",
86
+ "spec/background_queue/server_lib/sorted_workers_spec.rb",
87
+ "spec/background_queue/server_lib/task_registry_spec.rb",
88
+ "spec/background_queue/server_lib/task_spec.rb",
89
+ "spec/background_queue/server_lib/thread_manager_spec.rb",
90
+ "spec/background_queue/server_lib/worker_balancer_spec.rb",
91
+ "spec/background_queue/server_lib/worker_client_spec.rb",
92
+ "spec/background_queue/server_lib/worker_thread_spec.rb",
93
+ "spec/background_queue/utils_spec.rb",
94
+ "spec/background_queue/worker/base_spec.rb",
95
+ "spec/background_queue/worker/calling_spec.rb",
96
+ "spec/background_queue/worker/environment_spec.rb",
97
+ "spec/background_queue/worker/worker_loader_spec.rb",
98
+ "spec/background_queue_spec.rb",
99
+ "spec/resources/config-client.yml",
100
+ "spec/resources/config-serialize.yml",
101
+ "spec/resources/config.yml",
102
+ "spec/resources/example_worker.rb",
103
+ "spec/resources/example_worker_with_error.rb",
104
+ "spec/resources/test_worker.rb",
105
+ "spec/shared/queue_registry_shared.rb",
106
+ "spec/spec_helper.rb",
107
+ "spec/support/default_task.rb",
108
+ "spec/support/private.rb",
109
+ "spec/support/simple_server.rb",
110
+ "spec/support/simple_task.rb",
111
+ "spec/support/test_worker_server.rb"
112
+ ]
113
+ s.homepage = "http://github.com/markpent/background_queue"
114
+ s.licenses = ["MIT"]
115
+ s.require_paths = ["lib"]
116
+ s.rubygems_version = "1.8.17"
117
+ s.summary = "Background processing"
118
+
119
+ if s.respond_to? :specification_version then
120
+ s.specification_version = 3
121
+
122
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
123
+ s.add_runtime_dependency(%q<json>, [">= 0"])
124
+ s.add_runtime_dependency(%q<rufus-scheduler>, [">= 0"])
125
+ s.add_runtime_dependency(%q<eventmachine>, ["~> 0.12.10"])
126
+ s.add_runtime_dependency(%q<ipaddress>, ["~> 0.8.0"])
127
+ s.add_development_dependency(%q<rspec>, [">= 2.9.0"])
128
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
129
+ s.add_development_dependency(%q<yard>, ["~> 0.7"])
130
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
131
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
132
+ s.add_development_dependency(%q<redcarpet>, ["~> 2.1.1"])
133
+ else
134
+ s.add_dependency(%q<json>, [">= 0"])
135
+ s.add_dependency(%q<rufus-scheduler>, [">= 0"])
136
+ s.add_dependency(%q<eventmachine>, ["~> 0.12.10"])
137
+ s.add_dependency(%q<ipaddress>, ["~> 0.8.0"])
138
+ s.add_dependency(%q<rspec>, [">= 2.9.0"])
139
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
140
+ s.add_dependency(%q<yard>, ["~> 0.7"])
141
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
142
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
143
+ s.add_dependency(%q<redcarpet>, ["~> 2.1.1"])
144
+ end
145
+ else
146
+ s.add_dependency(%q<json>, [">= 0"])
147
+ s.add_dependency(%q<rufus-scheduler>, [">= 0"])
148
+ s.add_dependency(%q<eventmachine>, ["~> 0.12.10"])
149
+ s.add_dependency(%q<ipaddress>, ["~> 0.8.0"])
150
+ s.add_dependency(%q<rspec>, [">= 2.9.0"])
151
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
152
+ s.add_dependency(%q<yard>, ["~> 0.7"])
153
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
154
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
155
+ s.add_dependency(%q<redcarpet>, ["~> 2.1.1"])
156
+ end
157
+ end
158
+
data/bin/bg_queue ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'background_queue_server'
4
+
5
+ server = BackgroundQueue::ServerLib::Server.new
6
+
7
+ begin
8
+ options = server.process_args(ARGV)
9
+ if options[:command] == :stop
10
+ server.stop_pid(options)
11
+ else
12
+ server.start(options)
13
+ end
14
+ rescue SystemExit=>se
15
+
16
+ rescue BackgroundQueue::ServerLib::InitError=>ie
17
+ puts ie.message
18
+ exit(1)
19
+ rescue Exception=>e
20
+ puts "#{e.class.name} : #{e.message}"
21
+ puts e.backtrace.join("\n")
22
+ exit(1)
23
+ end
24
+
25
+
26
+
@@ -0,0 +1,8 @@
1
+ require "background_queue/utils"
2
+ require "background_queue/config"
3
+ require "background_queue/command"
4
+ require "background_queue/client_lib/config"
5
+ require "background_queue/client_lib/command"
6
+ require "background_queue/client_lib/connection"
7
+ require "background_queue/client_lib/job_handle"
8
+ require "background_queue/client"
@@ -0,0 +1,96 @@
1
+ #The Background Queue will schedule tasks in the background. Refer to README for more details.
2
+ module BackgroundQueue
3
+ #The main interface to the background queue from the client.
4
+ #this class will look after sending a command to the server, using a failover server if needed.
5
+ class Client
6
+
7
+ attr_accessor :config
8
+
9
+ def initialize(path)
10
+ @config = BackgroundQueue::ClientLib::Config.load_file(path)
11
+ end
12
+
13
+ #add a task to the background
14
+ def add_task(worker, owner_id, job_id, task_id, priority, task_parameters={}, options={}, server=nil )
15
+ job_id, task_id = generate_ids(worker, owner_id, job_id, task_id)
16
+ result, server = send_command(BackgroundQueue::ClientLib::Command.add_task_command(worker, owner_id, job_id, task_id, priority, task_parameters, options ), server)
17
+ #the server currently either returns :ok or an exception would have been thrown
18
+ BackgroundQueue::ClientLib::JobHandle.new(owner_id, job_id, server)
19
+ end
20
+
21
+ #add multiple tasks to the background, all with the same worker/owner/job
22
+ def add_tasks(worker, owner_id, job_id, tasks, priority, shared_parameters={}, options={}, server=nil )
23
+ send_command(BackgroundQueue::ClientLib::Command.add_tasks_command(worker, owner_id, job_id, tasks, priority, shared_parameters, options ), server)
24
+ end
25
+
26
+ def get_status(job_handle, options={})
27
+ result, server = send_command(BackgroundQueue::ClientLib::Command.get_status_command(job_handle.job_id, options ), job_handle.server)
28
+ result
29
+ end
30
+
31
+ def get_stats(server, options={})
32
+ result, server = send_command(BackgroundQueue::ClientLib::Command.stats_command(options ), server)
33
+ result.args
34
+ end
35
+
36
+
37
+ #removed for now
38
+ #remove tasks from the background queue
39
+ #def remove_tasks(tasks, options={})
40
+ # send_command(BackgroundQueue::ClientLib::Command.remove_tasks_command(tasks, options))
41
+ #end
42
+
43
+
44
+ private
45
+
46
+ #generate what should be a unique id for a job/task
47
+ def generate_ids(worker, owner_id, job_id, task_id)
48
+ if job_id.nil?
49
+ job_id = "#{worker}-#{get_node_name}-#{Process.pid}-#{Time.now.to_i}"
50
+ end
51
+
52
+ if task_id.nil?
53
+ task_id = "#{job_id}-#{Time.now.to_i}"
54
+ end
55
+
56
+ [job_id, task_id]
57
+ end
58
+
59
+ def get_node_name
60
+ if @@this_node_name.nil?
61
+ hostname = Socket.gethostname
62
+ @@this_node_name = hostname.index(".").nil? ? hostname : hostname[0, hostname.index(".")]
63
+ end
64
+ @@this_node_name
65
+ end
66
+
67
+ def send_command(command, server=nil)
68
+ server = @config.server if server.nil?
69
+ begin
70
+ send_command_to_server(command, server)
71
+ rescue BackgroundQueue::ClientLib::ConnectionError=>e
72
+ failures = []
73
+ failures << e.message
74
+ @config.failover.each_with_index do |server, idx|
75
+ begin
76
+ return send_command_to_server(command, server)
77
+ rescue BackgroundQueue::ClientLib::ConnectionError=>e2
78
+ failures << ", Attempt #{idx + 2}: #{e2.message}"
79
+ end
80
+ end
81
+ raise ClientException, failures.join("")
82
+ end
83
+ end
84
+
85
+ def send_command_to_server(command, server)
86
+ connection = BackgroundQueue::ClientLib::Connection.new(self, server)
87
+ [connection.send_command(command), server]
88
+ end
89
+ end
90
+
91
+
92
+ #Error raised when unable to send command
93
+ class ClientException < Exception
94
+
95
+ end
96
+ end