Skip to content

Commit 65f4aed

Browse files
committed
added new rake_failure mailer and updated monthly_maintenance rake task to send emails
1 parent ecd3685 commit 65f4aed

3 files changed

Lines changed: 66 additions & 8 deletions

File tree

app/mailers/dmptool/mailer.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module Dmptool
44
# DMPTool specific mailers
55
module Mailer
6+
include MailerHelper
7+
68
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
79
def invitation(inviter, invitee, plan)
810
@invitee = invitee
@@ -92,5 +94,15 @@ def new_plan_via_template(recipient:, sender:, plan:)
9294
mail(to: recipient.email, cc: sender.email, subject: subject)
9395
end
9496
end
97+
98+
# Sends a Rake failure to the helpdesk email
99+
def rake_failure(subj:, err:)
100+
@subj = subj || 'Rake task failure'
101+
@err = err || StandardError.new('Something went wrong!')
102+
103+
I18n.with_locale I18n.default_locale do
104+
mail(to: helpdesk_email, subject: subj)
105+
end
106+
end
95107
end
96108
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h1>Scheduled job failure!</h1>
2+
<p><%= @subj %></p>
3+
<br>
4+
<p><strong>Message:</strong> <%= @err.message %></p>
5+
<br>
6+
<p><strong>Backtrace:</strong> <%= @err.backtrace %></p>
7+
<br>
8+
<p>Thank you, Please do not reply to this email.</p>

lib/tasks/utils/housekeeping.rake

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
require 'text'
44
require 'httparty'
55

6+
# Error handling to log in more detail and also send an email
7+
def handle_error(subj:, err:)
8+
puts ''
9+
puts err.message
10+
puts err.backtrace
11+
12+
UserMailer.rake_failure(subj:, err:).deliver_now
13+
14+
puts ''
15+
end
16+
617
namespace :housekeeping do
718
desc 'Clear the cache'
819
task clear_cache: :environment do
@@ -13,25 +24,53 @@ namespace :housekeeping do
1324
task monthly_maintenance: :environment do
1425
p '----------------------------------'
1526
p 'Step 1 of 7 - Purging old OAuth tokens and grants'
16-
Rake::Task['housekeeping:cleanup_oauth'].execute
27+
begin
28+
Rake::Task['housekeeping:cleanup_oauth'].execute
29+
rescue StandardError => e
30+
handle_error(subj: 'Purge old OAuth artifacts', err: e)
31+
end
1732
p '----------------------------------'
1833
p 'Step 2 of 7 - Purging old External API tokens'
19-
Rake::Task['housekeeping:cleanup_external_api_access_tokens'].execute
34+
begin
35+
Rake::Task['housekeeping:cleanup_external_api_access_tokens'].execute
36+
rescue StandardError => e
37+
handle_error(subj: 'Purge old External API tokens', err: e)
38+
end
2039
p '----------------------------------'
2140
p 'Step 3 of 7 - Generating monthly usage statistics'
22-
Rake::Task['stat:build_last_month_parallel'].execute
41+
begin
42+
Rake::Task['stat:build_last_month_parallel'].execute
43+
rescue StandardError => e
44+
handle_error(subj: 'Generating monthly usage stats', err: e)
45+
end
2346
p '----------------------------------'
2447
p 'Step 4 of 7 - Fetching latest data from RDA Metadata standard catalog'
25-
Rake::Task['external_api:load_rdamsc_standards'].execute
48+
begin
49+
Rake::Task['external_api:load_rdamsc_standards'].execute
50+
rescue StandardError => e
51+
handle_error(subj: 'Fetching latest Metadata Standards from RDA', err: e)
52+
end
2653
p '----------------------------------'
2754
p 'Step 5 of 7 - Fetching latest data from SPX license database'
28-
Rake::Task['external_api:load_spdx_licenses'].execute
55+
begin
56+
Rake::Task['external_api:load_spdx_licenses'].execute
57+
rescue StandardError => e
58+
handle_error(subj: 'Fetching latest Licenses from SPDX', err: e)
59+
end
2960
p '----------------------------------'
3061
p 'Step 6 of 7 - Fetching latest data from the re3data repository'
31-
Rake::Task['external_api:load_re3data_repos'].execute
62+
begin
63+
Rake::Task['external_api:load_re3data_repos'].execute
64+
rescue StandardError => e
65+
handle_error(subj: 'Fetching latest Repositories from re3data', err: e)
66+
end
3267
p '----------------------------------'
3368
p 'Step 7 of 7 - Fetching latest data from the ROR API'
34-
Rake::Task['external_api:sync_registry_orgs'].execute
69+
begin
70+
Rake::Task['external_api:sync_registry_orgs'].execute
71+
rescue StandardError => e
72+
handle_error(subj: 'Fetching latest Orgs from ROR', err: e)
73+
end
3574
end
3675

3776
desc 'Sync DMP metadata with the DMP ID minting authority'
@@ -79,7 +118,6 @@ namespace :housekeeping do
79118
task cleanup_oauth: :environment do
80119
# Removing expired Access Tokens and Grants to help prune the DB
81120
# - Note that expiration values are stored in seconds!
82-
83121
Doorkeeper::AccessGrant.all.each do |grant|
84122
expiry = grant.created_at + grant.expires_in.seconds
85123
grant.destroy if Time.zone.now >= expiry

0 commit comments

Comments
 (0)