Skip to content

Commit

Permalink
Improve logging/logfile handling from the Watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
danschmidt5189 committed Dec 20, 2023
1 parent 84ca767 commit 8adf605
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
42 changes: 21 additions & 21 deletions lib/gingr/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,37 @@ def listener
@listener ||= begin
Listen.to(ready_dir, only: WATCH_FILTER, force_polling: true) do |_, added, _|
added.each do |zipfile|
logger.info("Processing zipfile: #{zipfile}")

begin
exec_gingr_all!(zipfile)
rescue => e
logger.error("Error processing #{zipfile}, moving to #{failed_dir}: #{e.inspect}")
logger.debug("Continuing to watch despite error: #{e}")
end
end
end
end
end

def exec_gingr_all!(zipfile)
begin
command = ['gingr', 'all', zipfile, *arguments]
logger.debug("Running command: #{command}")
command = ['gingr', 'all', zipfile, *arguments]

stdout, stderr, status = Open3.capture3(*command)
if !status.success?
raise SubprocessError, "Call to `gingr all` failed: #{status}"
end
logger.info("Processing zipfile: #{zipfile}")
logger.debug("Running command: #{command}")
logs, status = Open3.capture2e(*command)

logger.debug("Processed #{zipfile}, moving to #{processed_dir}")
FileUtils.mv(zipfile, processed_dir)
rescue => e
if !status.success?
logger.error("Error processing #{zipfile}, moving to #{processed_dir}")
logger.debug("Execute logs: #{logs}")

write_logs(logs, zipfile, failed_dir)
FileUtils.mv(zipfile, failed_dir)
File.write(error_log_for(zipfile), collate_logs(stdout, stderr))
raise

raise SubprocessError, "Call to `gingr all` failed: #{status}"
else
logger.info("Processed #{zipfile}, moving to #{processed_dir}")
logger.debug("Execute logs: #{logs}")

write_logs(logs, zipfile, processed_dir)
FileUtils.mv(zipfile, processed_dir)
end
end

Expand All @@ -106,12 +109,9 @@ def dasherize(str)
(str.length > 1 ? "--" : "-") + str.tr("_", "-")
end

def collate_logs(stdout, stderr)
"#{stdout}\n#{stderr}\n"
end

def error_log_for(zipfile)
File.join(failed_dir, "#{File.basename(zipfile, '.*')}.log")
def write_logs(logs, zipfile, logdir)
logfile = File.join(logdir, "#{File.basename(zipfile, '.*')}.log")
File.write(logfile, logs)
end

def validate_directories!
Expand Down
12 changes: 6 additions & 6 deletions spec/watcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ def success?
end

it 'passes arguments to `gingr all`' do
expect(Open3).to receive(:capture3).with(*%w(
expect(Open3).to receive(:capture2e).with(*%w(
gingr all /opt/app/data/gingr/ready/vector.zip
--geoserver-root /opt/app/data/geoserver
--geoserver-secure-url http://admin:geoserver@geoserver-secure:8080/geoserver/rest/
--geoserver-url http://admin:geoserver@geoserver:8080/geoserver/rest/
--solr-url http://solr:8983/solr/geodata-test
--spatial-root /opt/app/data/spatial
--update-reference-field true
)).and_return(['', '', MockStatus.successful])
)).and_return(['', MockStatus.successful])

copy_zipfile_to_ready('vector.zip')
watcher.exec_gingr_all!('/opt/app/data/gingr/ready/vector.zip')
end
end

it 'moves successfully processed files to the processed directory' do
expect(Open3).to receive(:capture3).and_return(['', '', MockStatus.successful])
expect(Open3).to receive(:capture2e).and_return(['', MockStatus.successful])

copy_zipfile_to_ready('vector.zip')
watcher.exec_gingr_all!('/opt/app/data/gingr/ready/vector.zip')
Expand All @@ -84,7 +84,7 @@ def success?
watcher.start

(1..4).each do |i|
exp = expect(Open3).to receive(:capture3).with(*%W(
exp = expect(Open3).to receive(:capture2e).with(*%W(
gingr all /opt/app/data/gingr/ready/vector#{i}.zip
--geoserver-root /opt/app/data/geoserver
--geoserver-secure-url http://admin:geoserver@geoserver-secure:8080/geoserver/rest/
Expand All @@ -97,7 +97,7 @@ def success?
if i.odd?
exp.and_raise(Gingr::Watcher::SubprocessError)
else
exp.and_return(['', '', MockStatus.successful])
exp.and_return(['', MockStatus.successful])
end

copy_zipfile_to_ready('vector.zip', "vector#{i}.zip")
Expand All @@ -110,7 +110,7 @@ def success?
let(:options) { { unexpected_argument: true } }

it 'moves failed files and the logs to the failed directory' do
expect(Open3).to receive(:capture3).and_return(['', 'Unknown switches', MockStatus.failed])
expect(Open3).to receive(:capture2e).and_return(['Unknown switches', MockStatus.failed])
copy_zipfile_to_ready('vector.zip')

expect { watcher.exec_gingr_all!('/opt/app/data/gingr/ready/vector.zip') }.to raise_error(Gingr::Watcher::SubprocessError)
Expand Down

0 comments on commit 8adf605

Please sign in to comment.