Skip to content

Commit

Permalink
Update code to use C# 7 features;
Browse files Browse the repository at this point in the history
Add explicit validations for connection/transaction to implement JobStorageConnection/JobStorageTransaction;
Update unit tests
  • Loading branch information
pieceofsummer committed Jun 1, 2018
1 parent 08d90df commit 2d338f3
Show file tree
Hide file tree
Showing 28 changed files with 146 additions and 145 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.3.10-{build}
version: 1.4.0-{build}

os: Visual Studio 2017

Expand Down
8 changes: 2 additions & 6 deletions src/Hangfire.Console/Dashboard/ConsoleDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ internal class ConsoleDispatcher : IDashboardDispatcher

public ConsoleDispatcher(ConsoleOptions options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));

_options = options;
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public Task Dispatch(DashboardContext context)
Expand All @@ -32,8 +29,7 @@ public Task Dispatch(DashboardContext context)
var startArg = context.Request.GetQuery("start");

// try to parse offset at which we should start returning requests
int start;
if (string.IsNullOrEmpty(startArg) || !int.TryParse(startArg, out start))
if (string.IsNullOrEmpty(startArg) || !int.TryParse(startArg, out var start))
{
// if not provided or invalid, fetch records from the very start
start = 0;
Expand Down
6 changes: 2 additions & 4 deletions src/Hangfire.Console/Dashboard/ConsoleRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Hangfire.Console.Storage;
using Hangfire.Dashboard;
using Hangfire.States;
using Hangfire.Storage;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -52,7 +51,7 @@ public static void RenderText(StringBuilder buffer, string text)
buffer.Append(Helper.HtmlEncode(text.Substring(start, m.Index - start)));
}

string schema = "";
var schema = "";
if (!m.Groups["schema"].Success)
{
// force schema for links without one (like www.google.com)
Expand Down Expand Up @@ -192,8 +191,7 @@ private static IEnumerable<ConsoleLine> ReadLines(IConsoleStorage storage, Conso

if (progressBars != null)
{
ConsoleLine prev;
if (progressBars.TryGetValue(entry.Message, out prev))
if (progressBars.TryGetValue(entry.Message, out var prev))
{
prev.ProgressValue = entry.ProgressValue;
prev.TextColor = entry.TextColor;
Expand Down
5 changes: 1 addition & 4 deletions src/Hangfire.Console/Dashboard/DynamicCssDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ internal class DynamicCssDispatcher : IDashboardDispatcher

public DynamicCssDispatcher(ConsoleOptions options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));

_options = options;
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public Task Dispatch(DashboardContext context)
Expand Down
13 changes: 5 additions & 8 deletions src/Hangfire.Console/Dashboard/DynamicJsDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ internal class DynamicJsDispatcher : IDashboardDispatcher

public DynamicJsDispatcher(ConsoleOptions options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));

_options = options;
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public Task Dispatch(DashboardContext context)
{
var builder = new StringBuilder();

builder.Append(@"(function (hangFire) {")
.Append("hangFire.config = hangFire.config || {};")
.AppendFormat("hangFire.config.consolePollInterval = {0};", _options.PollInterval)
.AppendFormat("hangFire.config.consolePollUrl = '{0}/console/';", context.Request.PathBase)
builder.Append(@"(function (hangfire) {")
.Append("hangfire.config = hangfire.config || {};")
.AppendFormat("hangfire.config.consolePollInterval = {0};", _options.PollInterval)
.AppendFormat("hangfire.config.consolePollUrl = '{0}/console/';", context.Request.PathBase)
.Append("})(window.Hangfire = window.Hangfire || {});")
.AppendLine();

Expand Down
6 changes: 2 additions & 4 deletions src/Hangfire.Console/Dashboard/ProcessingStateRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ namespace Hangfire.Console.Dashboard
/// </summary>
internal class ProcessingStateRenderer
{
// ReSharper disable once NotAccessedField.Local
private readonly ConsoleOptions _options;

public ProcessingStateRenderer(ConsoleOptions options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));

_options = options;
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public NonEscapedString Render(HtmlHelper helper, IDictionary<string, string> stateData)
Expand Down
12 changes: 6 additions & 6 deletions src/Hangfire.Console/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public static class EnumerableExtensions
/// <param name="count">Item count</param>
public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, IProgressBar progressBar, int count = -1)
{
if (enumerable is ICollection<T>)
if (enumerable is ICollection<T> collection)
{
count = ((ICollection<T>)enumerable).Count;
count = collection.Count;
}
else if (enumerable is IReadOnlyCollection<T>)
else if (enumerable is IReadOnlyCollection<T> readOnlyCollection)
{
count = ((IReadOnlyCollection<T>)enumerable).Count;
count = readOnlyCollection.Count;
}
else if (count < 0)
{
Expand All @@ -44,9 +44,9 @@ public static IEnumerable<T> WithProgress<T>(this IEnumerable<T> enumerable, IPr
/// <param name="count">Item count</param>
public static IEnumerable WithProgress(this IEnumerable enumerable, IProgressBar progressBar, int count = -1)
{
if (enumerable is ICollection)
if (enumerable is ICollection collection)
{
count = ((ICollection)enumerable).Count;
count = collection.Count;
}
else if (count < 0)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Hangfire.Console/Monitoring/ConsoleApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public IList<LineDto> GetLines(string jobId, DateTime timestamp, LineType type =

if (progressBars != null)
{
ProgressBarDto prev;
if (progressBars.TryGetValue(entry.Message, out prev))
if (progressBars.TryGetValue(entry.Message, out var prev))
{
prev.Progress = entry.ProgressValue.Value;
prev.Color = entry.TextColor;
Expand Down
7 changes: 7 additions & 0 deletions src/Hangfire.Console/Monitoring/ProgressBarDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class ProgressBarDto : LineDto
internal ProgressBarDto(ConsoleLine line, DateTime referenceTimestamp) : base(line, referenceTimestamp)
{
Id = int.Parse(line.Message, CultureInfo.InvariantCulture);
Name = line.ProgressName;
// ReSharper disable once PossibleInvalidOperationException
Progress = line.ProgressValue.Value;
}

Expand All @@ -23,6 +25,11 @@ internal ProgressBarDto(ConsoleLine line, DateTime referenceTimestamp) : base(li
/// </summary>
public int Id { get; }

/// <summary>
/// Returns optional name for a progress bar
/// </summary>
public string Name { get; }

/// <summary>
/// Returns progress value for a progress bar
/// </summary>
Expand Down
5 changes: 2 additions & 3 deletions src/Hangfire.Console/Progress/DefaultProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ internal class DefaultProgressBar : IProgressBar

internal DefaultProgressBar(ConsoleContext context, string progressBarId, string name, string color)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
if (string.IsNullOrEmpty(progressBarId))
throw new ArgumentNullException(nameof(progressBarId));

_context = context;
_context = context ?? throw new ArgumentNullException(nameof(context));
_progressBarId = progressBarId;
_name = name;
_color = color;
Expand All @@ -42,6 +40,7 @@ public void SetValue(double value)
if (value < 0 || value > 100)
throw new ArgumentOutOfRangeException(nameof(value), "Value should be in range 0..100");

// ReSharper disable once CompareOfFloatsByEqualityOperator
if (Interlocked.Exchange(ref _value, value) == value) return;

_context.AddLine(new ConsoleLine() { Message = _progressBarId, ProgressName = _name, ProgressValue = value, TextColor = _color });
Expand Down
7 changes: 1 addition & 6 deletions src/Hangfire.Console/Progress/IProgressBar.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Hangfire.Console.Progress
namespace Hangfire.Console.Progress
{
/// <summary>
/// Progress bar line inside console.
Expand Down
16 changes: 4 additions & 12 deletions src/Hangfire.Console/Progress/ProgressEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ internal class ProgressEnumerable : IEnumerable

public ProgressEnumerable(IEnumerable enumerable, IProgressBar progressBar, int count)
{
if (enumerable == null)
throw new ArgumentNullException(nameof(enumerable));
if (progressBar == null)
throw new ArgumentNullException(nameof(progressBar));
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));

_enumerable = enumerable;
_progressBar = progressBar;
_enumerable = enumerable ?? throw new ArgumentNullException(nameof(enumerable));
_progressBar = progressBar ?? throw new ArgumentNullException(nameof(progressBar));
_count = count;
}

Expand Down Expand Up @@ -98,15 +94,11 @@ internal class ProgressEnumerable<T> : IEnumerable<T>

public ProgressEnumerable(IEnumerable<T> enumerable, IProgressBar progressBar, int count)
{
if (enumerable == null)
throw new ArgumentNullException(nameof(enumerable));
if (progressBar == null)
throw new ArgumentNullException(nameof(progressBar));
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));

_enumerable = enumerable;
_progressBar = progressBar;
_enumerable = enumerable ?? throw new ArgumentNullException(nameof(enumerable));
_progressBar = progressBar ?? throw new ArgumentNullException(nameof(progressBar));
_count = count;
}

Expand Down
23 changes: 7 additions & 16 deletions src/Hangfire.Console/Serialization/ConsoleId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class ConsoleId : IEquatable<ConsoleId>
{
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

private string _cachedString = null;
private string _cachedString;

/// <summary>
/// Job identifier
Expand Down Expand Up @@ -68,24 +68,21 @@ public static ConsoleId Parse(string value)
// Timestamp is serialized in reverse order for better randomness!

long timestamp = 0;
for (int i = 10; i >= 0; i--)
for (var i = 10; i >= 0; i--)
{
var c = value[i] | 0x20;

var x = (c >= '0' && c <= '9') ? (c - '0') : (c >= 'a' && c <= 'f') ? (c - 'a' + 10) : -1;
if (x == -1)
throw new ArgumentException("Invalid value", nameof(value));

timestamp = (timestamp << 4) | (long)x;
timestamp = (timestamp << 4) + x;
}

return new ConsoleId(value.Substring(11), timestamp) { _cachedString = value };
}

/// <summary>
/// Determines if this instance is equal to <paramref name="other"/> instance.
/// </summary>
/// <param name="other">Other instance</param>
/// <inheritdoc />
public bool Equals(ConsoleId other)
{
if (ReferenceEquals(other, null)) return false;
Expand All @@ -103,7 +100,7 @@ public override string ToString()
var buffer = new char[11 + JobId.Length];

var timestamp = Timestamp;
for (int i = 0; i < 11; i++, timestamp >>= 4)
for (var i = 0; i < 11; i++, timestamp >>= 4)
{
var c = timestamp & 0x0F;
buffer[i] = (c < 10) ? (char)(c + '0') : (char)(c - 10 + 'a');
Expand All @@ -118,15 +115,9 @@ public override string ToString()
}

/// <inheritdoc />
public override bool Equals(object obj)
{
return Equals(obj as ConsoleId);
}
public override bool Equals(object obj) => Equals(obj as ConsoleId);

/// <inheritdoc />
public override int GetHashCode()
{
return (JobId.GetHashCode() * 17) ^ Timestamp.GetHashCode();
}
public override int GetHashCode() => (JobId.GetHashCode() * 17) ^ Timestamp.GetHashCode();
}
}
9 changes: 1 addition & 8 deletions src/Hangfire.Console/Serialization/ConsoleLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,11 @@ internal class ConsoleLine
[JsonProperty("s", Required = Required.Always)]
public string Message { get; set; }

[JsonIgnore]
private string _textColor;

/// <summary>
/// Text color for this message
/// </summary>
[JsonProperty("c", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string TextColor
{
get { return _textColor; }
set { _textColor = string.IsNullOrEmpty(value) ? null : value; }
}
public string TextColor { get; set; }

/// <summary>
/// Value update for a progress bar
Expand Down
13 changes: 4 additions & 9 deletions src/Hangfire.Console/Server/ConsoleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ public static ConsoleContext FromPerformContext(PerformContext context)

public ConsoleContext(ConsoleId consoleId, IConsoleStorage storage)
{
if (consoleId == null)
throw new ArgumentNullException(nameof(consoleId));
if (storage == null)
throw new ArgumentNullException(nameof(storage));

_consoleId = consoleId;
_storage = storage;
_consoleId = consoleId ?? throw new ArgumentNullException(nameof(consoleId));
_storage = storage ?? throw new ArgumentNullException(nameof(storage));

_lastTimeOffset = 0;
_nextProgressBarId = 0;
Expand Down Expand Up @@ -87,15 +82,15 @@ public IProgressBar WriteProgressBar(string name, double value, ConsoleTextColor

return progressBar;
}

public void Expire(TimeSpan expireIn)
{
_storage.Expire(_consoleId, expireIn);
}

public void FixExpiration()
{
TimeSpan ttl = _storage.GetConsoleTtl(_consoleId);
var ttl = _storage.GetConsoleTtl(_consoleId);
if (ttl <= TimeSpan.Zero)
{
// ConsoleApplyStateFilter not called yet, or current job state is not final.
Expand Down
5 changes: 1 addition & 4 deletions src/Hangfire.Console/Server/ConsoleServerFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ internal class ConsoleServerFilter : IServerFilter

public ConsoleServerFilter(ConsoleOptions options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));

_options = options;
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public void OnPerforming(PerformingContext filterContext)
Expand Down
5 changes: 1 addition & 4 deletions src/Hangfire.Console/States/ConsoleApplyStateFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ internal class ConsoleApplyStateFilter : IApplyStateFilter

public ConsoleApplyStateFilter(ConsoleOptions options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));

_options = options;
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
Expand Down
5 changes: 1 addition & 4 deletions src/Hangfire.Console/Storage/ConsoleExpirationTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ internal class ConsoleExpirationTransaction : IConsoleExpirationTransaction

public ConsoleExpirationTransaction(JobStorageTransaction transaction)
{
if (transaction == null)
throw new ArgumentNullException(nameof(transaction));

_transaction = transaction;
_transaction = transaction ?? throw new ArgumentNullException(nameof(transaction));
}

public void Dispose()
Expand Down
Loading

0 comments on commit 2d338f3

Please sign in to comment.