Skip to content

Commit

Permalink
Apply suggestions in Rider and fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang authored and ragnarstolsmark committed Oct 18, 2023
1 parent c323934 commit f022b37
Show file tree
Hide file tree
Showing 23 changed files with 187 additions and 216 deletions.
9 changes: 5 additions & 4 deletions src/Hangfire.Console/ConsoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Hangfire.Console
/// <summary>
/// Provides extension methods for writing to console.
/// </summary>
[PublicAPI]
public static class ConsoleExtensions
{
/// <summary>
Expand Down Expand Up @@ -38,7 +39,7 @@ public static void ResetTextColor(this PerformContext context)

consoleContext.TextColor = null;
}

/// <summary>
/// Adds an updateable progress bar to console.
/// </summary>
Expand Down Expand Up @@ -71,7 +72,7 @@ public static void WriteLine(this PerformContext context, string value)
{
ConsoleContext.FromPerformContext(context)?.WriteLine(value, null);
}

/// <summary>
/// Adds a string to console.
/// </summary>
Expand All @@ -82,7 +83,7 @@ public static void WriteLine(this PerformContext context, ConsoleTextColor color
{
ConsoleContext.FromPerformContext(context)?.WriteLine(value, color);
}

/// <summary>
/// Adds an empty line to console.
/// </summary>
Expand Down Expand Up @@ -140,7 +141,7 @@ public static void WriteLine(this PerformContext context, string format, object
[StringFormatMethod("format")]
public static void WriteLine(this PerformContext context, string format, params object[] args)
=> WriteLine(context, string.Format(format, args));

/// <summary>
/// Adds a value to a console.
/// </summary>
Expand Down
7 changes: 3 additions & 4 deletions src/Hangfire.Console/ConsoleOptions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using JetBrains.Annotations;

namespace Hangfire.Console
{
/// <summary>
/// Configuration options for console.
/// </summary>
[PublicAPI]
public class ConsoleOptions
{
/// <summary>
Expand All @@ -17,7 +19,7 @@ public class ConsoleOptions
/// When set to <c>true</c>, <see cref="ExpireIn"/> parameter is ignored.
/// </summary>
public bool FollowJobRetentionPolicy { get; set; } = true;

/// <summary>
/// Gets or sets console poll interval (in ms).
/// </summary>
Expand All @@ -38,7 +40,6 @@ public class ConsoleOptions
/// </summary>
public string TimestampColor { get; set; } = "#00aad7";


internal void Validate(string paramName)
{
if (ExpireIn < TimeSpan.FromMinutes(1))
Expand All @@ -47,7 +48,5 @@ internal void Validate(string paramName)
if (PollInterval < 100)
throw new ArgumentException("PollInterval shouldn't be less than 100 ms", paramName);
}


}
}
41 changes: 22 additions & 19 deletions src/Hangfire.Console/ConsoleTextColor.cs
Original file line number Diff line number Diff line change
@@ -1,90 +1,93 @@
namespace Hangfire.Console
using JetBrains.Annotations;

namespace Hangfire.Console
{
/// <summary>
/// Text color values
/// </summary>
[PublicAPI]
public class ConsoleTextColor
{
/// <summary>
/// The color black.
/// </summary>
public static readonly ConsoleTextColor Black = new ConsoleTextColor("#000000");
public static readonly ConsoleTextColor Black = new("#000000");

/// <summary>
/// The color dark blue.
/// </summary>
public static readonly ConsoleTextColor DarkBlue = new ConsoleTextColor("#000080");
public static readonly ConsoleTextColor DarkBlue = new("#000080");

/// <summary>
/// The color dark green.
/// </summary>
public static readonly ConsoleTextColor DarkGreen = new ConsoleTextColor("#008000");
public static readonly ConsoleTextColor DarkGreen = new("#008000");

/// <summary>
/// The color dark cyan (dark blue-green).
/// </summary>
public static readonly ConsoleTextColor DarkCyan = new ConsoleTextColor("#008080");
public static readonly ConsoleTextColor DarkCyan = new("#008080");

/// <summary>
/// The color dark red.
/// </summary>
public static readonly ConsoleTextColor DarkRed = new ConsoleTextColor("#800000");
public static readonly ConsoleTextColor DarkRed = new("#800000");

/// <summary>
/// The color dark magenta (dark purplish-red).
/// </summary>
public static readonly ConsoleTextColor DarkMagenta = new ConsoleTextColor("#800080");
public static readonly ConsoleTextColor DarkMagenta = new("#800080");

/// <summary>
/// The color dark yellow (ochre).
/// </summary>
public static readonly ConsoleTextColor DarkYellow = new ConsoleTextColor("#808000");
public static readonly ConsoleTextColor DarkYellow = new("#808000");

/// <summary>
/// The color gray.
/// </summary>
public static readonly ConsoleTextColor Gray = new ConsoleTextColor("#c0c0c0");
public static readonly ConsoleTextColor Gray = new("#c0c0c0");

/// <summary>
/// The color dark gray.
/// </summary>
public static readonly ConsoleTextColor DarkGray = new ConsoleTextColor("#808080");
public static readonly ConsoleTextColor DarkGray = new("#808080");

/// <summary>
/// The color blue.
/// </summary>
public static readonly ConsoleTextColor Blue = new ConsoleTextColor("#0000ff");
public static readonly ConsoleTextColor Blue = new("#0000ff");

/// <summary>
/// The color green.
/// </summary>
public static readonly ConsoleTextColor Green = new ConsoleTextColor("#00ff00");
public static readonly ConsoleTextColor Green = new("#00ff00");

/// <summary>
/// The color cyan (blue-green).
/// </summary>
public static readonly ConsoleTextColor Cyan = new ConsoleTextColor("#00ffff");
public static readonly ConsoleTextColor Cyan = new("#00ffff");

/// <summary>
/// The color red.
/// </summary>
public static readonly ConsoleTextColor Red = new ConsoleTextColor("#ff0000");
public static readonly ConsoleTextColor Red = new("#ff0000");

/// <summary>
/// The color magenta (purplish-red).
/// </summary>
public static readonly ConsoleTextColor Magenta = new ConsoleTextColor("#ff00ff");
public static readonly ConsoleTextColor Magenta = new("#ff00ff");

/// <summary>
/// The color yellow.
/// </summary>
public static readonly ConsoleTextColor Yellow = new ConsoleTextColor("#ffff00");
public static readonly ConsoleTextColor Yellow = new("#ffff00");

/// <summary>
/// The color white.
/// </summary>
public static readonly ConsoleTextColor White = new ConsoleTextColor("#ffffff");
public static readonly ConsoleTextColor White = new("#ffffff");

private readonly string _color;

private ConsoleTextColor(string color)
Expand Down
9 changes: 1 addition & 8 deletions src/Hangfire.Console/Dashboard/ConsoleDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@
namespace Hangfire.Console.Dashboard
{
/// <summary>
/// Provides incremental updates for a console.
/// Provides incremental updates for a console.
/// </summary>
internal class ConsoleDispatcher : IDashboardDispatcher
{
private readonly ConsoleOptions _options;

public ConsoleDispatcher(ConsoleOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public Task Dispatch(DashboardContext context)
{
if (context == null)
Expand Down
16 changes: 8 additions & 8 deletions src/Hangfire.Console/Dashboard/ConsoleRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal static class ConsoleRenderer
private static readonly Regex LinkDetector = new Regex(@"
\b(?:(?<schema>(?:f|ht)tps?://)|www\.|ftp\.)
(?:\([-\w+&@#/%=~|$?!:,.]*\)|[-\w+&@#/%=~|$?!:,.])*
(?:\([-\w+&@#/%=~|$?!:,.]*\)|[\w+&@#/%=~|$])",
(?:\([-\w+&@#/%=~|$?!:,.]*\)|[\w+&@#/%=~|$])",
RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Compiled);

private class DummyPage : RazorPage
Expand All @@ -41,7 +41,7 @@ public override void Execute()
public static void RenderText(StringBuilder buffer, string text)
{
if (string.IsNullOrEmpty(text)) return;

var start = 0;

foreach (Match m in LinkDetector.Matches(text))
Expand All @@ -55,7 +55,7 @@ public static void RenderText(StringBuilder buffer, string text)
if (!m.Groups["schema"].Success)
{
// force schema for links without one (like www.google.com)
schema = m.Value.StartsWith("ftp.", StringComparison.OrdinalIgnoreCase) ? "ftp://" : "http://";
schema = m.Value.StartsWith("ftp.", StringComparison.OrdinalIgnoreCase) ? "ftp://" : "https://";
}

buffer.Append("<a target=\"_blank\" rel=\"nofollow\" href=\"")
Expand Down Expand Up @@ -136,7 +136,7 @@ public static void RenderLines(StringBuilder builder, IEnumerable<ConsoleLine> l
RenderLine(builder, line, timestamp);
}
}

/// <summary>
/// Fetches and renders console line buffer.
/// </summary>
Expand All @@ -155,7 +155,7 @@ public static void RenderLineBuffer(StringBuilder builder, IConsoleStorage stora

var items = ReadLines(storage, consoleId, ref start);

builder.AppendFormat("<div class=\"line-buffer\" data-n=\"{1}\">", consoleId, start);
builder.AppendFormat("<div class=\"line-buffer\" data-n=\"{0}\">", start);
RenderLines(builder, items, consoleId.DateValue);
builder.Append("</div>");
}
Expand All @@ -167,8 +167,8 @@ public static void RenderLineBuffer(StringBuilder builder, IConsoleStorage stora
/// <param name="consoleId">Console identifier</param>
/// <param name="start">Offset to read lines from</param>
/// <remarks>
/// On completion, <paramref name="start"/> is set to the end of the current batch,
/// and can be used for next requests (or set to -1, if the job has finished processing).
/// On completion, <paramref name="start"/> is set to the end of the current batch,
/// and can be used for next requests (or set to -1, if the job has finished processing).
/// </remarks>
private static IEnumerable<ConsoleLine> ReadLines(IConsoleStorage storage, ConsoleId consoleId, ref int start)
{
Expand All @@ -182,7 +182,7 @@ private static IEnumerable<ConsoleLine> ReadLines(IConsoleStorage storage, Conso
// has some new items to fetch

Dictionary<string, ConsoleLine> progressBars = null;

foreach (var entry in storage.GetLines(consoleId, start, count - 1))
{
if (entry.ProgressValue.HasValue)
Expand Down
4 changes: 2 additions & 2 deletions src/Hangfire.Console/Dashboard/DynamicJsDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public DynamicJsDispatcher(ConsoleOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}

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

builder.Append(@"(function (hangfire) {")
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)
Expand Down
45 changes: 22 additions & 23 deletions src/Hangfire.Console/Dashboard/JobProgressDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,52 @@ namespace Hangfire.Console.Dashboard
/// </summary>
internal class JobProgressDispatcher : IDashboardDispatcher
{
internal static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings()
internal static readonly JsonSerializerSettings JsonSettings = new()
{
ContractResolver = new DefaultContractResolver()
};

// ReSharper disable once NotAccessedField.Local
private readonly ConsoleOptions _options;

public JobProgressDispatcher(ConsoleOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}

public async Task Dispatch(DashboardContext context)
{
if (!"POST".Equals(context.Request.Method, StringComparison.OrdinalIgnoreCase))
{
context.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
return;
}

var result = new Dictionary<string, double>();

var jobIds = await context.Request.GetFormValuesAsync("jobs[]");
if (jobIds.Count > 0)
{
// there are some jobs to process

using (var connection = context.Storage.GetConnection())
using (var storage = new ConsoleStorage(connection))

using var connection = context.Storage.GetConnection();
using var storage = new ConsoleStorage(connection);

foreach (var jobId in jobIds)
{
foreach (var jobId in jobIds)
var state = connection.GetStateData(jobId);
if (state != null && string.Equals(state.Name, ProcessingState.StateName, StringComparison.OrdinalIgnoreCase))
{
var consoleId = new ConsoleId(jobId, JobHelper.DeserializeDateTime(state.Data["StartedAt"]));

var progress = storage.GetProgress(consoleId);
if (progress.HasValue)
result[jobId] = progress.Value;
}
else
{
var state = connection.GetStateData(jobId);
if (state != null && string.Equals(state.Name, ProcessingState.StateName, StringComparison.OrdinalIgnoreCase))
{
var consoleId = new ConsoleId(jobId, JobHelper.DeserializeDateTime(state.Data["StartedAt"]));

var progress = storage.GetProgress(consoleId);
if (progress.HasValue)
result[jobId] = progress.Value;
}
else
{
// return -1 to indicate the job is not in Processing state
result[jobId] = -1;
}
// return -1 to indicate the job is not in Processing state
result[jobId] = -1;
}
}
}
Expand All @@ -74,4 +73,4 @@ public async Task Dispatch(DashboardContext context)
await context.Response.WriteAsync(serialized);
}
}
}
}
Loading

0 comments on commit f022b37

Please sign in to comment.