Skip to content

Commit

Permalink
Fixes #19314 - Notification for subscr. expiring soon
Browse files Browse the repository at this point in the history
The notifications drawer should show a warning when some subscription is
about to expire. "expiring_soon?" defined in the subscriptions.rb code
says this is 120 days before it expires.
  • Loading branch information
dLobatog committed Mar 21, 2018
1 parent bf02310 commit e72d549
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
9 changes: 9 additions & 0 deletions app/jobs/send_expire_soon_notifications.rb
@@ -0,0 +1,9 @@
class SendExpireSoonNotifications < ApplicationJob
after_perform do
self.class.set(:wait => 12.hours).perform_later
end

def perform
Katello::UINotifications::Subscriptions::ExpireSoon.deliver!
end
end
11 changes: 11 additions & 0 deletions app/models/katello/concerns/organization_extensions.rb
Expand Up @@ -182,6 +182,17 @@ def cancel_repo_discovery
def regenerate_ueber_cert
::Katello::Resources::Candlepin::Owner.generate_ueber_cert(self.label)
end

def expiring_subscriptions
subscriptions.select(&:expiring_soon?)
end

def notification_recipients_ids
users = User.unscoped.all.find_all do |user|
user.can?(:import_manifest, self)
end
users.pluck(:id)
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/katello/subscription.rb
Expand Up @@ -19,6 +19,8 @@ def self.subscribable
.group("#{self.table_name}.id")
end

delegate :user_ids, :to => :organization

def self.using_virt_who
joins(:pools).where("#{Katello::Pool.table_name}.virt_who" => true)
end
Expand Down
65 changes: 65 additions & 0 deletions app/services/katello/ui_notifications/subscriptions/expire_soon.rb
@@ -0,0 +1,65 @@
module Katello
module UINotifications
module Subscriptions
class ExpireSoon
class << self
def deliver!
::Organization.unscoped.all.each do |organization|
if (notification = notification_already_exists?(organization))
next unless organization.expiring_subscriptions.count.to_s ==
notification.message.split(' ').first
notification.update_attributes(
:message => message(organization),
:actions => actions
)
else
next unless organization.expiring_subscriptions.count > 0
::Notification.create!(
:subject => organization,
:initiator => User.anonymous_admin,
:audience => Notification::AUDIENCE_SUBJECT,
:message => message(organization),
:actions => actions,
:notification_blueprint => blueprint
)
end
end
end

def notification_already_exists?(subject)
subs_expiration_notification = Notification.unscoped.find_by(:subject => subject)
return false if subs_expiration_notification.blank? ||
subs_expiration_notification.notification_blueprint != blueprint
subs_expiration_notification
end

def message(organization)
::UINotifications::StringParser.new(
blueprint.message,
:expiring_subs => organization.expiring_subscriptions.count,
:subject => organization,
:days => ::Katello::Pool::DAYS_RECENTLY_EXPIRED
)
end

def actions
{
:links => [
{
:href => "/subscriptions?search=expires<\"#{Katello::Pool::DAYS_EXPIRING_SOON} days from now\"",
:title => _('Subscriptions'),
:external => true
}
]
}
end

def blueprint
@blueprint ||= NotificationBlueprint.unscoped.find_by(
:name => 'subs_expire_soon')
end
end
end
end
end
end
6 changes: 6 additions & 0 deletions db/seeds.d/109-katello-notification-blueprints.rb
Expand Up @@ -12,6 +12,12 @@
title: N_('Details')
]
}
},
{
group: N_('Subscriptions'),
name: 'subs_expire_soon',
message: N_('%{expiring_subs} subscriptions in %{subject} are going to expire in less than %{days} days. Please renew them before they expire to guarantee your hosts will continue receiving content.'),
level: 'warning'
}
]

Expand Down
12 changes: 9 additions & 3 deletions lib/katello/scheduled_jobs.rb
@@ -1,12 +1,18 @@
# First, we check if there's a job already enqueued for Pulp notifications
# First, we check if there's a job already enqueued for any notifications
::Foreman::Application.dynflow.config.on_init do |world|
pending_jobs = world.persistence.find_execution_plans(filters: { :state => 'scheduled' })
scheduled_job = pending_jobs.select do |job|
scheduled_pulp_job = pending_jobs.select do |job|
delayed_plan = world.persistence.load_delayed_plan job.id
next if delayed_plan.blank?
delayed_plan.to_hash[:serialized_args].first["job_class"] == 'CreatePulpDiskSpaceNotifications'
end
scheduled_subs_expiration_job = pending_jobs.select do |job|
delayed_plan = world.persistence.load_delayed_plan job.id
next if delayed_plan.blank?
delayed_plan.to_hash[:serialized_args].first["job_class"] == 'SendExpireSoonNotifications'
end

# Only create notifications if there isn't a scheduled job
CreatePulpDiskSpaceNotifications.perform_later if scheduled_job.blank?
CreatePulpDiskSpaceNotifications.perform_later if scheduled_pulp_job.blank?
SendExpireSoonNotifications.perform_later if scheduled_subs_expiration_job.blank?
end

0 comments on commit e72d549

Please sign in to comment.