Skip to content

Commit

Permalink
Merge pull request #179 from jibedoubleve/issue_150
Browse files Browse the repository at this point in the history
Issue 150
  • Loading branch information
jibedoubleve authored Apr 26, 2023
2 parents 4119c09 + 99d7a89 commit 9e96b5c
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Lanceur.Core/Models/AliasQueryResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static AliasQueryResult Duplicate(this AliasQueryResult @this)
}

public class AliasQueryResult : ExecutableWithPrivilege
{
{
#region Fields

private string _fileName;
Expand Down
11 changes: 5 additions & 6 deletions src/Lanceur.Infra/Managers/ExecutionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public async Task<ExecutionResponse> ExecuteAsync(ExecutionRequest request)
HasResult = true,
};
}
else if (request.QueryResult is IExecutable exec)
if (request.QueryResult is IExecutable exec)
{
if (request.QueryResult is IExecutableWithPrivilege exp)
{
Expand All @@ -133,13 +133,12 @@ public async Task<ExecutionResponse> ExecuteAsync(ExecutionRequest request)
? await ExecuteAliasAsync(alias)
: await exec.ExecuteAsync(_cmdlineManager.BuildFromText(request.Query));


return ExecutionResponse.FromResults(result);
}
else
{
_log.Info($"Alias '{request.QueryResult.Name}', is not executable. Add as a query");
return ExecutionResponse.EmptyResult;
}

_log.Info($"Alias '{request.QueryResult.Name}', is not executable. Add as a query");
return ExecutionResponse.EmptyResult;
}

#endregion Methods
Expand Down
32 changes: 32 additions & 0 deletions src/Lanceur.SharedKernel/Utils/TaskHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Lanceur.SharedKernel.Utils
{
/// https://stackoverflow.com/a/20364016/389529
public static class TaskHelper
{
#region Methods

/// <summary>
/// Runs a TPL Task fire-and-forget style, the right way - in the
/// background, separate from the current thread, with no risk
/// of it trying to rejoin the current thread.
/// </summary>
public static void RunBackground(Func<Task> fn)
{
Task.Run(fn).ConfigureAwait(false);
}

/// <summary>
/// Runs a task fire-and-forget style and notifies the TPL that this
/// will not need a Thread to resume on for a long time, or that there
/// are multiple gaps in thread use that may be long.
/// Use for example when talking to a slow webservice.
/// </summary>
public static void RunBackgroundLong(Func<Task> fn)
{
Task.Factory.StartNew(fn, TaskCreationOptions.LongRunning)
.ConfigureAwait(false);
}

#endregion Methods
}
}
5 changes: 2 additions & 3 deletions src/Lanceur/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Lanceur.Views;
using ReactiveUI;
using Splat;
using Splat.NLog;
using System;
using System.Data.SQLite;
using System.Reflection;
Expand Down Expand Up @@ -60,8 +59,8 @@ private static void RegisterServices()
{
var l = Locator.CurrentMutable;

// then in your service locator initialisation
l.UseNLogWithWrappingFullLogger();
Locator.CurrentMutable.RegisterConstant(new ReactiveUILogger() { Level = LogLevel.Debug }, typeof(ILogger));

l.RegisterLazySingleton<IMapper>(() => new Mapper(GetAutoMapperCfg()));
l.RegisterLazySingleton<IUserNotification>(() => new UserNotification());
l.RegisterLazySingleton(() => new RoutingState());
Expand Down
26 changes: 15 additions & 11 deletions src/Lanceur/Macros/MultiMacro.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using Lanceur.Core.Models;
using Lanceur.Core.Requests;
using Lanceur.Core.Services;
using Lanceur.SharedKernel.Utils;
using Splat;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -28,7 +30,6 @@ public class MultiMacro : ExecutableQueryResult

#if DEBUG


public MultiMacro() : this(0, null, null)
{
}
Expand Down Expand Up @@ -63,22 +64,25 @@ private AliasQueryResult GetAlias(string item)

public override async Task<IEnumerable<QueryResult>> ExecuteAsync(Cmdline cmdline = null)
{
var items = cmdline?.Parameters?.Split('@') ?? Array.Empty<string>();
var items = Parameters?.Split('@') ?? Array.Empty<string>();

foreach (var item in items)
TaskHelper.RunBackground(async () =>
{
await Task.Delay(_delay);
var alias = GetAlias(item);
if (alias is not null)
foreach (var item in items)
{
await _executionManager.ExecuteAsync(new ExecutionRequest
await Task.Delay(_delay);
var alias = GetAlias(item);
if (alias is not null)
{
QueryResult = alias,
});
await _executionManager.ExecuteAsync(new ExecutionRequest
{
QueryResult = alias,
});
}
}
}
});

return NoResult;
return await NoResultAsync;
}

#endregion Methods
Expand Down
72 changes: 72 additions & 0 deletions src/Lanceur/ReactiveUILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using NLog;
using System;
using System.ComponentModel;
using System.Globalization;
using ILogger = Splat.ILogger;
using LogLevel = Splat.LogLevel;
using NLogLevel = NLog.LogLevel;


namespace Lanceur
{
internal class ReactiveUILogger : ILogger
{
#region Fields

private readonly Logger _logger;

#endregion Fields

#region Constructors

public ReactiveUILogger()
{
_logger = LogManager.GetLogger("ReactiveUI");
}

#endregion Constructors

#region Properties

public LogLevel Level { get; set; }

#endregion Properties

#region Methods

private NLogLevel GetLevel(LogLevel logLevel)
{
return logLevel switch
{
LogLevel.Debug => NLogLevel.Trace,
LogLevel.Info => NLogLevel.Debug,
LogLevel.Warn => NLogLevel.Warn,
LogLevel.Error => NLogLevel.Error,
LogLevel.Fatal => NLogLevel.Fatal,
_ => throw new NotSupportedException($"Log level '{logLevel}' not supported."),
};
}

public void Write([Localizable(false)] string message, LogLevel logLevel)
{
_logger.Log(GetLevel(logLevel), message);
}

public void Write(Exception exception, [Localizable(false)] string message, LogLevel logLevel)
{
_logger.Log(GetLevel(logLevel), exception, CultureInfo.InvariantCulture, message);
}

public void Write([Localizable(false)] string message, [Localizable(false)] Type type, LogLevel logLevel)
{
_logger.Log(GetLevel(logLevel), message);
}

public void Write(Exception exception, [Localizable(false)] string message, [Localizable(false)] Type type, LogLevel logLevel)
{
_logger.Log(GetLevel(logLevel), exception, CultureInfo.InvariantCulture, message);
}

#endregion Methods
}
}

0 comments on commit 9e96b5c

Please sign in to comment.