public static IServiceCollection AddZipDeploy(this IServiceCollection services, Action <ZipDeployOptions> setupOptions = null) { var options = new ZipDeployOptions(); setupOptions?.Invoke(options); services.RegisterDefaults(options); services.AddHostedService <Application>(); return(services); }
public AspNetRestart( ILogger <AspNetRestart> logger, IProcessWebConfig processWebConfig, ICanPauseTrigger canPauseTrigger, ZipDeployOptions options) { _logger = logger; _processWebConfig = processWebConfig; _canPauseTrigger = canPauseTrigger; _options = options; }
public DetectPackage(ILogger <DetectPackage> logger, ZipDeployOptions options) { _logger = logger; _options = options; _fsw = new FileSystemWatcher(Environment.CurrentDirectory, options.NewPackageFileName); _fsw.Created += OnPackageDetected; _fsw.Changed += OnPackageDetected; _fsw.Renamed += OnPackageDetected; _fsw.EnableRaisingEvents = true; _logger.LogInformation($"Watching for {options.NewPackageFileName} in {Environment.CurrentDirectory}"); }
public Application( ILogger <Application> logger, ICleaner cleaner, IDetectPackage detectPackage, ITriggerRestart triggerRestart, ZipDeployOptions options, IUnzipper unzipper) { _logger = logger; _cleaner = cleaner; _detectPackage = detectPackage; _triggerRestart = triggerRestart; _options = options; _unzipper = unzipper; }
public static void Run(ILoggerFactory loggerFactory, Action <ZipDeployOptions> setupOptions, Action program) { loggerFactory = loggerFactory ?? new LoggerFactory(); var logger = loggerFactory.CreateLogger(typeof(ZipDeploy)); logger.LogDebug("ZipDeploy starting"); var options = new ZipDeployOptions(); setupOptions?.Invoke(options); var provider = new ServiceCollection() .RegisterLogger(loggerFactory) .RegisterDefaults(options) .BuildServiceProvider(); try { var detectPackage = provider.GetRequiredService <IDetectPackage>(); var triggerRestart = provider.GetRequiredService <ITriggerRestart>(); detectPackage.PackageDetected += triggerRestart.Trigger; var cleaner = provider.GetRequiredService <ICleaner>(); cleaner.DeleteObsoleteFiles(); detectPackage.Started(); program(); } finally { logger.Retry(options, "ZipDeploy before shutdown", () => { if (File.Exists(Path.Combine(Environment.CurrentDirectory, options.NewPackageFileName))) { logger.LogInformation("Found package {packageName}", options.NewPackageFileName); var unzipper = provider.GetRequiredService <IUnzipper>(); unzipper.Unzip(); } logger.LogDebug("ZipDeploy completed after process shutdown"); }); } }
public static void Retry(this ILogger logger, ZipDeployOptions options, string description, Action action) { const int maxRetries = 3; var retryCount = 0; while (retryCount < maxRetries) { try { logger.LogDebug("Start {description}", description); action(); logger.LogDebug("Finish {description}", description); return; } catch (Exception ex) { retryCount++; var level = retryCount < maxRetries ? LogLevel.Warning : LogLevel.Error; logger.Log(level, ex, "Error {retryCount} during {description}: {error}", retryCount, description, ex?.ToString()); Thread.Sleep(options.ErrorRetryPeriod); } } }
public static IServiceCollection RegisterDefaults(this IServiceCollection services, ZipDeployOptions options) { services.AddSingleton(options); services.AddSingleton <IDetectPackage, DetectPackage>(); services.AddSingleton <ITriggerRestart, AspNetRestart>(); services.AddSingleton <ICleaner, Cleaner>(); services.AddSingleton <IUnzipper, Unzipper>(); services.AddSingleton <IProcessWebConfig, ProcessWebConfig>(); services.AddSingleton <ICanPauseTrigger, CanPauseTrigger>(); return(services); }
public Unzipper(ILogger <Unzipper> logger, ZipDeployOptions options) { _logger = logger; _options = options; _fsUtil = new FsUtil(logger); }