Skip to content

Refactor code base to remove dependency on 'Microsoft.Extensions.Logging' and merge 'Function' with 'FunctionInfo' #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Azure.Functions.PowerShell.Worker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.1.0-rc.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="CommandLineParser" Version="2.3.0" />
Expand Down
43 changes: 0 additions & 43 deletions src/Function/FunctionInfo.cs

This file was deleted.

39 changes: 0 additions & 39 deletions src/Function/FunctionLoader.cs

This file was deleted.

61 changes: 61 additions & 0 deletions src/FunctionLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using Google.Protobuf.Collections;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;

namespace Microsoft.Azure.Functions.PowerShellWorker
{
internal class FunctionLoader
{
private readonly MapField<string, FunctionInfo> _loadedFunctions = new MapField<string, FunctionInfo>();

internal FunctionInfo GetFunctionInfo(string functionId)
{
FunctionInfo funcInfo = null;
_loadedFunctions.TryGetValue(functionId, out funcInfo);
return funcInfo;
}

internal void Load(FunctionLoadRequest request)
{
// TODO: catch "load" issues at "func start" time.
// ex. Script doesn't exist, entry point doesn't exist
_loadedFunctions.Add(request.FunctionId, new FunctionInfo(request.Metadata));
}
}

internal class FunctionInfo
{
internal readonly string Directory;
internal readonly string EntryPoint;
internal readonly string FunctionName;
internal readonly string ScriptPath;
internal readonly MapField<string, BindingInfo> AllBindings;
internal readonly MapField<string, BindingInfo> OutBindings;

public FunctionInfo(RpcFunctionMetadata metadata)
{
FunctionName = metadata.Name;
Directory = metadata.Directory;
EntryPoint = metadata.EntryPoint;
ScriptPath = metadata.ScriptFile;

AllBindings = new MapField<string, BindingInfo>();
OutBindings = new MapField<string, BindingInfo>();

foreach (var binding in metadata.Bindings)
{
AllBindings.Add(binding.Key, binding.Value);

// PowerShell doesn't support the 'InOut' type binding
if (binding.Value.Direction == BindingInfo.Types.Direction.Out)
{
OutBindings.Add(binding.Key, binding.Value);
}
}
}
}
}
6 changes: 3 additions & 3 deletions src/Messaging/MessagingStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Messaging
{
internal class MessagingStream : IDisposable
{
private SemaphoreSlim _writeStreamHandle = new SemaphoreSlim(1, 1);
private SemaphoreSlim _writeSemaphore = new SemaphoreSlim(1, 1);
private AsyncDuplexStreamingCall<StreamingMessage, StreamingMessage> _call;
private bool isDisposed;

Expand Down Expand Up @@ -45,14 +45,14 @@ public async Task WriteAsync(StreamingMessage message)

// Wait for the handle to be released because we can't have
// more than one message being sent at the same time
await _writeStreamHandle.WaitAsync();
await _writeSemaphore.WaitAsync();
try
{
await _call.RequestStream.WriteAsync(message);
}
finally
{
_writeStreamHandle.Release();
_writeSemaphore.Release();
}
}
}
Expand Down
73 changes: 25 additions & 48 deletions src/Messaging/RpcLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,49 @@

using Microsoft.Azure.Functions.PowerShellWorker.Messaging;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
using Microsoft.Extensions.Logging;
using LogLevel = Microsoft.Azure.WebJobs.Script.Grpc.Messages.RpcLog.Types.Level;

namespace Microsoft.Azure.Functions.PowerShellWorker.Utility
{
internal class RpcLogger : ILogger
internal class RpcLogger : IDisposable
{
private MessagingStream _msgStream;
private string _invocationId = "";
private string _requestId = "";
private string _invocationId;
private string _requestId;

public RpcLogger(MessagingStream msgStream)
{
_msgStream = msgStream;
}

public IDisposable BeginScope<TState>(TState state) =>
throw new NotImplementedException();

public static RpcLog.Types.Level ConvertLogLevel(LogLevel logLevel)
public IDisposable BeginScope(string requestId, string invocationId)
{
switch (logLevel)
{
case LogLevel.Critical:
return RpcLog.Types.Level.Critical;
case LogLevel.Debug:
return RpcLog.Types.Level.Debug;
case LogLevel.Error:
return RpcLog.Types.Level.Error;
case LogLevel.Information:
return RpcLog.Types.Level.Information;
case LogLevel.Trace:
return RpcLog.Types.Level.Trace;
case LogLevel.Warning:
return RpcLog.Types.Level.Warning;
default:
return RpcLog.Types.Level.None;
}
_requestId = requestId;
_invocationId = invocationId;
return this;
}

public bool IsEnabled(LogLevel logLevel) =>
throw new NotImplementedException();

public async void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
public void Dispose()
{
if (_msgStream != null)
{
var logMessage = new StreamingMessage
{
RequestId = _requestId,
RpcLog = new RpcLog()
{
Exception = exception?.ToRpcException(),
InvocationId = _invocationId,
Level = ConvertLogLevel(logLevel),
Message = formatter(state, exception)
}
};

await _msgStream.WriteAsync(logMessage);
}
_requestId = null;
_invocationId = null;
}

public void SetContext(string requestId, string invocationId)
public async void Log(LogLevel logLevel, string message, Exception exception = null)
{
_requestId = requestId;
_invocationId = invocationId;
var logMessage = new StreamingMessage
{
RequestId = _requestId,
RpcLog = new RpcLog()
{
Exception = exception?.ToRpcException(),
InvocationId = _invocationId,
Level = logLevel,
Message = message
}
};

await _msgStream.WriteAsync(logMessage);
}
}
}
6 changes: 3 additions & 3 deletions src/PowerShell/PowerShellManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

using Microsoft.Azure.Functions.PowerShellWorker.Utility;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
using Microsoft.Extensions.Logging;
using System.Management.Automation.Runspaces;
using LogLevel = Microsoft.Azure.WebJobs.Script.Grpc.Messages.RpcLog.Types.Level;

namespace Microsoft.Azure.Functions.PowerShellWorker.PowerShell
{
Expand Down Expand Up @@ -98,7 +98,7 @@ internal Hashtable InvokeFunction(
if(parameterMetadata.ContainsKey(_TriggerMetadataParameterName))
{
_pwsh.AddParameter(_TriggerMetadataParameterName, triggerMetadata);
_logger.LogDebug($"TriggerMetadata found. Value:{Environment.NewLine}{triggerMetadata.ToString()}");
_logger.Log(LogLevel.Debug, $"TriggerMetadata found. Value:{Environment.NewLine}{triggerMetadata.ToString()}");
}

PSObject returnObject = null;
Expand All @@ -108,7 +108,7 @@ internal Hashtable InvokeFunction(
Collection<PSObject> pipelineItems = _pwsh.InvokeAndClearCommands<PSObject>();
foreach (var psobject in pipelineItems)
{
_logger.LogInformation($"OUTPUT: {psobject.ToString()}");
_logger.Log(LogLevel.Information, $"OUTPUT: {psobject.ToString()}");
}

returnObject = pipelineItems[pipelineItems.Count - 1];
Expand Down
14 changes: 7 additions & 7 deletions src/PowerShell/StreamHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//

using Microsoft.Azure.Functions.PowerShellWorker.Utility;
using Microsoft.Extensions.Logging;
using LogLevel = Microsoft.Azure.WebJobs.Script.Grpc.Messages.RpcLog.Types.Level;

namespace Microsoft.Azure.Functions.PowerShellWorker.PowerShell
{
Expand All @@ -23,47 +23,47 @@ public void DebugDataAdding(object sender, DataAddingEventArgs e)
{
if(e.ItemAdded is DebugRecord record)
{
_logger.LogDebug($"DEBUG: {record.Message}");
_logger.Log(LogLevel.Debug, $"DEBUG: {record.Message}");
}
}

public void ErrorDataAdding(object sender, DataAddingEventArgs e)
{
if(e.ItemAdded is ErrorRecord record)
{
_logger.LogError(record.Exception, $"ERROR: {record.Exception.Message}");
_logger.Log(LogLevel.Error, $"ERROR: {record.Exception.Message}", record.Exception);
}
}

public void InformationDataAdding(object sender, DataAddingEventArgs e)
{
if(e.ItemAdded is InformationRecord record)
{
_logger.LogInformation($"INFORMATION: {record.MessageData}");
_logger.Log(LogLevel.Information, $"INFORMATION: {record.MessageData}");
}
}

public void ProgressDataAdding(object sender, DataAddingEventArgs e)
{
if(e.ItemAdded is ProgressRecord record)
{
_logger.LogTrace($"PROGRESS: {record.StatusDescription}");
_logger.Log(LogLevel.Trace, $"PROGRESS: {record.StatusDescription}");
}
}

public void VerboseDataAdding(object sender, DataAddingEventArgs e)
{
if(e.ItemAdded is VerboseRecord record)
{
_logger.LogTrace($"VERBOSE: {record.Message}");
_logger.Log(LogLevel.Trace, $"VERBOSE: {record.Message}");
}
}

public void WarningDataAdding(object sender, DataAddingEventArgs e)
{
if(e.ItemAdded is WarningRecord record)
{
_logger.LogWarning($"WARNING: {record.Message}");
_logger.Log(LogLevel.Warning, $"WARNING: {record.Message}");
}
}
}
Expand Down
Loading