of-watchdog icon indicating copy to clipboard operation
of-watchdog copied to clipboard

Serializing mode sends nothing in stdin

Open kdubuc opened this issue 6 years ago • 5 comments

How serializing works ?

Expected Behaviour

Hi all, I use serializing mode, and I get nothing in STDIN (while streaming works). Overall, how to use serializing mode ? I want to manipulate the stdin request and set status code, body, headers ... inside my handler, and send out to the stdout. I think it's the best suited mode, but i can get nothing.

Current Behaviour

POST http://openfaas_gateway:8000/function/name
{"foo": "bar"}

With a PHP script :

<?php

$stdin = file_get_contents("php://stdin");

// $stdin is empty ...

fwrite(STDOUT, $stdout); 

// How to write a HTTP response correctly with stdout ?

Steps to Reproduce (for bugs)

This Dockerfile with the script above.

FROM php:alpine
RUN apk add --no-cache git curl
RUN curl -sSLf https://github.com/openfaas-incubator/of-watchdog/releases/download/0.4.6/of-watchdog > /usr/bin/fwatchdog && chmod +x /usr/bin/fwatchdog
COPY index.php /usr/src/function
WORKDIR /usr/src/function
ENV function_process="php index.php"
ENV mode="serializing"
HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1
CMD ["fwatchdog"]

Context

I want to manipulate the stdin request and set status code, body, headers ... inside my function with stdout.

Your Environment

Docker 18.09.1 Docker swarm macOs PHP 7.3

kdubuc avatar Feb 05 '19 18:02 kdubuc

Hi how did you decide to use of-watchdog for this?

We already have an official PHP template with the classic watchdog that achieves the same thing.

Alex

alexellis avatar Feb 05 '19 18:02 alexellis

I want to manipulate and set status code, body, headers ... inside my handle

You can't do that with the serialising mode, what gave you that impression?

You must use http mode to set explicit headers.

If that's something you need to do then look at my example template with of-watchdog and Swoole (PHP) https://github.com/alexellis/php7-swoole-template

Alex

alexellis avatar Feb 05 '19 18:02 alexellis

Derek add label: support

alexellis avatar Feb 05 '19 18:02 alexellis

If you need help please join us on Slack as advised on the website. https://docs.openfaas.com/community - there you can join the #templates channel and chat with us more.

alexellis avatar Feb 05 '19 18:02 alexellis

Thanks for your replies.

Hi how did you decide to use of-watchdog for this?

We already have an official PHP template with the classic watchdog that achieves the same thing.

Alex

The power of openfaas I think is the ability to use any docker images, and turn them into a powerfull FaaS in a microservices universe. The watchdog is a tiny layer, to make it happen. Ideally, the FaaS Dockerfile should be independant of the gateway, but this is a little compromise. So, I like the fact i can build my Dockerfile, and have total control over them (repository public / private, packages ...), and not being tied to pre-packaged runtimes (like AWS, OpenWhisk, or openfaas templates ...).

I want to manipulate and set status code, body, headers ... inside my handle

You can't do that with the serialising mode, what gave you that impression?

You must use http mode to set explicit headers.

If that's something you need to do then look at my example template with of-watchdog and Swoole (PHP) https://github.com/alexellis/php7-swoole-template

Alex

https://github.com/openfaas-incubator/of-watchdog/blob/master/README.md#L89 :

Reads entire request into memory from the HTTP request. At this point we serialize or modify if required. That is then written into the stdin pipe. Stdout pipe is read into memory and then serialized or modified if necessary before being written back to the HTTP response.

This line confuse me. Overall, multi modes confuses me, because i went into openfaas because the communication between gateway and function is very light and powerful. But, the way stdin and stdout are managed by watchdog depending of types is difficult for me.

It would be great to pass http request as a standardized message into our functions in stdin, no matter of mode. Like :

{
  "type": "httprequest",
  "headers": [...],
  "method": "...",
  "protocol: "...",
  "uri": "...",
  "foo": "body param ..",
  "bar": "query param ..."
}

We process the message, and send in stdout :

{
  "type": "httpresponse",
  "headers": [...],
  "status_code": "xxx",
  "body": "json / octet / base64 ... response"
}

The watch dog transform this message into a proper HTTP response to the gateway.

Also, this can trigger an event like :

{
  "type": "event",
  "topic": "...",
  "foo": "",
  "bar": ""
}

And execute another function ...

It would be a very clear interface between openfass and functions, and avoid confusion 😉

kdubuc avatar Feb 06 '19 09:02 kdubuc