Skip to content

Commit

Permalink
Merge branch 'cbw-1249/add-observability' into cbw-1261/sc-listing-en…
Browse files Browse the repository at this point in the history
…dpoints
  • Loading branch information
schwartz-concordium committed Sep 1, 2023
2 parents 09d7102 + bc4440a commit 0faea97
Show file tree
Hide file tree
Showing 68 changed files with 577 additions and 596 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
using System.Threading;
using System.Threading.Tasks;
using Application.Aggregates.SmartContract.Entities;
using Application.Aggregates.SmartContract.Jobs;
using Application.Aggregates.Contract.Entities;
using Application.Aggregates.Contract.Jobs;
using Application.Api.GraphQL.EfCore;
using Application.Common.FeatureFlags;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;

namespace Application.Aggregates.SmartContract.BackgroundServices;
namespace Application.Aggregates.Contract.BackgroundServices;

/// <summary>
/// Background service which executes background jobs related to Smart Contracts.
/// Background service which executes background jobs related to Contracts.
///
/// When new jobs are added they should be dependency injected and added to the constructor of this service.
/// </summary>
internal sealed class SmartContractJobsBackgroundService : BackgroundService
internal sealed class ContractJobsBackgroundService : BackgroundService
{
private readonly ISmartContractJobFinder _jobFinder;
private readonly IContractJobFinder _jobFinder;
private readonly IFeatureFlags _featureFlags;
private readonly IDbContextFactory<GraphQlDbContext> _dbContextFactory;
private readonly ILogger _logger;

public SmartContractJobsBackgroundService(
ISmartContractJobFinder jobFinder,
public ContractJobsBackgroundService(
IContractJobFinder jobFinder,
IFeatureFlags featureFlags,
IDbContextFactory<GraphQlDbContext> dbContextFactory
)
{
_jobFinder = jobFinder;
_featureFlags = featureFlags;
_dbContextFactory = dbContextFactory;
_logger = Log.ForContext<SmartContractJobsBackgroundService>();
_logger = Log.ForContext<ContractJobsBackgroundService>();
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Expand All @@ -47,15 +47,15 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.WhenAll(jobs.Select(j => RunJob(j, stoppingToken)));

_logger.Information($"{nameof(SmartContractJobsBackgroundService)} done.");
_logger.Information($"{nameof(ContractJobsBackgroundService)} done.");
}
catch (Exception e)
{
_logger.Error(e, $"{nameof(SmartContractJobsBackgroundService)} didn't succeed successfully due to exception.");
_logger.Error(e, $"{nameof(ContractJobsBackgroundService)} didn't succeed successfully due to exception.");
}
}

private async Task RunJob(ISmartContractJob job, CancellationToken token)
private async Task RunJob(IContractJob job, CancellationToken token)
{
try
{
Expand All @@ -76,21 +76,21 @@ private async Task RunJob(ISmartContractJob job, CancellationToken token)
}
}

internal async Task<bool> DoesExistingJobExist(ISmartContractJob job, CancellationToken token = default)
internal async Task<bool> DoesExistingJobExist(IContractJob job, CancellationToken token = default)
{
await using var context = await _dbContextFactory.CreateDbContextAsync(token);
var existingJob = await context.SmartContractJobs
var existingJob = await context.ContractJobs
.AsNoTracking()
.Where(j => j.Job == job.GetUniqueIdentifier())
.FirstOrDefaultAsync(token);

return existingJob != null;
}

internal async Task SaveSuccessfullyExecutedJob(ISmartContractJob job, CancellationToken token = default)
internal async Task SaveSuccessfullyExecutedJob(IContractJob job, CancellationToken token = default)
{
await using var context = await _dbContextFactory.CreateDbContextAsync(token);
await context.SmartContractJobs.AddAsync(new SmartContractJob(job.GetUniqueIdentifier()), token);
await context.ContractJobs.AddAsync(new ContractJob(job.GetUniqueIdentifier()), token);
await context.SaveChangesAsync(token);
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
using System.Threading;
using System.Threading.Tasks;
using Application.Aggregates.SmartContract.Configurations;
using Application.Aggregates.SmartContract.Jobs;
using Application.Aggregates.SmartContract.Observability;
using Application.Aggregates.Contract.Configurations;
using Application.Aggregates.Contract.Jobs;
using Application.Aggregates.Contract.Observability;
using Application.Api.GraphQL.EfCore;
using Application.Common.FeatureFlags;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace Application.Aggregates.SmartContract.BackgroundServices;
namespace Application.Aggregates.Contract.BackgroundServices;

/// <summary>
/// Background service which starts smart data import data from nodes.
/// Background service which starts contract data import data from nodes.
/// </summary>
internal class SmartContractNodeImportBackgroundService : BackgroundService
internal class ContractNodeImportBackgroundService : BackgroundService
{
private readonly ISmartContractJobFinder _jobFinder;
private readonly IContractJobFinder _jobFinder;
private readonly IDbContextFactory<GraphQlDbContext> _dbContextFactory;
private readonly ISmartContractRepositoryFactory _repositoryFactory;
private readonly ISmartContractNodeClient _client;
private readonly IContractRepositoryFactory _repositoryFactory;
private readonly IContractNodeClient _client;
private readonly IFeatureFlags _featureFlags;
private readonly SmartContractHealthCheck _healthCheck;
private readonly SmartContractAggregateOptions _options;
private readonly ContractHealthCheck _healthCheck;
private readonly ContractAggregateOptions _options;
private readonly ILogger _logger;

public SmartContractNodeImportBackgroundService(
ISmartContractJobFinder jobFinder,
public ContractNodeImportBackgroundService(
IContractJobFinder jobFinder,
IDbContextFactory<GraphQlDbContext> dbContextFactory,
ISmartContractRepositoryFactory repositoryFactory,
ISmartContractNodeClient client,
IContractRepositoryFactory repositoryFactory,
IContractNodeClient client,
IFeatureFlags featureFlags,
IOptions<SmartContractAggregateOptions> options,
SmartContractHealthCheck healthCheck
)
IOptions<ContractAggregateOptions> options,
ContractHealthCheck healthCheck)
{
_jobFinder = jobFinder;
_dbContextFactory = dbContextFactory;
Expand All @@ -42,7 +41,7 @@ SmartContractHealthCheck healthCheck
_featureFlags = featureFlags;
_healthCheck = healthCheck;
_options = options.Value;
_logger = Log.ForContext<SmartContractNodeImportBackgroundService>();
_logger = Log.ForContext<ContractNodeImportBackgroundService>();
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Expand All @@ -57,15 +56,16 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await AwaitJobsAsync(stoppingToken);

var smartContractAggregate = new SmartContractAggregate(_repositoryFactory, _options);
var contractAggregate = new ContractAggregate(_repositoryFactory, _options);

_logger.Information($"{nameof(SmartContractNodeImportBackgroundService)} started.");
await smartContractAggregate.NodeImportJob(_client, stoppingToken);
_logger.Information($"{nameof(ContractNodeImportBackgroundService)} started.");
await contractAggregate.NodeImportJob(_client, stoppingToken);
}
catch (Exception e)
{
_logger.Fatal(e, $"{nameof(SmartContractNodeImportBackgroundService)} stopped due to exception.");
_healthCheck.AddUnhealthyJobWithMessage(nameof(SmartContractNodeImportBackgroundService), "Stopped due to exception.");
_logger.Fatal(e, $"{nameof(ContractNodeImportBackgroundService)} stopped due to exception.");
_healthCheck.AddUnhealthyJobWithMessage(nameof(ContractNodeImportBackgroundService), "Stopped due to exception.");
_logger.Fatal(e, $"{nameof(ContractNodeImportBackgroundService)} stopped due to exception.");
}
}

Expand All @@ -90,17 +90,17 @@ private async Task AwaitJobsAsync(CancellationToken token = default)

internal async Task<IList<string>> GetJobsToAwait(CancellationToken token = default)
{
var smartContractJobs = _jobFinder.GetJobs()
var contractJobs = _jobFinder.GetJobs()
.Select(j => j.GetUniqueIdentifier())
.ToList();
await using var context = await _dbContextFactory.CreateDbContextAsync(token);
var doneJobs = await context
.SmartContractJobs
.ContractJobs
.AsNoTracking()
.Where(j => smartContractJobs.Contains(j.Job))
.Where(j => contractJobs.Contains(j.Job))
.Select(j => j.Job)
.ToListAsync(cancellationToken: token);

return smartContractJobs.Except(doneJobs).ToList();
return contractJobs.Except(doneJobs).ToList();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Application.Aggregates.SmartContract.Configurations;
namespace Application.Aggregates.Contract.Configurations;

public class SmartContractAggregateJobOptions
public class ContractAggregateJobOptions
{
/// <summary>
/// Number of tasks which should be used for parallelism.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
namespace Application.Aggregates.SmartContract.Configurations;
namespace Application.Aggregates.Contract.Configurations;

public class SmartContractAggregateOptions
public class ContractAggregateOptions
{
/// <summary>
/// Set options for jobs related to smart contracts.
/// Set options for jobs related to contracts.
///
/// Done as dictionary such that it can be changed from configurations. Key is unique identifier of job and
/// it defined within the jobs class.
/// </summary>
public IDictionary<string, SmartContractAggregateJobOptions> Jobs { get; set; } =
new Dictionary<string, SmartContractAggregateJobOptions>();
public IDictionary<string, ContractAggregateJobOptions> Jobs { get; set; } =
new Dictionary<string, ContractAggregateJobOptions>();
/// <summary>
/// Delay which is used by the node importer between validation if all jobs has succeeded.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Application.Aggregates.SmartContract.Configurations;
namespace Application.Aggregates.Contract.Configurations;

public sealed class SmartContractEntityTypeConfigurations : IEntityTypeConfiguration<Entities.SmartContract>
public sealed class ContractEntityTypeConfigurations : IEntityTypeConfiguration<Entities.Contract>
{
public void Configure(EntityTypeBuilder<Entities.SmartContract> builder)
public void Configure(EntityTypeBuilder<Entities.Contract> builder)
{
builder.ToTable("graphql_smart_contracts");
builder.ToTable("graphql_contracts");
builder.HasKey(x => new
{
x.ContractAddressIndex,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Application.Aggregates.SmartContract.Entities;
using Application.Aggregates.Contract.Entities;
using Application.Api.GraphQL.EfCore.Converters.EfCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Application.Aggregates.SmartContract.Configurations;
namespace Application.Aggregates.Contract.Configurations;

public sealed class SmartContractEventEntityTypeConfigurations : IEntityTypeConfiguration<SmartContractEvent>
public sealed class ContractEventEntityTypeConfigurations : IEntityTypeConfiguration<ContractEvent>
{
public void Configure(EntityTypeBuilder<SmartContractEvent> builder)
public void Configure(EntityTypeBuilder<ContractEvent> builder)
{
builder.ToTable("graphql_smart_contract_events");
builder.ToTable("graphql_contract_events");
builder.HasKey(x => new
{
x.BlockHeight,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Application.Aggregates.Contract.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Application.Aggregates.Contract.Configurations;

public sealed class ContractJobEntityTypeConfigurations : IEntityTypeConfiguration<ContractJob>
{
public void Configure(EntityTypeBuilder<ContractJob> builder)
{
builder.ToTable("graphql_contract_jobs");
builder.HasKey(x => x.Job);
builder.Property(x => x.Job)
.HasColumnName("job");
builder.Property(x => x.CreatedAt)
.HasColumnName("created_at");
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Application.Aggregates.SmartContract.Entities;
using Application.Aggregates.Contract.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Application.Aggregates.SmartContract.Configurations;
namespace Application.Aggregates.Contract.Configurations;

public sealed class SmartContractReadHeightEntityTypeConfigurations : IEntityTypeConfiguration<
SmartContractReadHeight>
public sealed class ContractReadHeightEntityTypeConfigurations : IEntityTypeConfiguration<
ContractReadHeight>
{
public void Configure(EntityTypeBuilder<SmartContractReadHeight> builder)
public void Configure(EntityTypeBuilder<ContractReadHeight> builder)
{
builder.ToTable("graphql_smart_contract_read_heights");
builder.ToTable("graphql_contract_read_heights");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id).HasColumnName("id").ValueGeneratedOnAdd();
builder.Property(x => x.BlockHeight)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Application.Aggregates.SmartContract.Entities;
using Application.Aggregates.Contract.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Application.Aggregates.SmartContract.Configurations;
namespace Application.Aggregates.Contract.Configurations;

public sealed class ModuleReferenceSmartContractLinkEventEntityTypeConfigurations : IEntityTypeConfiguration<ModuleReferenceSmartContractLinkEvent>
public sealed class ModuleReferenceContractLinkEventEntityTypeConfigurations : IEntityTypeConfiguration<ModuleReferenceContractLinkEvent>
{
public void Configure(EntityTypeBuilder<ModuleReferenceSmartContractLinkEvent> builder)
public void Configure(EntityTypeBuilder<ModuleReferenceContractLinkEvent> builder)
{
builder.ToTable("graphql_module_reference_smart_contract_link_events");
builder.ToTable("graphql_module_reference_contract_link_events");
builder.HasKey(x => new
{
x.BlockHeight,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Application.Aggregates.SmartContract.Entities;
using Application.Aggregates.Contract.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Application.Aggregates.SmartContract.Configurations;
namespace Application.Aggregates.Contract.Configurations;

public sealed class ModuleReferenceEventEntityTypeConfigurations : IEntityTypeConfiguration<ModuleReferenceEvent>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Application.Api.GraphQL.Transactions;
using Dapper;

namespace Application.Aggregates.SmartContract.Configurations;
namespace Application.Aggregates.Contract.Configurations;


public class TransactionResultEventHandler : SqlMapper.TypeHandler<TransactionResultEvent>
Expand Down
Loading

0 comments on commit 0faea97

Please sign in to comment.