diff --git a/api/runner/func_logger.go b/api/runner/func_logger.go index e31b1a335..2376df7c4 100644 --- a/api/runner/func_logger.go +++ b/api/runner/func_logger.go @@ -27,6 +27,7 @@ func NewFuncLogger(logDB models.FnLog) FuncLogger { type writer struct { bytes.Buffer + stderr bytes.Buffer // for logging to stderr db models.FnLog ctx context.Context reqID string @@ -36,28 +37,41 @@ type writer struct { } func (w *writer) Close() error { + w.flush() return w.db.InsertLog(context.TODO(), w.reqID, w.String()) } func (w *writer) Write(b []byte) (int, error) { n, err := w.Buffer.Write(b) - // for now, also write to stderr so we can debug quick ;) - // TODO this should be a separate FuncLogger but time is running short ! - log := common.Logger(w.ctx) - log = log.WithFields(logrus.Fields{"user_log": true, "app_name": w.appName, "path": w.path, "image": w.image, "call_id": w.reqID}) - for i := 0; i < len(b); i++ { - j := i - i = bytes.IndexByte(b[i:], '\n') - if i < 0 { - i = len(b) - } - log.Println(string(b[j:i])) - } + // temp or should move to another FuncLogger implementation + w.writeStdErr(b) return n, err } +func (w *writer) writeStdErr(b []byte) { + // for now, also write to stderr so we can debug quick ;) + // TODO this should be a separate FuncLogger but time is running short ! + endLine := bytes.IndexByte(b, '\n') + if endLine < 0 { + w.stderr.Write(b) + return + } + // we have a new line, so: + w.stderr.Write(b[0:endLine]) + w.flush() + w.writeStdErr(b[endLine+1:]) + +} + +func (w *writer) flush() { + log := common.Logger(w.ctx) + log = log.WithFields(logrus.Fields{"user_log": true, "app_name": w.appName, "path": w.path, "image": w.image, "call_id": w.reqID}) + log.Println(w.stderr.String()) + w.stderr.Reset() +} + // overrides Write, keeps Close type limitWriter struct { n, max int diff --git a/examples/error/.gitignore b/examples/error/.gitignore index e715a8a80..391a9be89 100644 --- a/examples/error/.gitignore +++ b/examples/error/.gitignore @@ -1,2 +1,3 @@ bundle/ .bundle/ +func.yaml diff --git a/examples/error/Dockerfile b/examples/error/Dockerfile deleted file mode 100644 index d347e76fa..000000000 --- a/examples/error/Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -FROM funcy/ruby:dev - -WORKDIR /worker -ADD Gemfile* /worker/ -RUN bundle install - -ADD . /worker/ - -ENTRYPOINT ["ruby", "error.rb"] diff --git a/examples/error/README.md b/examples/error/README.md index 6daccb3c4..53164d6ff 100644 --- a/examples/error/README.md +++ b/examples/error/README.md @@ -1,77 +1,3 @@ # Error Function Image -This images compares the payload info with the header. - -## Requirements - -- Oracle Functions API - -## Development - -### Building image locally - -``` -# SET BELOW TO YOUR DOCKER HUB USERNAME -USERNAME=YOUR_DOCKER_HUB_USERNAME - -# build it -./build.sh -``` - -### Publishing to DockerHub - -``` -# tagging -docker run --rm -v "$PWD":/app treeder/bump patch -docker tag $USERNAME/func-error:latest $USERNAME/func-error:`cat VERSION` - -# pushing to docker hub -docker push $USERNAME/func-error -``` - -### Testing image - -``` -./test.sh -``` - -## Running it on Oracle Functions - -### Let's define some environment variables - -``` -# Set your Function server address -# Eg. 127.0.0.1:8080 -FUNCAPI=YOUR_FUNCTIONS_ADDRESS -``` - -### Running with Oracle Functions - -With this command we are going to create an application with name `error`. - -``` -curl -X POST --data '{ - "app": { - "name": "error", - } -}' http://$FUNCAPI/v1/apps -``` - -Now, we can create our route - -``` -curl -X POST --data '{ - "route": { - "image": "'$USERNAME'/func-error", - "path": "/error", - } -}' http://$FUNCAPI/v1/apps/error/routes -``` - -#### Testing function - -Now that we created our Oracle Functions route, let's test our new route - -``` -curl -X POST --data '{"input": "yoooo"}' http://$FUNCAPI/r/error/error -``` \ No newline at end of file +Raises an error. diff --git a/examples/error/build.sh b/examples/error/build.sh index e8702889b..469ab9e21 100755 --- a/examples/error/build.sh +++ b/examples/error/build.sh @@ -1,4 +1,4 @@ #!/bin/bash set -ex -docker build -t username/func-error . \ No newline at end of file +docker build -t funcy/error . diff --git a/examples/error/function.rb b/examples/error/func.rb similarity index 100% rename from examples/error/function.rb rename to examples/error/func.rb diff --git a/examples/error/func.yaml b/examples/error/func.yaml index aa48cd735..e81f3653d 100644 --- a/examples/error/func.yaml +++ b/examples/error/func.yaml @@ -1,3 +1,5 @@ -name: username/func-error -build: - - ./build.sh \ No newline at end of file +name: funcy/error +version: 0.0.2 +runtime: ruby +entrypoint: ruby func.rb +path: /error diff --git a/examples/tutorial/hello/go/func.go b/examples/tutorial/hello/go/func.go index 74ec37c55..306091faa 100644 --- a/examples/tutorial/hello/go/func.go +++ b/examples/tutorial/hello/go/func.go @@ -17,4 +17,8 @@ func main() { fmt.Printf("Hello %v!\n", p.Name) log.Println("---> stderr goes to the server logs.") + log.Println("---> LINE 2") + log.Println("---> LINE 3 with a break right here\nand LINE 4") + log.Println("---> LINE 5 with a double line break\n") + log.Println("---> LINE 6") } diff --git a/fn/deploy.go b/fn/deploy.go index 1b9cb2dc0..df67cbd22 100644 --- a/fn/deploy.go +++ b/fn/deploy.go @@ -146,7 +146,6 @@ func (p *deploycmd) deploy(c *cli.Context, funcFilePath string) error { func (p *deploycmd) route(c *cli.Context, ff *funcfile) error { fmt.Printf("Updating route %s using image %s...\n", ff.Path, ff.FullName()) - fmt.Printf("%+v\ntype: %v\n", ff, *ff.Type) if err := resetBasePath(p.Configuration); err != nil { return fmt.Errorf("error setting endpoint: %v", err) }