rabbah commented on a change in pull request #5: The go proxy 
URL: 
https://github.com/apache/incubator-openwhisk-runtime-go/pull/5#discussion_r178874617
 
 

 ##########
 File path: openwhisk/executor.go
 ##########
 @@ -0,0 +1,208 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package openwhisk
+
+import (
+       "bufio"
+       "fmt"
+       "log"
+       "os"
+       "os/exec"
+       "runtime"
+       "time"
+)
+
+// TIMEOUT to wait for process to start
+// and log to be produced
+const TIMEOUT = 5 * time.Millisecond
+
+// Executor is the container and the guardian  of a child process
+// It starts a command, feeds input and output, read logs and control its 
termination
+type Executor struct {
+       io      chan string
+       log     chan bool
+       exit    chan error
+       _cmd    *exec.Cmd
+       _input  *bufio.Writer
+       _output *bufio.Scanner
+       _logout *bufio.Scanner
+       _logerr *bufio.Scanner
+}
+
+// NewExecutor creates a child subprocess using the provided command line.
+// You can then start it getting a communication channel
+func NewExecutor(command string, args ...string) (proc *Executor) {
+       cmd := exec.Command(command, args...)
+
+       stdin, err := cmd.StdinPipe()
+       if err != nil {
+               return nil
+       }
+
+       stdout, err := cmd.StdoutPipe()
+       if err != nil {
+               return nil
+       }
+
+       stderr, err := cmd.StderrPipe()
+       if err != nil {
+               return nil
+       }
+
+       pipeOut, pipeIn, err := os.Pipe()
+       if err != nil {
+               return nil
+       }
+       cmd.ExtraFiles = []*os.File{pipeIn}
+
+       return &Executor{
+               make(chan string),
+               make(chan bool),
+               make(chan error),
+               cmd,
+               bufio.NewWriter(stdin),
+               bufio.NewScanner(pipeOut),
+               bufio.NewScanner(stdout),
+               bufio.NewScanner(stderr),
+       }
+}
+
+// collect log from a stream
+func _collect(ch chan string, scan *bufio.Scanner) {
+       for scan.Scan() {
+               ch <- scan.Text()
+       }
+}
+
+// loop over the command executing
+// returning when the command exits
+func (proc *Executor) run() {
+       log.Println("run: start")
+       err := proc._cmd.Start()
+       if err != nil {
+               proc.exit <- err
+               log.Println("run: early exit")
+               proc._cmd = nil // do not kill
+               return
+       }
+       // wait for the exit
+       proc.exit <- proc._cmd.Wait()
+       proc._cmd = nil // do not kill
+       log.Println("run: end")
+}
+
+// manage copying stdout and stder in output
+// with log guards
+func (proc *Executor) logger() {
+       log.Println("logger: start")
+       // poll stdout and stderr
+       chOut := make(chan string)
+       go _collect(chOut, proc._logout)
+       chErr := make(chan string)
+       go _collect(chErr, proc._logerr)
+
+       // wait for the signal
+       for <-proc.log {
+               // flush stdout
+               runtime.Gosched()
+               for loop := true; loop; {
+                       select {
+                       case buf := <-chOut:
+                               fmt.Println(buf)
+                       case <-time.After(TIMEOUT):
+                               loop = false
+                       }
+               }
+               fmt.Println("XXX_THE_END_OF_A_WHISK_ACTIVATION_XXX")
+
+               // flush stderr
+               runtime.Gosched()
+               for loop := true; loop; {
+                       select {
+                       case buf := <-chErr:
+                               fmt.Println(buf)
+                       case <-time.After(TIMEOUT):
+                               loop = false
+                       }
+               }
+               fmt.Println("XXX_THE_END_OF_A_WHISK_ACTIVATION_XXX")
+       }
+       log.Printf("logger: end")
+}
+
+// main service function
+// writing in input
+// and reading in output
+// using the provide channels
+func (proc *Executor) service() {
+       log.Println("service: start")
+       for {
+               in := <-proc.io
+               if in == "" {
+                       log.Println("terminated upon request")
+                       break
+               }
+               // input/output with the process
+               log.Printf(">>>%s\n", in)
+               proc._input.WriteString(in + "\n")
+               proc._input.Flush()
+               if proc._output.Scan() {
+                       out := proc._output.Text()
+                       log.Printf("<<<%s\n", out)
+                       proc.io <- out
+                       if out == "" {
+                               break
+                       }
+               }
+       }
+       log.Printf("service: end")
+}
+
+// Start execution of the command
+// returns an error if the program fails
+func (proc *Executor) Start() error {
+       // start the underlying executable
+       // check if died
+       go proc.run()
+       select {
+       case <-proc.exit:
+               // oops, it died
+               return fmt.Errorf("command exited")
+       case <-time.After(TIMEOUT):
+               // ok let's process it
+               go proc.service()
+               go proc.logger()
+       }
+       return nil
+}
+
+// Stop will kill the process
+// and close the channels
+func (proc *Executor) Stop() {
+       log.Println("stopping")
 
 Review comment:
   this is another method that for now won't be called with the current 
container lifecycle; a comment would be nice to that effect if you agree.
   
   in general there are a few flows that can be tested and provide a clean 
interface but should be qualified to separate the behavior against the invoker.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to