You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/03/30 20:34:13 UTC

[GitHub] sciabarra commented on a change in pull request #5: The go proxy

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

 ##########
 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")
 
 Review comment:
   they are drained to docker stdout, exacly as docker the python proxy does.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services