internal Hashtable InvokeOrchestrationFunction(
            AzFunctionInfo functionInfo,
            string contextParamName,
            OrchestrationContext context)
        {
            context.ActionEvent = _actionEvent;

            try
            {
                if (!functionInfo.FuncParameters.ContainsKey(contextParamName))
                {
                    // TODO: we should do more analysis in FunctionLoadRequest phase
                    throw new InvalidOperationException($"Orchestration function should define the parameter '-{contextParamName}'");
                }

                _pwsh.AddCommand("Microsoft.Azure.Functions.PowerShellWorker\\Set-FunctionInvocationContext")
                .AddParameter("OrchestrationContext", context)
                .InvokeAndClearCommands();

                _pwsh.AddCommand(string.IsNullOrEmpty(functionInfo.EntryPoint) ? functionInfo.ScriptPath : functionInfo.EntryPoint)
                .AddParameter(contextParamName, context);

                var outputBuffer = new PSDataCollection <object>();
                var asyncResult  = _pwsh.BeginInvoke <object, object>(input: null, output: outputBuffer);

                var waitHandles         = new[] { _actionEvent, asyncResult.AsyncWaitHandle };
                var signaledHandleIndex = WaitHandle.WaitAny(waitHandles);
                var signaledHandle      = waitHandles[signaledHandleIndex];

                OrchestrationMessage retResult = null;
                if (ReferenceEquals(signaledHandle, _actionEvent))
                {
                    // _actionEvent signaled
                    _pwsh.Stop();
                    retResult = new OrchestrationMessage(isDone: false, actions: context.Actions, output: null);
                }
                else
                {
                    // asyncResult.AsyncWaitHandle signaled
                    _pwsh.EndInvoke(asyncResult);
                    var result = CreateReturnValueFromFunctionOutput(outputBuffer);
                    retResult = new OrchestrationMessage(isDone: true, actions: context.Actions, output: result);
                }

                return(new Hashtable {
                    { AzFunctionInfo.DollarReturn, retResult }
                });
            }
            finally
            {
                _pwsh.Streams.ClearStreams();
                _pwsh.Commands.Clear();

                ClearInvocationContext();
                ResetRunspace();
            }
        }
Пример #2
0
        public void TestMessage()
        {
            var    iobuf    = new IOBuffer();
            string testbody = "this is a test";
            var    txmsg    = new OrchestrationMessage(testbody, Options.LAN);

            Message.Transmit(iobuf, txmsg);

            int SYNC = 4;
            int MLEN = 4;
            int MTYP = 4;
            int OPTS = 4;
            int TERM = 1;

            Assert.AreEqual(SYNC + MLEN + MTYP + OPTS + testbody.Length + TERM, iobuf.Length);
            var rxmsg = Message.Receive(iobuf) as OrchestrationMessage;

            Assert.AreEqual(txmsg.Body, rxmsg.Body);
            Assert.AreEqual(txmsg.IsLan, rxmsg.IsLan);
            Assert.AreEqual(txmsg.IsWan, rxmsg.IsWan);
        }
Пример #3
0
        public void SendMessage(string msg, Options opts = Options.LAN)
        {
            OrchestrationMessage xmsg = new OrchestrationMessage(msg, opts);

            SendMessage(xmsg);
        }