Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
/ NuGet.Jobs Public archive

Commit

Permalink
Add ReinitializeAfterSeconds CLI argument
Browse files Browse the repository at this point in the history
  • Loading branch information
loic-sharma committed Apr 16, 2018
1 parent e83ba05 commit 5e75c48
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/NuGet.Jobs.Common/Configuration/JobArgumentNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class JobArgumentNames
public const string Once = "Once";
public const string Sleep = "Sleep";
public const string Interval = "Interval";
public const string ReinitializeAfterSeconds = "ReinitializeAfterSeconds";

public const string WhatIf = "WhatIf";

Expand Down
47 changes: 43 additions & 4 deletions src/NuGet.Jobs.Common/JobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public static async Task Run(JobBase job, string[] commandLineArgs)
loggerFactory = ConfigureLogging(job);

var runContinuously = !JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, JobArgumentNames.Once);
var reinitializeAfterSeconds = JobConfigurationManager.TryGetIntArgument(jobArgsDictionary, JobArgumentNames.ReinitializeAfterSeconds);
var sleepDuration = JobConfigurationManager.TryGetIntArgument(jobArgsDictionary, JobArgumentNames.Sleep); // sleep is in milliseconds

if (!sleepDuration.HasValue)
{
sleepDuration = JobConfigurationManager.TryGetIntArgument(jobArgsDictionary, JobArgumentNames.Interval);
Expand All @@ -88,12 +90,19 @@ public static async Task Run(JobBase job, string[] commandLineArgs)
sleepDuration = 5000;
}

if (!reinitializeAfterSeconds.HasValue)
{
_logger.LogInformation(
$"{JobArgumentNames.ReinitializeAfterSeconds} command line argument is not provided or is not a valid integer. " +
"The job will reinitialize on every iteration");
}

// Ensure that SSLv3 is disabled and that Tls v1.2 is enabled.
ServicePointManager.SecurityProtocol &= ~SecurityProtocolType.Ssl3;
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

// Run the job loop
await JobLoop(job, runContinuously, sleepDuration.Value, jobArgsDictionary);
await JobLoop(job, runContinuously, sleepDuration.Value, reinitializeAfterSeconds, jobArgsDictionary);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -123,22 +132,34 @@ private static string PrettyPrintTime(double milliSeconds)
$"'{milliSeconds:F3}' ms (or '{seconds:F3}' seconds or '{minutes:F3}' mins)";
}

private static async Task JobLoop(JobBase job, bool runContinuously, int sleepDuration, IDictionary<string, string> jobArgsDictionary)
private static async Task JobLoop(
JobBase job,
bool runContinuously,
int sleepDuration,
int? reinitializeAfterSeconds,
IDictionary<string, string> jobArgsDictionary)
{
// Run the job now
var stopWatch = new Stopwatch();
Stopwatch timeSinceInitialization = null;

while (true)
{
_logger.LogInformation("Running {RunType}", (runContinuously ? " continuously..." : " once..."));
_logger.LogInformation("SleepDuration is {SleepDuration}", PrettyPrintTime(sleepDuration));
_logger.LogInformation("Job run started...");

var initialized = false;
stopWatch.Restart();

try
{
job.Init(jobArgsDictionary);
if (ShouldInitialize(reinitializeAfterSeconds, timeSinceInitialization))
{
job.Init(jobArgsDictionary);
timeSinceInitialization = Stopwatch.StartNew();
}

initialized = true;

await job.Run();
Expand Down Expand Up @@ -169,5 +190,23 @@ private static async Task JobLoop(JobBase job, bool runContinuously, int sleepDu
await Task.Delay(sleepDuration);
}
}

private static bool ShouldInitialize(int? reinitializeAfterSeconds, Stopwatch timeSinceInitialization)
{
// If there is no wait time between reinitializations, always reinitialize.
if (!reinitializeAfterSeconds.HasValue)
{
return true;
}

// A null time since last initialization indicates that the job should be initialized for the first time.
if (timeSinceInitialization == null)
{
return true;
}

// Otherwise, only reinitialize if the reinitialization threshold has been reached.
return (timeSinceInitialization.Elapsed.TotalSeconds > reinitializeAfterSeconds.Value);
}
}
}
28 changes: 5 additions & 23 deletions src/Validation.PackageSigning.RevalidateCertificate/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,18 @@ public override void Init(IDictionary<string, string> jobArgsDictionary)
{
base.Init(jobArgsDictionary);

if (!JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, JobArgumentNames.Once))
{
throw new ArgumentException($"Missing {JobArgumentNames.Once}");
}

_revalidator = _serviceProvider.GetRequiredService<ICertificateRevalidator>();
_configuration = _serviceProvider.GetRequiredService<RevalidationConfiguration>();
_logger = _serviceProvider.GetRequiredService<ILogger<Job>>();
}

public override async Task Run()
{
var executionTime = Stopwatch.StartNew();

while (executionTime.Elapsed < _configuration.RestartThreshold)
{
// Both of these methods only do a chunk of the possible promotion/revalidating work before
// completing. This "Run" method may need to run several times to promote all signatures
// and to revalidate all stale certificates.
await _revalidator.PromoteSignaturesAsync();
await _revalidator.RevalidateStaleCertificatesAsync();

_logger.LogInformation("Sleeping for {SleepDuration}...", _configuration.SleepDuration);

await Task.Delay(_configuration.SleepDuration);
}

_logger.LogInformation(
"Job has reached configured restart threshold after {ElapsedTime}. Restarting...",
executionTime.Elapsed);
// Both of these methods only do a chunk of the possible promotion/revalidating work before
// completing. This "Run" method may need to run several times to promote all signatures
// and to revalidate all stale certificates.
await _revalidator.PromoteSignaturesAsync();
await _revalidator.RevalidateStaleCertificatesAsync();
}

protected override void ConfigureJobServices(IServiceCollection services, IConfigurationRoot configurationRoot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ namespace Validation.PackageSigning.RevalidateCertificate
{
public class RevalidationConfiguration
{
/// <summary>
/// How long this job should sleep between runs.
/// </summary>
public TimeSpan SleepDuration { get; set; }

/// <summary>
/// The time period after which the job is gracefully shut down and restarted.
/// </summary>
public TimeSpan RestartThreshold { get; set; }

/// <summary>
/// The maximum number of package signatures that can be scanned for promotion.
/// Each iteration of the job will scan signatures until it finds <see cref="SignaturePromotionBatchSize"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ title #{Jobs.validation.packagesigning.revalidatecertificate.Title}
start /w Validation.PackageSigning.RevalidateCertificate.exe ^
-Configuration #{Jobs.validation.packagesigning.revalidatecertificate.Configuration} ^
-InstrumentationKey "#{Jobs.validation.packagesigning.revalidatecertificate.InstrumentationKey}" ^
-Once
-ReinitializeAfterSeconds 86400

echo "Finished #{Jobs.validation.packagesigning.revalidatecertificate.Title}"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"RevalidateJob": {
"SleepDuration": "00:00:01:00",
"RestartThreshold": "1:00:00:00",
"SignaturePromotionScanSize": 50,
"SignaturePromotionBatchSize": 10,
"CertificateRevalidationBatchSize": 10,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"RevalidateJob": {
"SleepDuration": "00:00:01:00",
"RestartThreshold": "1:00:00:00",
"SignaturePromotionScanSize": 50,
"SignaturePromotionBatchSize": 10,
"CertificateRevalidationBatchSize": 10,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"RevalidateJob": {
"SleepDuration": "00:00:01:00",
"RestartThreshold": "1:00:00:00",
"SignaturePromotionScanSize": 50,
"SignaturePromotionBatchSize": 10,
"CertificateRevalidationBatchSize": 10,
Expand Down

0 comments on commit 5e75c48

Please sign in to comment.