Skip to content

Update argument parsing and change a namespace name #12

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 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Grpc.Core;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;

namespace Azure.Functions.PowerShell.Worker.Messaging
namespace Microsoft.Azure.Functions.PowerShellWorker.Messaging
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for doing this. I'm not totally sold that this is the best namespace name but at least we're consistent.

{
public class FunctionMessagingClient : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.4" />
<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" />
</ItemGroup>

<ItemGroup>
Expand Down
44 changes: 0 additions & 44 deletions src/Azure.Functions.PowerShell.Worker/StartupArguments.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Azure.Functions.PowerShell.Worker/Utility/RpcLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

using System;

using Azure.Functions.PowerShell.Worker.Messaging;
using Microsoft.Azure.Functions.PowerShellWorker.Messaging;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
using Microsoft.Extensions.Logging;

Expand Down
49 changes: 38 additions & 11 deletions src/Azure.Functions.PowerShell.Worker/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

using System;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

using Azure.Functions.PowerShell.Worker.Messaging;
using CommandLine;
using Microsoft.Azure.Functions.PowerShellWorker.Messaging;
using Microsoft.Azure.Functions.PowerShellWorker.PowerShell.Host;
using Microsoft.Azure.Functions.PowerShellWorker.Requests;
using Microsoft.Azure.Functions.PowerShellWorker.Utility;
Expand All @@ -29,12 +31,17 @@ static void InitPowerShell()

s_runspace = RunspaceFactory.CreateRunspace(host);
s_runspace.Open();
s_ps = System.Management.Automation.PowerShell.Create(InitialSessionState.CreateDefault());
s_ps = System.Management.Automation.PowerShell.Create();
s_ps.Runspace = s_runspace;

s_ps.AddScript("$PSHOME");
//s_ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", ExecutionPolicy.Unrestricted).AddParameter("Scope", ExecutionPolicyScope.Process);
s_ps.Invoke<string>();
if (Platform.IsWindows)
{
s_ps.AddCommand("Set-ExecutionPolicy")
.AddParameter("ExecutionPolicy", "Unrestricted")
.AddParameter("Scope", "Process")
.Invoke();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I updated this in my PR. We can deal with the merge conflicts later. Yours is better anyway.

s_ps.Commands.Clear();
}

// Add HttpResponseContext namespace so users can reference
// HttpResponseContext without needing to specify the full namespace
Expand All @@ -44,21 +51,23 @@ static void InitPowerShell()

public async static Task Main(string[] args)
{
StartupArguments startupArguments = StartupArguments.Parse(args);
WorkerArguments arguments = null;
Parser.Default.ParseArguments<WorkerArguments>(args)
.WithParsed(ops => arguments = ops)
.WithNotParsed(err => Environment.Exit(1));

// Initialize Rpc client, logger, and PowerShell
s_client = new FunctionMessagingClient(startupArguments.Host, startupArguments.Port);
s_client = new FunctionMessagingClient(arguments.Host, arguments.Port);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on this usage, I guess we can assume that the action call in WithParsed is synchronous. It's an interesting package. In the javascript world the callback pattern doesn't have that guarantee at all.

s_logger = new RpcLogger(s_client);
InitPowerShell();

// Send StartStream message
var streamingMessage = new StreamingMessage() {
RequestId = startupArguments.RequestId,
StartStream = new StartStream() { WorkerId = startupArguments.WorkerId }
RequestId = arguments.RequestId,
StartStream = new StartStream() { WorkerId = arguments.WorkerId }
};

await s_client.WriteAsync(streamingMessage);

await ProcessEvent();
}

Expand Down Expand Up @@ -105,4 +114,22 @@ static async Task ProcessEvent()
}
}
}
}

internal class WorkerArguments
{
[Option("host", Required = true, HelpText = "IP Address used to connect to the Host via gRPC.")]
public string Host { get; set; }

[Option("port", Required = true, HelpText = "Port used to connect to the Host via gRPC.")]
public int Port { get; set; }

[Option("workerId", Required = true, HelpText = "Worker ID assigned to this language worker.")]
public string WorkerId { get; set; }

[Option("requestId", Required = true, HelpText = "Request ID used for gRPC communication with the Host.")]
public string RequestId { get; set; }

[Option("grpcMaxMessageLength", Required = true, HelpText = "gRPC Maximum message size.")]
public int MaxMessageLength { get; set; }
}
}