Skip to content

Commit a3aec71

Browse files
author
Joe Blackman
committed
Fixed legacy purge worker. Changed conversion worker to run only once (i.e. not retry errors) and to always send the callback if defined. Changed callback to POST variables, rather than sending JSON
1 parent 5ebfa65 commit a3aec71

File tree

8 files changed

+98
-16
lines changed

8 files changed

+98
-16
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ gem 'rspec'
1818
gem 'rack-test'
1919
gem 'simplecov'
2020
gem 'byebug'
21+
gem 'timecop'
2122
#gem 'api-config', git: 'http://code.ifad.org/api-config'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ GEM
7676
eventmachine (~> 1.0)
7777
rack (~> 1.0)
7878
tilt (1.4.1)
79+
timecop (0.7.1)
7980
timers (4.0.1)
8081
hitimes
8182
unicorn (4.3.1)
@@ -103,5 +104,6 @@ DEPENDENCIES
103104
simplecov
104105
sinatra
105106
thin
107+
timecop
106108
unicorn
107109
wkhtmltopdf

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,17 @@ Response:
271271
Content-Type: application/pdf; charset=binary
272272
273273
... PDF document body ...
274+
275+
## Callbacks
276+
277+
When a document conversion is completed, an attempt will be made to POST a callback to the URL specified when the conversion was attempted. The callback will be a normal form post, sending these values:
278+
279+
status - the result of the conversion, 200 for success, 400+ for failure
280+
description - the outcome of the conversion, e.g. "Document converted"
281+
app - the application name
282+
doc_id - the ID of the document
283+
version - the version of the document that was converted
284+
action - the conversion action performed
285+
path - a path to the converted file (you will have to tack the Colore URL base onto this)
286+
287+

lib/sidekiq_workers.rb

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Sidekiq workers for the Colore system.
33
#
44
#
5+
require 'rest_client'
56
require 'sidekiq'
67
require 'sidetiq'
78

@@ -10,7 +11,7 @@ module Sidekiq
1011
# This worker converts a document file to a new format and stores it.
1112
class ConversionWorker
1213
include ::Sidekiq::Worker
13-
sidekiq_options queue: :colore, retry: 5, backtrace: true
14+
sidekiq_options queue: :colore, retry: false
1415

1516
# Converts a document file to a new format. The converted file will be stored in
1617
# the document version directory. If the callback_url is specified, the [CallbackWorker]
@@ -23,9 +24,18 @@ class ConversionWorker
2324
def perform doc_key_str, version, filename, action, callback_url=nil
2425
doc_key = DocKey.parse doc_key_str
2526
new_filename = Converter.new.convert doc_key, version, filename, action
26-
CallbackWorker.perform_async doc_key_str, version, action, new_filename, callback_url if callback_url
27+
status = 200
28+
message = "Document converted"
2729
rescue Heathen::TaskNotFound => e
2830
logger.warn "#{e.message}, will not attempt to re-process this request"
31+
status = 400
32+
message = e.message
33+
rescue StandardError => e
34+
logger.warn "#{e.message}, will not attempt to re-process this request"
35+
status = 500
36+
message = e.message
37+
ensure
38+
CallbackWorker.perform_async doc_key_str, version, action, new_filename, callback_url, status, message if callback_url
2939
end
3040
end
3141

@@ -39,20 +49,22 @@ class CallbackWorker
3949
# @param version [String] the file version
4050
# @param action [String] the conversion to perform
4151
# @param filename [String] the converted file name
42-
# @param callback_url [Stringoptional callback URL
43-
def perform doc_key_str, version, action, new_filename, callback_url
52+
# @param callback_url [String] callback URL
53+
# @param status [Integer) status code to send in callback
54+
# @param message [String] description text to send in callback
55+
def perform doc_key_str, version, action, new_filename, callback_url, status, description
4456
doc_key = DocKey.parse doc_key_str
4557
doc = Document.load C_.storage_directory, doc_key
4658
rsp_hash = {
47-
status: 200,
48-
description: "Document converted",
59+
status: status.to_i,
60+
description: description,
4961
app: doc_key.app,
5062
doc_id: doc_key.doc_id,
5163
version: version,
5264
action: action,
53-
path: doc.file_path(version,new_filename),
65+
path: (doc.file_path(version,new_filename) if status < 300),
5466
}
55-
RestClient.post callback_url, JSON.pretty_generate(rsp_hash), content_type: :json
67+
RestClient.post callback_url, rsp_hash
5668
end
5769
end
5870

@@ -69,11 +81,12 @@ class LegacyPurgeWorker
6981

7082
# Looks for old legacy docs and deletes them
7183
def perform
72-
purge_seconds = (C_.legacy_purge_days || 1).to_i * 86400.0
73-
LegacyConverter.new.legacy_dir.each_entry do |file|
74-
next if file.directory?
75-
if Time.now - file.ctime > purge_seconds
76-
file.unlink
84+
purge_seconds = (C_.legacy_purge_days || 1).to_f * 86400.0
85+
dir = LegacyConverter.new.legacy_dir
86+
dir.each_entry do |file|
87+
next if (dir+file).directory?
88+
if Time.now - (dir+file).ctime > purge_seconds
89+
(dir+file).unlink
7790
logger.debug "Deleted old legacy file: #{file}"
7891
end
7992
end

spec/helpers/storage.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def setup_storage
88
FileUtils.rm_rf tmp_storage_dir
99
FileUtils.mkdir_p tmp_storage_dir
1010
FileUtils.cp_r fixture('app'), tmp_storage_dir
11+
FileUtils.cp_r fixture('legacy'), tmp_storage_dir
1112
end
1213

1314
def delete_storage

spec/integration/callback.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env ruby
2+
#
3+
# Simple sinatra app to receive and display callbacks
4+
#
5+
require 'pp'
6+
require 'sinatra'
7+
8+
set :port, 9230
9+
10+
post '/callback' do
11+
puts "Received callback"
12+
pp params
13+
end

spec/lib/sidekiq_workers_spec.rb

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919

2020
it 'gives up on Heathen::TaskNotFound' do
2121
allow(@mock_converter).to receive(:convert) { raise Heathen::TaskNotFound.new('foo','bar') }
22-
expect(Colore::Sidekiq::CallbackWorker).not_to receive(:perform_async)
22+
expect(Colore::Sidekiq::CallbackWorker).to receive(:perform_async) {}
23+
described_class.new.perform doc_key.to_s, 'current', 'arglebargle.docx', 'pdf', callback_url
24+
end
25+
26+
it 'gives up on other errors' do
27+
allow(@mock_converter).to receive(:convert) { raise 'arglebargle' }
28+
expect(Colore::Sidekiq::CallbackWorker).to receive(:perform_async) {}
2329
described_class.new.perform doc_key.to_s, 'current', 'arglebargle.docx', 'pdf', callback_url
2430
end
2531
end
@@ -37,8 +43,39 @@
3743
end
3844
context '#perform' do
3945
it 'runs' do
40-
expect(RestClient).to receive(:post).with(callback_url,String,Hash)
41-
described_class.new.perform doc_key.to_s, 'current', 'arglebargle.docx', 'pdf', callback_url
46+
expect(RestClient).to receive(:post).with(callback_url,Hash)
47+
described_class.new.perform doc_key.to_s, 'current', 'arglebargle.docx', 'pdf', callback_url, 250, 'foobar'
48+
end
49+
end
50+
end
51+
52+
describe Colore::Sidekiq::LegacyPurgeWorker do
53+
before do
54+
setup_storage
55+
allow(Colore::C_).to receive(:storage_directory) { tmp_storage_dir }
56+
allow(Colore::C_).to receive(:legacy_purge_days) { 2 }
57+
end
58+
after do
59+
delete_storage
60+
end
61+
context '#perform' do
62+
it 'runs' do
63+
dir = Colore::LegacyConverter.new.legacy_dir
64+
file1 = dir + 'file1.tiff'
65+
file2 = dir + 'file2.tiff'
66+
file1.open('w') { |f| f.write 'foobar' }
67+
file2.open('w') { |f| f.write 'foobar' }
68+
described_class.new.perform
69+
expect(file1.file?).to eq true
70+
expect(file2.file?).to eq true
71+
Timecop.freeze(Date.today + 1)
72+
described_class.new.perform
73+
expect(file1.file?).to eq true
74+
expect(file2.file?).to eq true
75+
Timecop.freeze(Date.today + 3)
76+
described_class.new.perform
77+
expect(file1.file?).to eq false
78+
expect(file2.file?).to eq false
4279
end
4380
end
4481
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'byebug'
55
require 'rack/test'
66
require 'simplecov'
7+
require 'timecop'
78

89
SimpleCov.start
910

0 commit comments

Comments
 (0)