Skip to content

Commit

Permalink
Merge pull request #39 from BertCotton/BC.UpdaterLogging
Browse files Browse the repository at this point in the history
Adding base class for logger and adding logging
  • Loading branch information
BertCotton authored Aug 7, 2017
2 parents f325ac2 + 4475eb4 commit 79c1a4f
Show file tree
Hide file tree
Showing 10 changed files with 387 additions and 469 deletions.
75 changes: 27 additions & 48 deletions src/TfsAdvanced.Updater/Tasks/BuildDefinitionUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,58 @@
using TfsAdvanced.Models;
using TfsAdvanced.Models.Infrastructure;
using TFSAdvanced.Models.DTO;
using TFSAdvanced.Updater.Tasks;

namespace TfsAdvanced.Updater.Tasks
{
public class BuildDefinitionUpdater
public class BuildDefinitionUpdater : UpdaterBase
{
private readonly BuildDefinitionRepository buildDefinitionRepository;
private readonly UpdateStatusRepository updateStatusRepository;
private readonly ProjectRepository projectRepository;
private readonly RepositoryRepository repositoryRepository;
private readonly RequestData requestData;
private readonly ILogger<BuildDefinitionUpdater> logger;
private bool IsRunning;

public BuildDefinitionUpdater(BuildDefinitionRepository buildDefinitionRepository, RequestData requestData, ProjectRepository projectRepository, UpdateStatusRepository updateStatusRepository, RepositoryRepository repositoryRepository, ILogger<BuildDefinitionUpdater> logger)

public BuildDefinitionUpdater(BuildDefinitionRepository buildDefinitionRepository, RequestData requestData, ProjectRepository projectRepository, UpdateStatusRepository updateStatusRepository, RepositoryRepository repositoryRepository, ILogger<BuildDefinitionUpdater> logger) :
base(logger)
{
this.buildDefinitionRepository = buildDefinitionRepository;
this.requestData = requestData;
this.projectRepository = projectRepository;
this.updateStatusRepository = updateStatusRepository;
this.repositoryRepository = repositoryRepository;
this.logger = logger;
}

[AutomaticRetry(Attempts = 0)]
public void Update()
protected override void Update()
{
if (IsRunning)
return;
IsRunning = true;
try
var buildDefinitions = new ConcurrentBag<BuildDefinition>();
Parallel.ForEach(projectRepository.GetAll(), new ParallelOptions {MaxDegreeOfParallelism = AppSettings.MAX_DEGREE_OF_PARALLELISM}, project =>
{

var buildDefinitions = new ConcurrentBag<BuildDefinition>();
Parallel.ForEach(projectRepository.GetAll(), new ParallelOptions {MaxDegreeOfParallelism = AppSettings.MAX_DEGREE_OF_PARALLELISM}, project =>
var definitions = GetAsync.FetchResponseList<TFSAdvanced.Updater.Models.Builds.BuildDefinition>(requestData, $"{requestData.BaseAddress}/{project.Name}/_apis/build/definitions?api=2.2").Result;
if (definitions == null)
{
var definitions = GetAsync.FetchResponseList<TFSAdvanced.Updater.Models.Builds.BuildDefinition>(requestData, $"{requestData.BaseAddress}/{project.Name}/_apis/build/definitions?api=2.2").Result;
if (definitions == null)
{
logger.LogInformation($"Unable to get the definitiosn for the project {project.Name}");
return;
}
Parallel.ForEach(definitions, new ParallelOptions {MaxDegreeOfParallelism = AppSettings.MAX_DEGREE_OF_PARALLELISM}, definition =>
{
var populatedDefinition = GetAsync.Fetch<TFSAdvanced.Updater.Models.Builds.BuildDefinition>(requestData, definition.url).Result;
logger.LogInformation($"Unable to get the definitiosn for the project {project.Name}");
return;
}
Parallel.ForEach(definitions, new ParallelOptions {MaxDegreeOfParallelism = AppSettings.MAX_DEGREE_OF_PARALLELISM}, definition =>
{
var populatedDefinition = GetAsync.Fetch<TFSAdvanced.Updater.Models.Builds.BuildDefinition>(requestData, definition.url).Result;

buildDefinitions.Add(new BuildDefinition
{
DefaultBranch = populatedDefinition.repository.defaultBranch,
Folder = populatedDefinition.path,
Id = populatedDefinition.id,
Name = populatedDefinition.name,
Url = populatedDefinition._links.web.href,
Repository = repositoryRepository.GetById(populatedDefinition.repository.id)
});
buildDefinitions.Add(new BuildDefinition
{
DefaultBranch = populatedDefinition.repository.defaultBranch,
Folder = populatedDefinition.path,
Id = populatedDefinition.id,
Name = populatedDefinition.name,
Url = populatedDefinition._links.web.href,
Repository = repositoryRepository.GetById(populatedDefinition.repository.id)
});
});
});

buildDefinitionRepository.Update(buildDefinitions);

updateStatusRepository.UpdateStatus(new UpdateStatus { LastUpdate = DateTime.Now, UpdatedRecords = buildDefinitions.Count, UpdaterName = nameof(BuildDefinitionUpdater)});

}
catch (Exception ex)
{
throw new InvalidOperationException("Error processing the build definition updater.", ex);
}
finally

{
IsRunning = false;
}

buildDefinitionRepository.Update(buildDefinitions);

updateStatusRepository.UpdateStatus(new UpdateStatus {LastUpdate = DateTime.Now, UpdatedRecords = buildDefinitions.Count, UpdaterName = nameof(BuildDefinitionUpdater)});
}
}
}
82 changes: 32 additions & 50 deletions src/TfsAdvanced.Updater/Tasks/BuildUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,66 @@
using TfsAdvanced.Models.Infrastructure;
using TFSAdvanced.Models.DTO;
using TFSAdvanced.Updater.Models.Builds;
using TFSAdvanced.Updater.Tasks;
using Build = TFSAdvanced.Models.DTO.Build;
using BuildStatus = TFSAdvanced.Models.DTO.BuildStatus;

namespace TfsAdvanced.Updater.Tasks
{
public class BuildUpdater
public class BuildUpdater : UpdaterBase
{
private readonly BuildRepository buildRepository;
private readonly UpdateStatusRepository updateStatusRepository;
private readonly ProjectRepository projectRepository;
private readonly RequestData requestData;
private readonly ILogger<BuildUpdater> logger;
private bool IsRunning;


public BuildUpdater(BuildRepository buildRepository, RequestData requestData, ProjectRepository projectRepository, UpdateStatusRepository updateStatusRepository, ILogger<BuildUpdater> logger)
:base(logger)
{
this.buildRepository = buildRepository;
this.requestData = requestData;
this.projectRepository = projectRepository;
this.updateStatusRepository = updateStatusRepository;
this.logger = logger;
}

[AutomaticRetry(Attempts = 0)]
public void Update()
protected override void Update()
{
if (IsRunning)
return;
IsRunning = true;
try
DateTime yesterday = DateTime.Now.Date.AddDays(-1);
var builds = new ConcurrentStack<TFSAdvanced.Updater.Models.Builds.Build>();
Parallel.ForEach(projectRepository.GetAll(), new ParallelOptions {MaxDegreeOfParallelism = AppSettings.MAX_DEGREE_OF_PARALLELISM}, project =>
{

DateTime yesterday = DateTime.Now.Date.AddDays(-1);
var builds = new ConcurrentStack<TFSAdvanced.Updater.Models.Builds.Build>();
Parallel.ForEach(projectRepository.GetAll(), new ParallelOptions {MaxDegreeOfParallelism = AppSettings.MAX_DEGREE_OF_PARALLELISM}, project =>
// Finished PR builds
var projectBuilds = GetAsync.FetchResponseList<TFSAdvanced.Updater.Models.Builds.Build>(requestData, $"{requestData.BaseAddress}/{project.Name}/_apis/build/builds?api-version=2.2&reasonFilter=validateShelveset&minFinishTime={yesterday:O}").Result;
if (projectBuilds != null && projectBuilds.Any())
{
// Finished PR builds
var projectBuilds = GetAsync.FetchResponseList<TFSAdvanced.Updater.Models.Builds.Build>(requestData, $"{requestData.BaseAddress}/{project.Name}/_apis/build/builds?api-version=2.2&reasonFilter=validateShelveset&minFinishTime={yesterday:O}").Result;
if (projectBuilds != null && projectBuilds.Any())
{
builds.PushRange(projectBuilds.ToArray());
}
builds.PushRange(projectBuilds.ToArray());
}


// Current active builds
projectBuilds = GetAsync.FetchResponseList<TFSAdvanced.Updater.Models.Builds.Build>(requestData, $"{requestData.BaseAddress}/{project.Name}/_apis/build/builds?api-version=2.2&statusFilter=inProgress&inProgress=notStarted").Result;
if (projectBuilds != null && projectBuilds.Any())
{
builds.PushRange(projectBuilds.ToArray());
}
// Current active builds
projectBuilds = GetAsync.FetchResponseList<TFSAdvanced.Updater.Models.Builds.Build>(requestData, $"{requestData.BaseAddress}/{project.Name}/_apis/build/builds?api-version=2.2&statusFilter=inProgress&inProgress=notStarted").Result;
if (projectBuilds != null && projectBuilds.Any())
{
builds.PushRange(projectBuilds.ToArray());
}


DateTime twoHoursAgo = DateTime.Now.AddHours(-2);
// Because we want to capture the final state of any build that was running and just finished we are getting those too
// Finished builds within the last 2 hours
projectBuilds = GetAsync.FetchResponseList<TFSAdvanced.Updater.Models.Builds.Build>(requestData, $"{requestData.BaseAddress}/{project.Name}/_apis/build/builds?api-version=2.2&minFinishTime={twoHoursAgo:O}").Result;
if (projectBuilds != null && projectBuilds.Any())
{
builds.PushRange(projectBuilds.ToArray());
}

});
DateTime twoHoursAgo = DateTime.Now.AddHours(-2);
// Because we want to capture the final state of any build that was running and just finished we are getting those too
// Finished builds within the last 2 hours
projectBuilds = GetAsync.FetchResponseList<TFSAdvanced.Updater.Models.Builds.Build>(requestData, $"{requestData.BaseAddress}/{project.Name}/_apis/build/builds?api-version=2.2&minFinishTime={twoHoursAgo:O}").Result;
if (projectBuilds != null && projectBuilds.Any())
{
builds.PushRange(projectBuilds.ToArray());
}

});

// The builds must be requested without the filter because the only filter available is minFinishTime, which will filter out those that haven't finished yet
var buildLists = builds.ToList();
buildRepository.Update(buildLists.Select(CreateBuild));
updateStatusRepository.UpdateStatus(new UpdateStatus {LastUpdate = DateTime.Now, UpdatedRecords = buildLists.Count, UpdaterName = nameof(BuildUpdater)});

}
catch (Exception ex)
{
throw new InvalidOperationException("Error running build updater", ex);
}
finally
{
IsRunning = false;
}
// The builds must be requested without the filter because the only filter available is minFinishTime, which will filter out those that haven't finished yet
var buildLists = builds.ToList();
buildRepository.Update(buildLists.Select(CreateBuild));
updateStatusRepository.UpdateStatus(new UpdateStatus {LastUpdate = DateTime.Now, UpdatedRecords = buildLists.Count, UpdaterName = nameof(BuildUpdater)});
}

private Build CreateBuild(TFSAdvanced.Updater.Models.Builds.Build build)
Expand Down
Loading

0 comments on commit 79c1a4f

Please sign in to comment.