rocketjob 1.3.0 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +201 -0
- data/README.md +15 -10
- data/bin/rocketjob +3 -1
- data/bin/rocketjob_perf +92 -0
- data/lib/rocket_job/cli.rb +71 -31
- data/lib/rocket_job/config.rb +21 -23
- data/lib/rocket_job/dirmon_entry.rb +63 -45
- data/lib/rocket_job/extensions/aasm.rb +56 -0
- data/lib/rocket_job/extensions/mongo.rb +23 -0
- data/lib/rocket_job/job.rb +9 -433
- data/lib/rocket_job/jobs/dirmon_job.rb +20 -20
- data/lib/rocket_job/jobs/simple_job.rb +12 -0
- data/lib/rocket_job/plugins/document.rb +69 -0
- data/lib/rocket_job/plugins/job/callbacks.rb +92 -0
- data/lib/rocket_job/plugins/job/defaults.rb +40 -0
- data/lib/rocket_job/plugins/job/logger.rb +36 -0
- data/lib/rocket_job/plugins/job/model.rb +288 -0
- data/lib/rocket_job/plugins/job/persistence.rb +167 -0
- data/lib/rocket_job/plugins/job/state_machine.rb +166 -0
- data/lib/rocket_job/plugins/job/worker.rb +167 -0
- data/lib/rocket_job/plugins/restart.rb +54 -0
- data/lib/rocket_job/plugins/singleton.rb +26 -0
- data/lib/rocket_job/plugins/state_machine.rb +105 -0
- data/lib/rocket_job/version.rb +1 -1
- data/lib/rocket_job/worker.rb +150 -119
- data/lib/rocketjob.rb +43 -21
- data/test/config_test.rb +12 -0
- data/test/dirmon_entry_test.rb +81 -85
- data/test/dirmon_job_test.rb +40 -28
- data/test/job_test.rb +14 -257
- data/test/plugins/job/callbacks_test.rb +163 -0
- data/test/plugins/job/defaults_test.rb +52 -0
- data/test/plugins/job/logger_test.rb +58 -0
- data/test/plugins/job/model_test.rb +97 -0
- data/test/plugins/job/persistence_test.rb +81 -0
- data/test/plugins/job/state_machine_test.rb +118 -0
- data/test/plugins/job/worker_test.rb +183 -0
- data/test/plugins/restart_test.rb +185 -0
- data/test/plugins/singleton_test.rb +94 -0
- data/test/plugins/state_machine_event_callbacks_test.rb +101 -0
- data/test/plugins/state_machine_test.rb +64 -0
- data/test/test_helper.rb +3 -36
- metadata +64 -19
- data/lib/rocket_job/concerns/singleton.rb +0 -33
- data/lib/rocket_job/concerns/worker.rb +0 -214
- data/test/files/_archive/archived.txt +0 -3
- data/test/job_worker_test.rb +0 -86
- data/test/jobs/test_job.rb +0 -46
- data/test/worker_test.rb +0 -97
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 766a8b76f06f83475128aa268aa2d54fc4ca7b10
|
4
|
+
data.tar.gz: 20b232869d4a43acd28fecf4417030e99c88d2dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56a8ce86eb62461128bace7cf0a8756deab0f88591f1a94f2ea951574cd47cc64e0dae0a2afdef8c6f74d2c4eb658cb00f7e0169612cbcac6d21aa68fff9b306
|
7
|
+
data.tar.gz: 74facaf88ce3dd6bc744d9074d10a50e98c1366fe8da2a070b6fe4f92daf13a3fa37696466a811f344d1c4672fd75243e64375bdd06e23a0e1685216bd858810
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright 2014, 2015, 2016 Reid Morrison
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
CHANGED
@@ -1,30 +1,35 @@
|
|
1
|
-
#
|
1
|
+
# Rocket Job
|
2
|
+
[![Gem Version](https://img.shields.io/gem/v/rocketjob.svg)](https://rubygems.org/gems/rocketjob) [![Build Status](https://travis-ci.org/rocketjob/rocketjob.svg?branch=master)](https://travis-ci.org/rocketjob/rocketjob) [![License](https://img.shields.io/badge/license-Apache%202.0-brightgreen.svg)](http://opensource.org/licenses/Apache-2.0) ![](https://img.shields.io/badge/status-Production%20Ready-blue.svg) [![Gitter chat](https://img.shields.io/badge/IRC%20(gitter)-Support-brightgreen.svg)](https://gitter.im/rocketjob/support)
|
2
3
|
|
3
|
-
|
4
|
+
Rocket Job is a distributed, priority-based, background job, computation system for Ruby.
|
5
|
+
Rocket Job makes it easy to reliably process data using jobs written in Ruby.
|
4
6
|
|
5
|
-
Outgrown existing
|
7
|
+
Outgrown existing Ruby background job processing solutions?
|
6
8
|
|
7
|
-
|
9
|
+
Upgrade to Rocket Job.
|
10
|
+
Or, start small with Rocket Job and seamlessly scale up to meet future business demands.
|
8
11
|
|
9
|
-
|
12
|
+
Checkout http://rocketjob.io/
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
Already in use in production processing large files with millions
|
14
|
-
of records, as well as large jobs to walk though large databases.
|
14
|
+
![Rocket Job](http://rocketjob.io/images/rocket/rocket-icon-512x512.png)
|
15
15
|
|
16
16
|
## Documentation
|
17
17
|
|
18
18
|
* [Guide](http://rocketjob.io/)
|
19
19
|
* [API Reference](http://www.rubydoc.info/gems/rocketjob/)
|
20
20
|
|
21
|
+
## Support
|
22
|
+
|
23
|
+
* Questions? Join the chat room on Gitter for [rocketjob support](https://gitter.im/rocketjob/support)
|
24
|
+
* [Report bugs](https://github.com/rocketjob/rocketjob/issues)
|
25
|
+
|
21
26
|
## Versioning
|
22
27
|
|
23
28
|
This project uses [Semantic Versioning](http://semver.org/).
|
24
29
|
|
25
30
|
## Author
|
26
31
|
|
27
|
-
[Reid Morrison](https://github.com/reidmorrison)
|
32
|
+
[Reid Morrison](https://github.com/reidmorrison)
|
28
33
|
|
29
34
|
## Contributors
|
30
35
|
|
data/bin/rocketjob
CHANGED
data/bin/rocketjob_perf
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'csv'
|
3
|
+
require 'yaml'
|
4
|
+
require 'rocketjob'
|
5
|
+
|
6
|
+
# Log to console
|
7
|
+
SemanticLogger.add_appender(STDOUT, &SemanticLogger::Appender::Base.colorized_formatter)
|
8
|
+
|
9
|
+
class RocketJobPerf
|
10
|
+
attr_accessor :count, :worker_processes, :worker_threads, :version, :ruby, :environment, :mongo_config
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@version = RocketJob::VERSION
|
14
|
+
@ruby = defined?(JRuby) ? "jruby_#{JRUBY_VERSION}" : "ruby_#{RUBY_VERSION}"
|
15
|
+
@count = 100_000
|
16
|
+
@worker_processes = 0
|
17
|
+
@worker_threads = 0
|
18
|
+
@environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
19
|
+
@mongo_config = 'config/mongo.yml'
|
20
|
+
end
|
21
|
+
|
22
|
+
def run_test_case(count = self.count)
|
23
|
+
self.worker_processes = RocketJob::Worker.count
|
24
|
+
raise 'Please start workers before starting the performance test' if worker_processes == 0
|
25
|
+
|
26
|
+
self.worker_processes = 0
|
27
|
+
self.worker_threads = 0
|
28
|
+
RocketJob::Worker.where(state: :running).each do |worker_process|
|
29
|
+
unless worker_process.zombie?
|
30
|
+
self.worker_processes += 1
|
31
|
+
self.worker_threads += worker_process.heartbeat.current_threads
|
32
|
+
end
|
33
|
+
end
|
34
|
+
puts "Running: #{worker_threads} workers, distributed across #{worker_processes} processes"
|
35
|
+
|
36
|
+
puts 'Waiting for workers to pause'
|
37
|
+
RocketJob::Worker.pause_all
|
38
|
+
RocketJob::Jobs::SimpleJob.delete_all
|
39
|
+
sleep 15
|
40
|
+
|
41
|
+
puts 'Enqueuing jobs'
|
42
|
+
first = RocketJob::Jobs::SimpleJob.create(priority: 1, destroy_on_complete: false)
|
43
|
+
(count - 2).times { |i| RocketJob::Jobs::SimpleJob.create! }
|
44
|
+
last = RocketJob::Jobs::SimpleJob.create(priority: 100, destroy_on_complete: false)
|
45
|
+
|
46
|
+
puts 'Resuming workers'
|
47
|
+
RocketJob::Worker.resume_all
|
48
|
+
|
49
|
+
while (!last.reload.completed?)
|
50
|
+
sleep 3
|
51
|
+
end
|
52
|
+
|
53
|
+
duration = last.reload.completed_at - first.reload.started_at
|
54
|
+
{count: count, duration: duration, jobs_per_second: (count.to_f / duration).to_i}
|
55
|
+
end
|
56
|
+
|
57
|
+
# Export the Results hash to a CSV file
|
58
|
+
def export_results(results)
|
59
|
+
CSV.open("job_results_#{ruby}_#{worker_processes}p_#{threads}t_v#{version}.csv", 'wb') do |csv|
|
60
|
+
csv << results.first.keys
|
61
|
+
results.each { |result| csv << result.values }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Parse command line options
|
66
|
+
def parse(argv)
|
67
|
+
parser = OptionParser.new do |o|
|
68
|
+
o.on('-c', '--count COUNT', 'Count of jobs to enqueue') do |arg|
|
69
|
+
self.count = arg.to_i
|
70
|
+
end
|
71
|
+
o.on('-m', '--mongo MONGO_CONFIG_FILE_NAME', 'Location of mongo.yml config file') do |arg|
|
72
|
+
self.mongo_config = arg
|
73
|
+
end
|
74
|
+
o.on('-e', '--environment ENVIRONMENT', 'The environment to run the app on (Default: RAILS_ENV || RACK_ENV || development)') do |arg|
|
75
|
+
self.environment = arg
|
76
|
+
end
|
77
|
+
end
|
78
|
+
parser.banner = 'rocketjob_perf <options>'
|
79
|
+
parser.on_tail '-h', '--help', 'Show help' do
|
80
|
+
puts parser
|
81
|
+
exit 1
|
82
|
+
end
|
83
|
+
parser.parse! argv
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
perf = RocketJobPerf.new
|
89
|
+
perf.parse(ARGV)
|
90
|
+
RocketJob::Config.load!(perf.environment, perf.mongo_config)
|
91
|
+
results = perf.run_test_case
|
92
|
+
p results
|
data/lib/rocket_job/cli.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require 'optparse'
|
2
2
|
require 'yaml'
|
3
|
+
require 'semantic_logger'
|
3
4
|
module RocketJob
|
4
5
|
# Command Line Interface parser for RocketJob
|
5
6
|
class CLI
|
6
7
|
include SemanticLogger::Loggable
|
7
|
-
attr_accessor :name, :threads, :environment, :pidfile, :directory, :quiet, :log_level
|
8
|
+
attr_accessor :name, :threads, :environment, :pidfile, :directory, :quiet, :log_level, :log_file
|
8
9
|
|
9
10
|
def initialize(argv)
|
10
11
|
@name = nil
|
@@ -14,14 +15,16 @@ module RocketJob
|
|
14
15
|
@pidfile = nil
|
15
16
|
@directory = '.'
|
16
17
|
@log_level = nil
|
18
|
+
@log_file = nil
|
17
19
|
parse(argv)
|
18
20
|
end
|
19
21
|
|
20
22
|
# Run a RocketJob::Worker from the command line
|
21
23
|
def run
|
24
|
+
Thread.current.name = 'rocketjob main'
|
22
25
|
setup_environment
|
23
26
|
setup_logger
|
24
|
-
|
27
|
+
rails? ? boot_rails : boot_standalone
|
25
28
|
write_pidfile
|
26
29
|
|
27
30
|
opts = {}
|
@@ -30,28 +33,60 @@ module RocketJob
|
|
30
33
|
Worker.run(opts)
|
31
34
|
end
|
32
35
|
|
36
|
+
def rails?
|
37
|
+
@rails ||= begin
|
38
|
+
boot_file = Pathname.new(directory).join('config/environment.rb').expand_path
|
39
|
+
boot_file.file?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
33
43
|
# Initialize the Rails environment
|
34
44
|
# Returns [true|false] whether Rails is present
|
35
45
|
def boot_rails
|
46
|
+
logger.info "Loading Rails environment: #{environment}"
|
47
|
+
|
36
48
|
boot_file = Pathname.new(directory).join('config/environment.rb').expand_path
|
37
|
-
|
49
|
+
require(boot_file.to_s)
|
50
|
+
|
51
|
+
begin
|
52
|
+
require 'rails_semantic_logger'
|
53
|
+
rescue LoadError
|
54
|
+
raise "Add the following line to your Gemfile when running rails:\n gem 'rails_semantic_logger'"
|
55
|
+
end
|
56
|
+
|
57
|
+
# Override Rails log level if command line option was supplied
|
58
|
+
SemanticLogger.default_level = log_level.to_sym if log_level
|
38
59
|
|
39
|
-
logger.info 'Booting Rails'
|
40
|
-
require boot_file.to_s
|
41
60
|
if Rails.configuration.eager_load
|
42
61
|
RocketJob::Worker.logger.benchmark_info('Eager loaded Rails and all Engines') do
|
43
62
|
Rails.application.eager_load!
|
44
63
|
Rails::Engine.subclasses.each(&:eager_load!)
|
45
64
|
end
|
46
65
|
end
|
47
|
-
|
48
|
-
self.class.load_config(Rails.env)
|
49
|
-
true
|
50
66
|
end
|
51
67
|
|
68
|
+
# In a standalone environment, explicitly load config files
|
52
69
|
def boot_standalone
|
53
|
-
|
54
|
-
|
70
|
+
# Try to load bundler if present
|
71
|
+
begin
|
72
|
+
require 'bundler/setup'
|
73
|
+
Bundler.require(environment)
|
74
|
+
rescue LoadError
|
75
|
+
end
|
76
|
+
|
77
|
+
require 'rocketjob'
|
78
|
+
begin
|
79
|
+
require 'rocketjob_pro'
|
80
|
+
rescue LoadError
|
81
|
+
end
|
82
|
+
|
83
|
+
# Log to file except when booting rails, when it will add the log file path
|
84
|
+
path = log_file ? Pathname.new(log_file) : Pathname.pwd.join("log/#{environment}.log")
|
85
|
+
path.dirname.mkpath
|
86
|
+
SemanticLogger.add_appender(path.to_s, &SemanticLogger::Appender::Base.colorized_formatter)
|
87
|
+
|
88
|
+
logger.info "Rails not detected. Running standalone: #{environment}"
|
89
|
+
RocketJob::Config.load!(environment)
|
55
90
|
self.class.eager_load_jobs
|
56
91
|
end
|
57
92
|
|
@@ -79,21 +114,9 @@ module RocketJob
|
|
79
114
|
def setup_logger
|
80
115
|
SemanticLogger.add_appender(STDOUT, &SemanticLogger::Appender::Base.colorized_formatter) unless quiet
|
81
116
|
SemanticLogger.default_level = log_level.to_sym if log_level
|
82
|
-
end
|
83
117
|
|
84
|
-
|
85
|
-
|
86
|
-
return false if MongoMapper.config
|
87
|
-
|
88
|
-
config_file = file_name ? Pathname.new(file_name) : Pathname.pwd.join('config/mongo.yml')
|
89
|
-
if config_file.file?
|
90
|
-
config = YAML.load(ERB.new(config_file.read).result)
|
91
|
-
log = SemanticLogger::DebugAsTraceLogger.new('Mongo')
|
92
|
-
MongoMapper.setup(config, environment, logger: log)
|
93
|
-
true
|
94
|
-
else
|
95
|
-
raise(ArgumentError, "Mongo Configuration file: #{config_file.to_s} not found")
|
96
|
-
end
|
118
|
+
# Enable SemanticLogger signal handling for this process
|
119
|
+
SemanticLogger.add_signal_handler
|
97
120
|
end
|
98
121
|
|
99
122
|
# Eager load files in jobs folder
|
@@ -108,13 +131,30 @@ module RocketJob
|
|
108
131
|
# Parse command line options placing results in the corresponding instance variables
|
109
132
|
def parse(argv)
|
110
133
|
parser = OptionParser.new do |o|
|
111
|
-
o.on('-n', '--name NAME', 'Unique Name of this worker instance (Default: hostname:PID)')
|
112
|
-
|
113
|
-
|
114
|
-
o.on('-
|
115
|
-
|
116
|
-
|
117
|
-
o.on('
|
134
|
+
o.on('-n', '--name NAME', 'Unique Name of this worker instance (Default: hostname:PID)') do |arg|
|
135
|
+
@name = arg
|
136
|
+
end
|
137
|
+
o.on('-t', '--threads COUNT', 'Number of worker threads to start') do |arg|
|
138
|
+
@threads = arg.to_i
|
139
|
+
end
|
140
|
+
o.on('-q', '--quiet', 'Do not write to stdout, only to logfile. Necessary when running as a daemon') do
|
141
|
+
@quiet = true
|
142
|
+
end
|
143
|
+
o.on('-d', '--dir DIR', 'Directory containing Rails app, if not current directory') do |arg|
|
144
|
+
@directory = arg
|
145
|
+
end
|
146
|
+
o.on('-e', '--environment ENVIRONMENT', 'The environment to run the app on (Default: RAILS_ENV || RACK_ENV || development)') do |arg|
|
147
|
+
@environment = arg
|
148
|
+
end
|
149
|
+
o.on('-l', '--log_level trace|debug|info|warn|error|fatal', 'The log level to use') do |arg|
|
150
|
+
@log_level = arg
|
151
|
+
end
|
152
|
+
o.on('-f', '--log_file FILE_NAME', 'The log file to write to. Default: log/<environment>.log') do |arg|
|
153
|
+
@log_file = arg
|
154
|
+
end
|
155
|
+
o.on('--pidfile PATH', 'Use PATH as a pidfile') do |arg|
|
156
|
+
@pidfile = arg
|
157
|
+
end
|
118
158
|
o.on('-v', '--version', 'Print the version information') do
|
119
159
|
puts "Rocket Job v#{RocketJob::VERSION}"
|
120
160
|
exit 1
|
data/lib/rocket_job/config.rb
CHANGED
@@ -2,10 +2,7 @@
|
|
2
2
|
module RocketJob
|
3
3
|
# Centralized Configuration for Rocket Jobs
|
4
4
|
class Config
|
5
|
-
include
|
6
|
-
|
7
|
-
# Prevent data in MongoDB from re-defining the model behavior
|
8
|
-
#self.static_keys = true
|
5
|
+
include Plugins::Document
|
9
6
|
|
10
7
|
# Returns the single instance of the Rocket Job Configuration for this site
|
11
8
|
# in a thread-safe way
|
@@ -18,7 +15,8 @@ module RocketJob
|
|
18
15
|
end
|
19
16
|
end
|
20
17
|
|
21
|
-
#
|
18
|
+
# Useful for Testing, not recommended elsewhere
|
19
|
+
# By enabling inline_mode jobs will be called in-line using perform_now
|
22
20
|
# No worker processes will be created, nor threads created
|
23
21
|
cattr_accessor(:inline_mode) { false }
|
24
22
|
|
@@ -43,9 +41,6 @@ module RocketJob
|
|
43
41
|
# Not all job types support pausing in the middle
|
44
42
|
key :re_check_seconds, Integer, default: 60
|
45
43
|
|
46
|
-
# Limit the number of workers per job class per worker
|
47
|
-
# 'class_name' / group => 100
|
48
|
-
#key :limits, Hash
|
49
44
|
# @formatter:on
|
50
45
|
|
51
46
|
# Replace the MongoMapper default mongo connection for holding jobs
|
@@ -64,22 +59,25 @@ module RocketJob
|
|
64
59
|
DirmonEntry.set_database_name(db_name)
|
65
60
|
end
|
66
61
|
|
67
|
-
#
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
@@mongo_work_connection = connection
|
78
|
-
end
|
62
|
+
# Configure MongoMapper
|
63
|
+
def self.load!(environment='development', file_name=nil, encryption_file_name=nil)
|
64
|
+
config_file = file_name ? Pathname.new(file_name) : Pathname.pwd.join('config/mongo.yml')
|
65
|
+
if config_file.file?
|
66
|
+
logger.debug "Reading MongoDB configuration from: #{config_file}"
|
67
|
+
config = YAML.load(ERB.new(config_file.read).result)
|
68
|
+
MongoMapper.setup(config, environment)
|
69
|
+
else
|
70
|
+
raise(ArgumentError, "Mongo Configuration file: #{config_file.to_s} not found")
|
71
|
+
end
|
79
72
|
|
80
|
-
|
81
|
-
|
82
|
-
|
73
|
+
# Load Encryption configuration file if present
|
74
|
+
if defined?(SymmetricEncryption)
|
75
|
+
config_file = encryption_file_name ? Pathname.new(encryption_file_name) : Pathname.pwd.join('config/symmetric-encryption.yml')
|
76
|
+
if config_file.file?
|
77
|
+
logger.debug "Reading SymmetricEncryption configuration from: #{config_file}"
|
78
|
+
SymmetricEncryption.load!(config_file.to_s, environment)
|
79
|
+
end
|
80
|
+
end
|
83
81
|
end
|
84
82
|
|
85
83
|
end
|