private static void SendExpirationNotification(ExpirationNotifier notifier, License license, FrameworkServer server, bool sentRenewalRequest)
        {
            if (license.Owners.Count == 0)
            {
                LogError($"Unable to notify license expiration for '{server.HostName}'.  No contacts specified.");
                return;
            }

            LicenseOwner owner = license.Owners.FirstOrDefault();

            try
            {
                MailAddress recipient = new MailAddress(owner.Contact);
                string      subject   = string.Format(Resource.LicenseEmailSubject, license.Solution, server.HostName);
                string      body      = sentRenewalRequest ?
                                        string.Format(Resource.LicenseEmailBodyAuto, license.Solution, server.HostName, license.ExpirationDate.ToShortDateString(), Environment.MachineName) :
                                        string.Format(Resource.LicenseEmailBodyManual, license.Solution, server.HostName, license.ExpirationDate.ToShortDateString(), Environment.MachineName);
                notifier.SendNotification(recipient, subject, body);
            }
            catch (FormatException ex)
            {
                // Notification recipient is not a valid email address
                LogDebug(ex.ToString());
            }
        }
        /// <summary>
        /// Sends notifications for all expiring licenses.
        /// </summary>
        /// <param name="expirationNotifier">The <see cref="ExpirationNotifier" />.</param>
        public void SendExpirationNotifications(ExpirationNotifier expirationNotifier)
        {
            using (AssetInventoryContext context = new AssetInventoryContext(_connectionString))
            {
                List <License> expiringLicenses = context.Licenses.Include(n => n.Owners)
                                                  .Where(n => n.RequestSentDate == null && DbFunctions.AddDays(DateTime.Now, n.ExpirationNoticeDays) > n.ExpirationDate)
                                                  .ToList();
                LogDebug($"Found {expiringLicenses.Count} expiring licenses.");

                IEnumerable <Guid> serverIds = expiringLicenses.Select(n => n.FrameworkServerId).Distinct();
                Dictionary <Guid, FrameworkServer> servers = context.FrameworkServers
                                                             .Where(n => serverIds.Contains(n.FrameworkServerId))
                                                             .ToDictionary(n => n.FrameworkServerId, n => n);

                foreach (License license in expiringLicenses)
                {
                    servers.TryGetValue(license.FrameworkServerId, out FrameworkServer server);

                    bool sentRenewalRequest = false;
                    if (license.SolutionVersion.Equals("latest", StringComparison.InvariantCultureIgnoreCase))
                    {
                        //If not set to "latest" the licence renewal will be done manually
                        SendNewLicenseRequest(license, server);
                        sentRenewalRequest = true;
                    }

                    SendExpirationNotification(expirationNotifier, license, server, sentRenewalRequest);

                    license.RequestSentDate = DateTime.Now;
                    context.SaveChanges();
                }

                LogDebug("Finished processing license notifications.");
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AssetInventoryService" /> class with the specified maintenance managers.
        /// </summary>
        /// <param name="managers">The collection of <see cref="IAssetInventoryMaintenanceManager" /> to use when running maintenance.</param>
        /// <param name="expirationNotifier">The <see cref="ExpirationNotifier" />.</param>
        /// <exception cref="ArgumentNullException"><paramref name="managers" /> is null.</exception>
        public AssetInventoryService(IEnumerable <IAssetInventoryMaintenanceManager> managers, ExpirationNotifier expirationNotifier)
        {
            if (managers == null)
            {
                throw new ArgumentNullException(nameof(managers));
            }

            _expirationNotifier    = expirationNotifier;
            _maintenanceManagers   = new List <IAssetInventoryMaintenanceManager>(managers);
            _maintenanceCheckTimer = new Timer(RunMaintenance, null, _maintenanceCheckFrequency, _maintenanceCheckFrequency);
        }
Пример #4
0
 private static void SendExpirationNotification(ExpirationNotifier notifier, AssetReservation reservation)
 {
     try
     {
         MailAddress recipient       = new MailAddress(reservation.NotificationRecipient);
         string      reservationText = (reservation.End < DateTime.Now ? Resource.ExpiredReservationText : Resource.ExpiringReservationText);
         string      subject         = string.Format(reservationText, reservation.AssetId);
         string      body            = string.Format(Resource.ReservationEmailBody, subject, reservation.End.ToString("f"));
         notifier.SendNotification(recipient, subject, body);
     }
     catch (FormatException)
     {
         LogWarn($"Ignoring invalid notification recipient: {reservation.NotificationRecipient}");
     }
 }
Пример #5
0
        /// <summary>
        /// Sends notifications for all expiring and expired asset reservations.
        /// </summary>
        /// <param name="expirationNotifier">The <see cref="ExpirationNotifier" />.</param>
        public void SendExpirationNotifications(ExpirationNotifier expirationNotifier)
        {
            using (AssetInventoryContext context = new AssetInventoryContext(_connectionString))
            {
                List <AssetReservation> reservations = context.AssetReservations.Where(n => n.End < DbFunctions.AddHours(DateTime.Now, 24)).ToList();
                LogDebug($"Found {reservations.Count} expiring or expired reservations.");

                foreach (AssetReservation reservation in reservations.Where(n => n.NotificationRecipient != null))
                {
                    SendExpirationNotification(expirationNotifier, reservation);
                }

                LogDebug("Finished processing asset reservation notifications.");
            }
        }
 void IAssetInventoryMaintenanceManager.SendExpirationNotifications(ExpirationNotifier expirationNotifier)
 {
     // Nothing to do in this class
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="AssetInventoryService" /> class with all available maintenance managers.
 /// </summary>
 /// <param name="connectionString">The <see cref="AssetInventoryConnectionString" />.</param>
 /// <param name="expirationNotifier">The <see cref="ExpirationNotifier" />.</param>
 /// <exception cref="ArgumentNullException"><paramref name="connectionString" /> is null.</exception>
 public AssetInventoryService(AssetInventoryConnectionString connectionString, ExpirationNotifier expirationNotifier)
     : this(GetAllManagers(connectionString), expirationNotifier)
 {
 }