Skip to content

Commit

Permalink
ran dotnet format
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Mar 21, 2024
1 parent dcee94a commit a4f6847
Show file tree
Hide file tree
Showing 38 changed files with 3,699 additions and 3,750 deletions.
41 changes: 19 additions & 22 deletions samples/Foundatio.SampleJob/Extensions/NumberExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
using System;
namespace Foundatio.Extensions;

namespace Foundatio.Extensions
internal static class NumericExtensions
{
internal static class NumericExtensions
public static string ToOrdinal(this int num)
{
public static string ToOrdinal(this int num)
switch (num % 100)
{
switch (num % 100)
{
case 11:
case 12:
case 13:
return num.ToString("#,###0") + "th";
}
case 11:
case 12:
case 13:
return num.ToString("#,###0") + "th";
}

switch (num % 10)
{
case 1:
return num.ToString("#,###0") + "st";
case 2:
return num.ToString("#,###0") + "nd";
case 3:
return num.ToString("#,###0") + "rd";
default:
return num.ToString("#,###0") + "th";
}
switch (num % 10)
{
case 1:
return num.ToString("#,###0") + "st";
case 2:
return num.ToString("#,###0") + "nd";
case 3:
return num.ToString("#,###0") + "rd";
default:
return num.ToString("#,###0") + "th";
}
}
}
26 changes: 12 additions & 14 deletions samples/Foundatio.SampleJob/Extensions/TaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using System;
using System.Diagnostics;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace Foundatio.Extensions
namespace Foundatio.Extensions;

internal static class TaskExtensions
{
internal static class TaskExtensions
[DebuggerStepThrough]
public static ConfiguredTaskAwaitable<TResult> AnyContext<TResult>(this Task<TResult> task)
{
[DebuggerStepThrough]
public static ConfiguredTaskAwaitable<TResult> AnyContext<TResult>(this Task<TResult> task)
{
return task.ConfigureAwait(continueOnCapturedContext: false);
}
return task.ConfigureAwait(continueOnCapturedContext: false);
}

[DebuggerStepThrough]
public static ConfiguredTaskAwaitable AnyContext(this Task task)
{
return task.ConfigureAwait(continueOnCapturedContext: false);
}
[DebuggerStepThrough]
public static ConfiguredTaskAwaitable AnyContext(this Task task)
{
return task.ConfigureAwait(continueOnCapturedContext: false);
}
}
65 changes: 32 additions & 33 deletions samples/Foundatio.SampleJob/PingQueueJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,46 @@
using Foundatio.Utility;
using Microsoft.Extensions.Logging;

namespace Foundatio.SampleJob
namespace Foundatio.SampleJob;

public class PingQueueJob : QueueJobBase<PingRequest>
{
public class PingQueueJob : QueueJobBase<PingRequest>
{
private readonly ILockProvider _locker;
private int _runCount;
private readonly ILockProvider _locker;
private int _runCount;

public PingQueueJob(IQueue<PingRequest> queue, ILoggerFactory loggerFactory, ICacheClient cacheClient, IMessageBus messageBus) : base(queue, loggerFactory)
{
AutoComplete = true;
_locker = new CacheLockProvider(cacheClient, messageBus, loggerFactory);
}
public PingQueueJob(IQueue<PingRequest> queue, ILoggerFactory loggerFactory, ICacheClient cacheClient, IMessageBus messageBus) : base(queue, loggerFactory)
{
AutoComplete = true;
_locker = new CacheLockProvider(cacheClient, messageBus, loggerFactory);
}

public int RunCount => _runCount;
public int RunCount => _runCount;

protected override Task<ILock> GetQueueEntryLockAsync(IQueueEntry<PingRequest> queueEntry, CancellationToken cancellationToken = new CancellationToken())
{
return _locker.AcquireAsync(String.Concat("pull:", queueEntry.Value.Id),
TimeSpan.FromMinutes(30),
TimeSpan.FromSeconds(1));
}
protected override Task<ILock> GetQueueEntryLockAsync(IQueueEntry<PingRequest> queueEntry, CancellationToken cancellationToken = new CancellationToken())
{
return _locker.AcquireAsync(String.Concat("pull:", queueEntry.Value.Id),
TimeSpan.FromMinutes(30),
TimeSpan.FromSeconds(1));
}

protected override async Task<JobResult> ProcessQueueEntryAsync(QueueEntryContext<PingRequest> context)
{
Interlocked.Increment(ref _runCount);
protected override async Task<JobResult> ProcessQueueEntryAsync(QueueEntryContext<PingRequest> context)
{
Interlocked.Increment(ref _runCount);

if (_logger.IsEnabled(LogLevel.Information))
_logger.LogInformation("Got {RunCount} ping. Sending pong!", RunCount.ToOrdinal());
await SystemClock.SleepAsync(TimeSpan.FromMilliseconds(1)).AnyContext();
if (_logger.IsEnabled(LogLevel.Information))
_logger.LogInformation("Got {RunCount} ping. Sending pong!", RunCount.ToOrdinal());
await SystemClock.SleepAsync(TimeSpan.FromMilliseconds(1)).AnyContext();

if (RandomData.GetBool(context.QueueEntry.Value.PercentChanceOfException))
throw new ApplicationException("Boom!");
if (RandomData.GetBool(context.QueueEntry.Value.PercentChanceOfException))
throw new ApplicationException("Boom!");

return JobResult.Success;
}
return JobResult.Success;
}
}

public class PingRequest
{
public string Data { get; set; }
public string Id { get; set; }
public int PercentChanceOfException { get; set; } = 0;
}
public class PingRequest
{
public string Data { get; set; }
public string Id { get; set; }
public int PercentChanceOfException { get; set; } = 0;
}
45 changes: 21 additions & 24 deletions samples/Foundatio.SampleJob/Program.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
using System;
using System.Threading.Tasks;
using Foundatio.Jobs;
using Foundatio.Jobs;
using Foundatio.Messaging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Foundatio.SampleJob
{
public class Program
{
private static ILogger _logger;
namespace Foundatio.SampleJob;

public static int Main()
{
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
_logger = loggerFactory.CreateLogger("MessageBus");
public class Program
{
private static ILogger _logger;

var serviceProvider = SampleServiceProvider.Create(loggerFactory);
var jobOptions = JobOptions.GetDefaults<PingQueueJob>(() => serviceProvider.GetRequiredService<PingQueueJob>());
var messageBus = serviceProvider.GetRequiredService<IMessageBus>();
messageBus.SubscribeAsync<EchoMessage>(m => HandleEchoMessage(m)).GetAwaiter().GetResult();
return new JobRunner(jobOptions).RunInConsoleAsync().GetAwaiter().GetResult();
}
public static int Main()
{
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
_logger = loggerFactory.CreateLogger("MessageBus");

private static void HandleEchoMessage(EchoMessage m)
{
_logger.LogInformation($"Got message: {m.Message}");
}
var serviceProvider = SampleServiceProvider.Create(loggerFactory);
var jobOptions = JobOptions.GetDefaults<PingQueueJob>(() => serviceProvider.GetRequiredService<PingQueueJob>());
var messageBus = serviceProvider.GetRequiredService<IMessageBus>();
messageBus.SubscribeAsync<EchoMessage>(m => HandleEchoMessage(m)).GetAwaiter().GetResult();
return new JobRunner(jobOptions).RunInConsoleAsync().GetAwaiter().GetResult();
}

public class EchoMessage
private static void HandleEchoMessage(EchoMessage m)
{
public string Message { get; set; }
_logger.LogInformation($"Got message: {m.Message}");
}
}

public class EchoMessage
{
public string Message { get; set; }
}
38 changes: 18 additions & 20 deletions samples/Foundatio.SampleJob/SampleServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,33 @@
using Foundatio.Caching;
using Foundatio.Lock;
using Foundatio.Messaging;
using Foundatio.Metrics;
using Foundatio.Queues;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;

namespace Foundatio.SampleJob
namespace Foundatio.SampleJob;

public class SampleServiceProvider
{
public class SampleServiceProvider
public static IServiceProvider Create(ILoggerFactory loggerFactory)
{
public static IServiceProvider Create(ILoggerFactory loggerFactory)
{
var container = new ServiceCollection();
var container = new ServiceCollection();

if (loggerFactory != null)
{
container.AddSingleton<ILoggerFactory>(loggerFactory);
container.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
}
if (loggerFactory != null)
{
container.AddSingleton<ILoggerFactory>(loggerFactory);
container.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
}

var muxer = ConnectionMultiplexer.Connect("localhost", o => o.LoggerFactory = loggerFactory);
container.AddSingleton(muxer);
container.AddSingleton<IQueue<PingRequest>>(s => new RedisQueue<PingRequest>(o => o.ConnectionMultiplexer(muxer).RetryDelay(TimeSpan.FromSeconds(1)).WorkItemTimeout(TimeSpan.FromSeconds(5)).LoggerFactory(loggerFactory)));
container.AddSingleton<ICacheClient>(s => new RedisCacheClient(o => o.ConnectionMultiplexer(muxer).LoggerFactory(loggerFactory)));
container.AddSingleton<IMessageBus>(s => new RedisMessageBus(o => o.Subscriber(muxer.GetSubscriber()).LoggerFactory(loggerFactory).MapMessageTypeToClassName<EchoMessage>()));
container.AddSingleton<ILockProvider>(s => new CacheLockProvider(s.GetRequiredService<ICacheClient>(), s.GetRequiredService<IMessageBus>(), loggerFactory));
container.AddTransient<PingQueueJob>();
var muxer = ConnectionMultiplexer.Connect("localhost", o => o.LoggerFactory = loggerFactory);
container.AddSingleton(muxer);
container.AddSingleton<IQueue<PingRequest>>(s => new RedisQueue<PingRequest>(o => o.ConnectionMultiplexer(muxer).RetryDelay(TimeSpan.FromSeconds(1)).WorkItemTimeout(TimeSpan.FromSeconds(5)).LoggerFactory(loggerFactory)));
container.AddSingleton<ICacheClient>(s => new RedisCacheClient(o => o.ConnectionMultiplexer(muxer).LoggerFactory(loggerFactory)));
container.AddSingleton<IMessageBus>(s => new RedisMessageBus(o => o.Subscriber(muxer.GetSubscriber()).LoggerFactory(loggerFactory).MapMessageTypeToClassName<EchoMessage>()));
container.AddSingleton<ILockProvider>(s => new CacheLockProvider(s.GetRequiredService<ICacheClient>(), s.GetRequiredService<IMessageBus>(), loggerFactory));
container.AddTransient<PingQueueJob>();

return container.BuildServiceProvider();
}
return container.BuildServiceProvider();
}
}
Loading

0 comments on commit a4f6847

Please sign in to comment.