Example #1
0
        /// <summary>
        /// Get a previously cached order or if its too old, create a new one
        /// </summary>
        /// <param name="renewal"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public async Task <OrderDetails?> GetOrCreate(Order order, RunLevel runLevel)
        {
            var cacheKey      = _certificateService.CacheKey(order);
            var existingOrder = default(OrderDetails); // FindRecentOrder(cacheKey);

            if (existingOrder != null)
            {
                try
                {
                    if (runLevel.HasFlag(RunLevel.IgnoreCache))
                    {
                        _log.Warning("Cached order available but not used with the --{switch} switch.",
                                     nameof(MainArguments.Force).ToLower());
                    }
                    else
                    {
                        existingOrder = await RefreshOrder(existingOrder);

                        if (existingOrder.Payload.Status == AcmeClient.OrderValid ||
                            existingOrder.Payload.Status == AcmeClient.OrderReady)
                        {
                            _log.Warning("Using cached order. To force issue of a new certificate within {days} days, " +
                                         "run with the --{switch} switch. Be ware that you might run into rate limits doing so.",
                                         _settings.Cache.ReuseDays,
                                         nameof(MainArguments.Force).ToLower());
                            return(existingOrder);
                        }
                        else
                        {
                            _log.Debug("Cached order has status {status}, discarding", existingOrder.Payload.Status);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _log.Warning("Unable to refresh cached order: {ex}", ex.Message);
                }
            }
            var identifiers = order.Target.GetHosts(false);

            return(await CreateOrder(identifiers, cacheKey));
        }