Skip to content

Commit

Permalink
Fix problems with logger in JSON format
Browse files Browse the repository at this point in the history
This commit addresses PR comments and fixes a problem with the JSON log
  • Loading branch information
lagoan committed Jun 5, 2024
1 parent 09f95e9 commit cd1b645
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
8 changes: 3 additions & 5 deletions lib/pushmi_pullyu/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,8 @@ def rotate_logs
def run_preservation_cycle
begin
entity = queue.wait_next_item
# We add + 1 to the get_entity_ingestion_attempt because humans like to start counting from 1
PushmiPullyu::Logging.log_preservation_attempt(entity,
queue.get_entity_ingestion_attempt(entity) + 1)
queue.get_entity_ingestion_attempt(entity))
return unless entity && entity[:type].present? && entity[:uuid].present?
rescue StandardError => e
log_exception(e)
Expand All @@ -224,10 +223,9 @@ def run_preservation_cycle
log_exception(e)
begin
queue.add_entity_in_timeframe(entity)
# We add + 1 to the get_entity_ingestion_attempt because humans like to start counting from 1
PushmiPullyu::Logging.log_preservation_fail_and_retry(entity, queue.get_entity_ingestion_attempt(entity) + 1, e)
PushmiPullyu::Logging.log_preservation_fail_and_retry(entity, queue.get_entity_ingestion_attempt(entity), e)
rescue PushmiPullyu::PreservationQueue::MaxDepositAttemptsReached => e
PushmiPullyu::Logging.log_preservation_failure(entity, queue.get_entity_ingestion_attempt(entity) + 1, e)
PushmiPullyu::Logging.log_preservation_failure(entity, queue.get_entity_ingestion_attempt(entity), e)
log_exception(e)
end

Expand Down
10 changes: 8 additions & 2 deletions lib/pushmi_pullyu/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def log_preservation_success(entity, deposited_file, aip_directory)
end

def log_preservation_fail_and_retry(entity, try_attempt, exception)
# We add + 1 to try_attempt because humans like to start counting from 1
try_attempt += 1
message = "#{entity[:type]} failed to be deposited and will try again.\n" \
"Here are the details of this preservation event:\n" \
"\t#{entity[:type]} uuid: #{entity[:uuid]}" \
Expand All @@ -106,10 +108,12 @@ def log_preservation_fail_and_retry(entity, try_attempt, exception)
end

def log_preservation_failure(entity, try_attempt, exception)
# We add + 1 to try_attempt because humans like to start counting from 1
try_attempt += 1
message = "#{entity[:type]} failed to be deposited.\n" \
"Here are the details of this preservation event:\n" \
"\t#{entity[:type]} uuid: #{entity[:uuid]}" \
"\tRetry attempt: #{try_attempt}\n"
"\tTry attempt: #{try_attempt}\n"

message_information = {
event_type: :failure,
Expand All @@ -124,10 +128,12 @@ def log_preservation_failure(entity, try_attempt, exception)
end

def log_preservation_attempt(entity, try_attempt)
# We add + 1 to try_attempt because humans like to start counting from 1
try_attempt += 1
message = "#{entity[:type]} will attempt to be deposited.\n" \
"Here are the details of this preservation event:\n" \
"\t#{entity[:type]} uuid: #{entity[:uuid]}" \
"\tRetry attempt: #{try_attempt}\n"
"\tTry attempt: #{try_attempt}\n"

message_information = {
event_type: :attempt,
Expand Down
12 changes: 6 additions & 6 deletions spec/pushmi_pullyu/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class LoggerTest
expect(File.exist?("#{tmp_log_dir}/preservation_events.log")).to be(true)
log_data = File.read("#{tmp_log_dir}/preservation_events.log")
expect(log_data).to include("#{entity[:type]} will attempt to be deposited.")
expect(log_data).to include("#{entity[:type]} uuid: #{entity[:uuid]} Retry attempt: 1")
expect(log_data).to include("#{entity[:type]} uuid: #{entity[:uuid]} Try attempt: 2")

# Check JSON log
expect(File.exist?("#{tmp_log_dir}/preservation_events.json")).to be(true)
Expand All @@ -194,7 +194,7 @@ class LoggerTest
'event_type' => 'attempt',
'entity_type' => 'items',
'entity_uuid' => 'e2ec88e3-3266-4e95-8575-8b04fac2a679',
'try_attempt' => 1,
'try_attempt' => 2,
'event_time' => Time.now.to_s
)
end
Expand Down Expand Up @@ -232,7 +232,7 @@ class LoggerTest
log_data = File.read("#{tmp_log_dir}/preservation_events.log")
expect(log_data).to include("#{entity[:type]} failed to be deposited and will try again.")
expect(log_data).to include(
"#{entity[:type]} uuid: #{entity[:uuid]} Readding to preservation queue with try attempt: 2"
"#{entity[:type]} uuid: #{entity[:uuid]} Readding to preservation queue with try attempt: 3"
)

# Check JSON log
Expand All @@ -244,7 +244,7 @@ class LoggerTest
'event_type' => 'fail_and_retry',
'entity_type' => 'items',
'entity_uuid' => 'e2ec88e3-3266-4e95-8575-8b04fac2a679',
'try_attempt' => 2,
'try_attempt' => 3,
'error_message' => 'PushmiPullyu::AIP::Downloader::JupiterDownloadError',
'event_time' => Time.now.to_s
)
Expand Down Expand Up @@ -280,7 +280,7 @@ class LoggerTest
expect(File.exist?("#{tmp_log_dir}/preservation_events.log")).to be(true)
log_data = File.read("#{tmp_log_dir}/preservation_events.log")
expect(log_data).to include("#{entity[:type]} failed to be deposited.")
expect(log_data).to include("#{entity[:type]} uuid: #{entity[:uuid]} Retry attempt: 15")
expect(log_data).to include("#{entity[:type]} uuid: #{entity[:uuid]} Try attempt: 16")

# Check JSON log
expect(File.exist?("#{tmp_log_dir}/preservation_events.json")).to be(true)
Expand All @@ -290,7 +290,7 @@ class LoggerTest
'event_type' => 'failure',
'entity_type' => 'items',
'entity_uuid' => 'e2ec88e3-3266-4e95-8575-8b04fac2a679',
'try_attempt' => 15,
'try_attempt' => 16,
'error_message' => 'PushmiPullyu::PreservationQueue::MaxDepositAttemptsReached',
'event_time' => Time.now.to_s
)
Expand Down

0 comments on commit cd1b645

Please sign in to comment.