-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DP-827: Adds the watcher CLI command
- Loading branch information
1 parent
8a253c0
commit 103106b
Showing
21 changed files
with
248 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
/log/* | ||
/zold_app/* | ||
/data/* | ||
/tmp/* | ||
/import/* | ||
.irb_history | ||
.bash_history | ||
.bundle/* | ||
./local/* | ||
|
||
/data/* | ||
!/data/gingr | ||
/data/gingr/ready/* | ||
/data/gingr/processed/* | ||
/data/gingr/failed/* | ||
|
||
/solr/geodata-test/data | ||
/solr/geodata-test/data | ||
/solr/geodata/data | ||
/solr/geodata/data | ||
|
||
!**/.keep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ gem 'uri' | |
group :test do | ||
gem 'rspec', '~> 3.12' | ||
end | ||
|
||
gem "listen", "~> 3.8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Binary file not shown.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
require 'thor' | ||
require_relative 'config' | ||
require_relative 'import_util' | ||
require_relative 'watcher' | ||
|
||
module Gingr | ||
class Cli < Thor | ||
|
@@ -10,6 +11,23 @@ class Cli < Thor | |
|
||
Thor.check_unknown_options! | ||
|
||
desc 'watch', 'Watches a Gingr directory for files ready to be processed' | ||
long_desc <<-TEXT, wrapping: false | ||
EXAMPLES | ||
gingr watch data/gingr --solr-url=https://foo:[email protected]:8983/solr/geodata ... | ||
TEXT | ||
option :solr_url | ||
option :update_reference_field, type: :boolean, default: false | ||
option :spatial_root | ||
option :geoserver_root | ||
option :geoserver_url | ||
option :geoserver_secure_url | ||
def watch(root_dir = nil) | ||
root_dir ||= ENV['GINGR_WATCH_DIRECTORY'] || '/opt/app/data/gingr' | ||
watcher = Gingr::Watcher.new(root_dir, *options) | ||
watcher.start! | ||
end | ||
|
||
desc 'solr', | ||
'Giving a directory path, it will index all json files from the directory/sub-directory to solr' | ||
long_desc <<-TEXT, wrapping: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
require 'listen' | ||
require 'open3' | ||
require_relative 'config' | ||
|
||
module Gingr | ||
class Watcher | ||
include Config | ||
|
||
# Only watch for files in WATCHED_DIRECTORIES[:READY] that match this pattern | ||
WATCH_FILTER = Regexp.compile(/\.zip$/) | ||
|
||
WATCHED_DIRECTORIES = { | ||
# Directory into which new files are dropped when ready for processing. | ||
# .watch! monitors this for new .zip's. | ||
READY: 'ready'.freeze, | ||
# Directory into which successfully processed files are moved post-processing. | ||
PROCESSED: 'processed'.freeze, | ||
# Directory into which failed files are moved post-processing. | ||
FAILED: 'failed'.freeze, | ||
} | ||
|
||
attr_reader :options | ||
attr_reader :root_dir | ||
|
||
def initialize(root_dir, *options) | ||
@root_dir = root_dir | ||
@options = options | ||
|
||
validate_directories! | ||
end | ||
|
||
def start! | ||
start | ||
sleep | ||
end | ||
|
||
def start | ||
Config.logger.info("Monitoring directory for new zipfiles: #{ready_dir}") | ||
listener.start unless listener.processing? | ||
end | ||
|
||
def listener | ||
@listener ||= begin | ||
Listen.to(ready_dir, only: WATCH_FILTER, force_polling: true) do |_, added, _| | ||
added.each do |zipfile| | ||
Config.logger.debug("Processing zipfile: #{zipfile}") | ||
exec_gingr_with_file!(zipfile) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def ready_dir | ||
@ready_dir ||= File.join(@root_dir, WATCHED_DIRECTORIES[:READY]) | ||
end | ||
|
||
def processed_dir | ||
@processed_dir ||= File.join(@root_dir, WATCHED_DIRECTORIES[:PROCESSED]) | ||
end | ||
|
||
def failed_dir | ||
@failed_dir ||= File.join(@root_dir, WATCHED_DIRECTORIES[:FAILED]) | ||
end | ||
|
||
def exec_gingr_with_file!(zipfile) | ||
begin | ||
stdout, stderr, status = Open3.capture3('gingr', 'all', zipfile, *options) | ||
if !status.success? | ||
raise StandardError, "Call to `gingr all` failed: #{status}" | ||
end | ||
|
||
Config.logger.debug("Processed #{zipfile}, moving to #{processed_dir}") | ||
FileUtils.mv(zipfile, processed_dir) | ||
rescue => e | ||
Config.logger.debug("Error processing #{zipfile}, moving to #{failed_dir} and writing captured logs") | ||
FileUtils.mv(zipfile, failed_dir) | ||
File.write(path_to_logfile(zipfile), "#{stdout}\n#{stderr}\n") | ||
end | ||
end | ||
|
||
private | ||
|
||
def path_to_logfile(zipfile) | ||
File.join(failed_dir, "#{File.basename(zipfile, '.*')}.log") | ||
end | ||
|
||
def validate_directories! | ||
validate_directory!('Ready', ready_dir) | ||
validate_directory!('Processed', processed_dir) | ||
validate_directory!('Failed', failed_dir) | ||
end | ||
|
||
def validate_directory!(name, filepath) | ||
if !File.writable?(filepath) | ||
raise StandardError, "#{name} directory is not writeable: #{filepath}" | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.