rocketjob 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rocket_job/dirmon_entry.rb +21 -8
- data/lib/rocket_job/version.rb +1 -1
- data/test/dirmon_entry_test.rb +81 -21
- data/test/dirmon_job_test.rb +4 -3
- data/test/files/_archive/archived.txt +3 -0
- data/test/files/text.txt +3 -0
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 201f922d37f2533e56109c053e3c9f4bdef30108
|
4
|
+
data.tar.gz: 00598e8fb3039d7ce629f629bf270d3cc06f2e87
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 447f63cf687c4e7ac10189aff59730874ea25f3e57b7d6ca301fd4a34178e91d68d4428f5be095c2a6a4a5426d603262c3d82c78e9f3988a9ed35d76bf75bddf
|
7
|
+
data.tar.gz: e6a54a1b72cb2c7192894140eea4e49e42dc29049a85a77338de773e539c23a5e1ed09a5691a0a5c238a881e99398cb07b99eddf5abdda8e044971cf433766f3
|
@@ -213,30 +213,38 @@ module RocketJob
|
|
213
213
|
@@default_archive_directory = '_archive'.freeze
|
214
214
|
|
215
215
|
# Returns [Pathname] the archive_directory if set, otherwise the default_archive_directory
|
216
|
-
|
217
|
-
|
216
|
+
# Creates the archive directory if one is set
|
217
|
+
def archive_pathname(file_pathname)
|
218
|
+
if archive_directory
|
219
|
+
path = Pathname.new(archive_directory)
|
220
|
+
path.mkpath unless path.exist?
|
221
|
+
path.realpath
|
222
|
+
else
|
223
|
+
file_pathname.dirname.join(self.class.default_archive_directory).realdirpath
|
224
|
+
end
|
218
225
|
end
|
219
226
|
|
220
227
|
# Passes each filename [Pathname] found that matches the pattern into the supplied block
|
221
228
|
def each(&block)
|
222
229
|
logger.tagged("DirmonEntry:#{id}") do
|
223
|
-
|
230
|
+
# Case insensitive filename matching
|
231
|
+
Pathname.glob(pattern, File::FNM_CASEFOLD).each do |pathname|
|
224
232
|
next if pathname.directory?
|
225
233
|
pathname = pathname.realpath
|
226
234
|
file_name = pathname.to_s
|
227
235
|
|
228
236
|
# Skip archive directories
|
229
|
-
next if file_name.
|
237
|
+
next if file_name.include?(self.class.default_archive_directory)
|
230
238
|
|
231
239
|
# Security check?
|
232
|
-
if (
|
233
|
-
logger.
|
240
|
+
if (whitelist_paths.size > 0) && whitelist_paths.none? { |whitepath| file_name.start_with?(whitepath) }
|
241
|
+
logger.error "Skipping file: #{file_name} since it is not in any of the whitelisted paths: #{whitelist_paths.join(', ')}"
|
234
242
|
next
|
235
243
|
end
|
236
244
|
|
237
245
|
# File must be writable so it can be removed after processing
|
238
246
|
unless pathname.writable?
|
239
|
-
logger.
|
247
|
+
logger.error "Skipping file: #{file_name} since it is not writable by the current user. Must be able to delete/move the file after queueing the job"
|
240
248
|
next
|
241
249
|
end
|
242
250
|
block.call(pathname)
|
@@ -283,6 +291,11 @@ module RocketJob
|
|
283
291
|
|
284
292
|
protected
|
285
293
|
|
294
|
+
# Instance method to return whitelist paths
|
295
|
+
def whitelist_paths
|
296
|
+
@@whitelist_paths
|
297
|
+
end
|
298
|
+
|
286
299
|
# Upload the file to the job
|
287
300
|
def upload_file(job, pathname)
|
288
301
|
if job.respond_to?(:file_store_upload)
|
@@ -316,7 +329,7 @@ module RocketJob
|
|
316
329
|
# Note:
|
317
330
|
# - Works across partitions when the file and the archive are on different partitions
|
318
331
|
def archive_file(job, pathname)
|
319
|
-
target_path = archive_pathname
|
332
|
+
target_path = archive_pathname(pathname)
|
320
333
|
target_path.mkpath
|
321
334
|
target_file_name = target_path.join("#{job.id}_#{pathname.basename}")
|
322
335
|
# In case the file is being moved across partitions
|
data/lib/rocket_job/version.rb
CHANGED
data/test/dirmon_entry_test.rb
CHANGED
@@ -63,7 +63,7 @@ class DirmonEntryTest < Minitest::Test
|
|
63
63
|
|
64
64
|
describe '#fail_with_exception!' do
|
65
65
|
before do
|
66
|
-
@dirmon_entry = RocketJob::DirmonEntry.new(job_class_name: 'Jobs::TestJob', pattern: '/
|
66
|
+
@dirmon_entry = RocketJob::DirmonEntry.new(job_class_name: 'Jobs::TestJob', pattern: 'test/files/**', arguments: [1])
|
67
67
|
@dirmon_entry.enable!
|
68
68
|
end
|
69
69
|
after do
|
@@ -111,7 +111,7 @@ class DirmonEntryTest < Minitest::Test
|
|
111
111
|
|
112
112
|
describe 'job_class_name' do
|
113
113
|
it 'ensure presence' do
|
114
|
-
assert entry = RocketJob::DirmonEntry.new(pattern: '/
|
114
|
+
assert entry = RocketJob::DirmonEntry.new(pattern: 'test/files/**')
|
115
115
|
assert_equal false, entry.valid?
|
116
116
|
assert_equal ["can't be blank", 'job_class_name must be defined and must be derived from RocketJob::Job'], entry.errors[:job_class_name], entry.errors.inspect
|
117
117
|
end
|
@@ -121,7 +121,7 @@ class DirmonEntryTest < Minitest::Test
|
|
121
121
|
it 'allow no arguments' do
|
122
122
|
assert entry = RocketJob::DirmonEntry.new(
|
123
123
|
job_class_name: 'Jobs::TestJob',
|
124
|
-
pattern: '/
|
124
|
+
pattern: 'test/files/**',
|
125
125
|
perform_method: :result
|
126
126
|
)
|
127
127
|
assert_equal true, entry.valid?, entry.errors.inspect
|
@@ -131,7 +131,7 @@ class DirmonEntryTest < Minitest::Test
|
|
131
131
|
it 'ensure correct number of arguments' do
|
132
132
|
assert entry = RocketJob::DirmonEntry.new(
|
133
133
|
job_class_name: 'Jobs::TestJob',
|
134
|
-
pattern: '/
|
134
|
+
pattern: 'test/files/**'
|
135
135
|
)
|
136
136
|
assert_equal false, entry.valid?
|
137
137
|
assert_equal ['There must be 1 argument(s)'], entry.errors[:arguments], entry.errors.inspect
|
@@ -140,7 +140,7 @@ class DirmonEntryTest < Minitest::Test
|
|
140
140
|
it 'return false if the job name is bad' do
|
141
141
|
assert entry = RocketJob::DirmonEntry.new(
|
142
142
|
job_class_name: 'Jobs::Tests::Names::Things',
|
143
|
-
pattern: '/
|
143
|
+
pattern: 'test/files/**'
|
144
144
|
)
|
145
145
|
assert_equal false, entry.valid?
|
146
146
|
assert_equal [], entry.errors[:arguments], entry.errors.inspect
|
@@ -150,7 +150,7 @@ class DirmonEntryTest < Minitest::Test
|
|
150
150
|
it 'arguments with perform_method' do
|
151
151
|
assert entry = RocketJob::DirmonEntry.new(
|
152
152
|
job_class_name: 'Jobs::TestJob',
|
153
|
-
pattern: '/
|
153
|
+
pattern: 'test/files/**',
|
154
154
|
perform_method: :sum
|
155
155
|
)
|
156
156
|
assert_equal false, entry.valid?
|
@@ -160,7 +160,7 @@ class DirmonEntryTest < Minitest::Test
|
|
160
160
|
it 'valid' do
|
161
161
|
assert entry = RocketJob::DirmonEntry.new(
|
162
162
|
job_class_name: 'Jobs::TestJob',
|
163
|
-
pattern: '/
|
163
|
+
pattern: 'test/files/**',
|
164
164
|
arguments: [1]
|
165
165
|
)
|
166
166
|
assert entry.valid?, entry.errors.inspect
|
@@ -169,7 +169,7 @@ class DirmonEntryTest < Minitest::Test
|
|
169
169
|
it 'valid with perform_method' do
|
170
170
|
assert entry = RocketJob::DirmonEntry.new(
|
171
171
|
job_class_name: 'Jobs::TestJob',
|
172
|
-
pattern: '/
|
172
|
+
pattern: 'test/files/**',
|
173
173
|
perform_method: :sum,
|
174
174
|
arguments: [1, 2]
|
175
175
|
)
|
@@ -180,20 +180,23 @@ class DirmonEntryTest < Minitest::Test
|
|
180
180
|
describe 'with valid entry' do
|
181
181
|
before do
|
182
182
|
@archive_directory = '/tmp/archive_directory'
|
183
|
-
@
|
184
|
-
|
183
|
+
@archive_path = Pathname.new(@archive_directory)
|
184
|
+
@archive_path.mkpath
|
185
|
+
@archive_path = @archive_path.realdirpath
|
186
|
+
@entry = RocketJob::DirmonEntry.new(
|
187
|
+
pattern: 'test/files/**/*',
|
185
188
|
job_class_name: 'Jobs::TestJob',
|
186
189
|
arguments: [{input: 'yes'}],
|
187
190
|
properties: {priority: 23, perform_method: :event},
|
188
191
|
archive_directory: @archive_directory
|
189
192
|
)
|
190
|
-
@job
|
191
|
-
@file
|
192
|
-
@file_name
|
193
|
-
@pathname
|
193
|
+
@job = Jobs::TestJob.new
|
194
|
+
@file = Tempfile.new('archive')
|
195
|
+
@file_name = @file.path
|
196
|
+
@pathname = Pathname.new(@file_name)
|
194
197
|
File.open(@file_name, 'w') { |file| file.write('Hello World') }
|
195
198
|
assert File.exists?(@file_name)
|
196
|
-
@
|
199
|
+
@archive_real_name = @archive_path.join("#{@job.id}_#{File.basename(@file_name)}").to_s
|
197
200
|
end
|
198
201
|
|
199
202
|
after do
|
@@ -202,26 +205,26 @@ class DirmonEntryTest < Minitest::Test
|
|
202
205
|
|
203
206
|
describe '#archive_pathname' do
|
204
207
|
it 'with archive directory' do
|
205
|
-
assert_equal @
|
208
|
+
assert_equal File.dirname(@archive_real_name), @entry.archive_pathname(@pathname).to_s
|
206
209
|
end
|
207
210
|
|
208
211
|
it 'without archive directory' do
|
209
212
|
@entry.archive_directory = nil
|
210
|
-
|
213
|
+
assert @entry.archive_pathname(@pathname).to_s.end_with?('_archive')
|
211
214
|
end
|
212
215
|
end
|
213
216
|
|
214
217
|
describe '#archive_file' do
|
215
218
|
it 'archive file' do
|
216
|
-
assert_equal @
|
217
|
-
assert File.exists?(@
|
219
|
+
assert_equal @archive_real_name, @entry.send(:archive_file, @job, Pathname.new(@file_name))
|
220
|
+
assert File.exists?(@archive_real_name)
|
218
221
|
end
|
219
222
|
end
|
220
223
|
|
221
224
|
describe '#upload_default' do
|
222
225
|
it 'upload' do
|
223
226
|
@entry.send(:upload_default, @job, @pathname)
|
224
|
-
assert_equal
|
227
|
+
assert_equal @archive_real_name, @job.arguments.first[:full_file_name], @job.arguments
|
225
228
|
end
|
226
229
|
end
|
227
230
|
|
@@ -248,10 +251,67 @@ class DirmonEntryTest < Minitest::Test
|
|
248
251
|
@entry.arguments = [{}]
|
249
252
|
@entry.perform_method = :event
|
250
253
|
job = @entry.later(@pathname)
|
251
|
-
assert_equal
|
254
|
+
assert_equal Pathname.new(@archive_directory).join("#{job.id}_#{File.basename(@file_name)}").realdirpath.to_s, job.arguments.first[:full_file_name]
|
252
255
|
assert job.queued?
|
253
256
|
end
|
254
257
|
end
|
258
|
+
|
259
|
+
describe '#each' do
|
260
|
+
it 'without archive path' do
|
261
|
+
@entry.archive_directory = nil
|
262
|
+
files = []
|
263
|
+
@entry.each do |file_name|
|
264
|
+
files << file_name
|
265
|
+
end
|
266
|
+
assert_equal nil, @entry.archive_directory
|
267
|
+
assert_equal 1, files.count
|
268
|
+
assert_equal Pathname.new('test/files/text.txt').realpath, files.first
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'with archive path' do
|
272
|
+
files = []
|
273
|
+
@entry.each do |file_name|
|
274
|
+
files << file_name
|
275
|
+
end
|
276
|
+
assert_equal 1, files.count
|
277
|
+
assert_equal Pathname.new('test/files/text.txt').realpath, files.first
|
278
|
+
end
|
279
|
+
|
280
|
+
it 'with case-insensitive pattern' do
|
281
|
+
@entry.pattern = 'test/files/**/*.TxT'
|
282
|
+
files = []
|
283
|
+
@entry.each do |file_name|
|
284
|
+
files << file_name
|
285
|
+
end
|
286
|
+
assert_equal 1, files.count
|
287
|
+
assert_equal Pathname.new('test/files/text.txt').realpath, files.first
|
288
|
+
end
|
289
|
+
|
290
|
+
it 'reads paths inside of the whitelist' do
|
291
|
+
@entry.archive_directory = nil
|
292
|
+
files = []
|
293
|
+
@entry.stub(:whitelist_paths, [Pathname.new('test/files').realpath.to_s]) do
|
294
|
+
@entry.each do |file_name|
|
295
|
+
files << file_name
|
296
|
+
end
|
297
|
+
end
|
298
|
+
assert_equal nil, @entry.archive_directory
|
299
|
+
assert_equal 1, files.count
|
300
|
+
assert_equal Pathname.new('test/files/text.txt').realpath, files.first
|
301
|
+
end
|
302
|
+
|
303
|
+
it 'skips paths outside of the whitelist' do
|
304
|
+
@entry.archive_directory = nil
|
305
|
+
files = []
|
306
|
+
@entry.stub(:whitelist_paths, [Pathname.new('test/config').realpath.to_s]) do
|
307
|
+
@entry.each do |file_name|
|
308
|
+
files << file_name
|
309
|
+
end
|
310
|
+
end
|
311
|
+
assert_equal nil, @entry.archive_directory
|
312
|
+
assert_equal 0, files.count
|
313
|
+
end
|
314
|
+
end
|
255
315
|
end
|
256
316
|
|
257
317
|
end
|
data/test/dirmon_job_test.rb
CHANGED
@@ -118,11 +118,12 @@ class DirmonJobTest < Minitest::Test
|
|
118
118
|
it 'skip files in archive directory' do
|
119
119
|
@entry.archive_directory = nil
|
120
120
|
@entry.pattern = "#{@directory}/abc/**/*"
|
121
|
+
file_pathname = Pathname.new("#{@directory}/abc/file1")
|
121
122
|
|
122
|
-
create_file(
|
123
|
+
create_file(file_pathname.to_s, 5)
|
123
124
|
create_file("#{@directory}/abc/file2", 10)
|
124
|
-
FileUtils.makedirs(
|
125
|
-
create_file("#{@
|
125
|
+
FileUtils.makedirs(@entry.archive_pathname(file_pathname))
|
126
|
+
create_file("#{@entry.archive_pathname(file_pathname)}/file3", 10)
|
126
127
|
|
127
128
|
result = @dirmon_job.send(:check_directories, {})
|
128
129
|
|
data/test/files/text.txt
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rocketjob
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reid Morrison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aasm
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 4.2.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 4.2.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: semantic_logger
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -135,6 +135,8 @@ files:
|
|
135
135
|
- test/config/mongo.yml
|
136
136
|
- test/dirmon_entry_test.rb
|
137
137
|
- test/dirmon_job_test.rb
|
138
|
+
- test/files/_archive/archived.txt
|
139
|
+
- test/files/text.txt
|
138
140
|
- test/job_test.rb
|
139
141
|
- test/job_worker_test.rb
|
140
142
|
- test/jobs/test_job.rb
|
@@ -168,6 +170,8 @@ test_files:
|
|
168
170
|
- test/config/mongo.yml
|
169
171
|
- test/dirmon_entry_test.rb
|
170
172
|
- test/dirmon_job_test.rb
|
173
|
+
- test/files/_archive/archived.txt
|
174
|
+
- test/files/text.txt
|
171
175
|
- test/job_test.rb
|
172
176
|
- test/job_worker_test.rb
|
173
177
|
- test/jobs/test_job.rb
|