From 1599cebc4f2d9f0d71c0203372a9bcb958a8589a Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Mon, 25 Jul 2022 19:11:59 +0800 Subject: [PATCH 1/6] Support array result --- core/ruby2.5Action/rackapp/run.rb | 6 +++--- core/ruby2.6ActionLoop/Dockerfile | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/ruby2.5Action/rackapp/run.rb b/core/ruby2.5Action/rackapp/run.rb index 2e4eab2..d2200b5 100644 --- a/core/ruby2.5Action/rackapp/run.rb +++ b/core/ruby2.5Action/rackapp/run.rb @@ -44,7 +44,7 @@ def call(env) if system(env, "bundle exec ruby -r #{ENTRYPOINT} #{RACKAPP_DIR}runner.rb | tee #{OUT}") then if File.exist? RESULT then result = File.read(RESULT) - if valid_json?(result) then + if valid_json_or_array(result) then SuccessResponse.new(JSON.parse(result)) else warn "Result must be an array but has type '#{result.class.to_s}': #{result}" @@ -59,8 +59,8 @@ def call(env) end private - def valid_json?(json) - JSON.parse(json).class == Hash + def valid_json_or_array(json) + JSON.parse(json).class == Hash or JSON.parse(json).class == Array rescue false end diff --git a/core/ruby2.6ActionLoop/Dockerfile b/core/ruby2.6ActionLoop/Dockerfile index 8daffed..c1ab5a3 100644 --- a/core/ruby2.6ActionLoop/Dockerfile +++ b/core/ruby2.6ActionLoop/Dockerfile @@ -17,8 +17,8 @@ # build go proxy from source FROM golang:1.18 AS builder_source -ARG GO_PROXY_GITHUB_USER=apache -ARG GO_PROXY_GITHUB_BRANCH=master +ARG GO_PROXY_GITHUB_USER=ningyougang +ARG GO_PROXY_GITHUB_BRANCH=support-array-result-include-sequence-action RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \ https://github.com/${GO_PROXY_GITHUB_USER}/openwhisk-runtime-go /src ;\ cd /src ; env GO111MODULE=on CGO_ENABLED=0 go build main/proxy.go && \ @@ -31,12 +31,12 @@ RUN curl -sL \ https://github.com/apache/openwhisk-runtime-go/archive/{$GO_PROXY_RELEASE_VERSION}.tar.gz\ | tar xzf -\ && cd openwhisk-runtime-go-*/main\ - && GO111MODULE=on go build -o /bin/proxy + && GO111MODULE=on CGO_ENABLED=0 go build -o /bin/proxy FROM ruby:2.6 # select the builder to use -ARG GO_PROXY_BUILD_FROM=release +ARG GO_PROXY_BUILD_FROM=source RUN \ apt-get -y update \ From 3da6d4d6fc85ddae2630682723fe1c64b2df5613 Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Mon, 25 Jul 2022 19:13:57 +0800 Subject: [PATCH 2/6] Add test case --- .../Ruby25ActionContainerTests.scala | 18 +++++++++++++++++ .../Ruby26ActionLoopContainerTests.scala | 20 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala b/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala index 4e5839b..23cdc1c 100644 --- a/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala +++ b/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala @@ -426,4 +426,22 @@ class Ruby25ActionContainerTests extends BasicActionRunnerTests with WskActorSys } } + it should "support array result" in { + val (out, err) = withRuby25Container { c => + val code = """ + | def main(args) + | nums = Array["a","b"] + | nums + | end + """.stripMargin + + val (initCode, _) = c.init(initPayload(code)) + + initCode should be(200) + + val (runCode, runRes) = c.runForJsArray(runPayload(JsObject())) + runCode should be(200) + runRes shouldBe Some(JsArray(JsString("a"), JsString("b"))) + } + } } diff --git a/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala b/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala index a5ed68d..dbe4df2 100644 --- a/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala +++ b/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala @@ -21,6 +21,7 @@ import actionContainers.{ActionContainer, BasicActionRunnerTests} import common.WskActorSystem import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner +import spray.json.{JsArray, JsObject, JsString} @RunWith(classOf[JUnitRunner]) class Ruby26ActionLoopContainerTests extends BasicActionRunnerTests with WskActorSystem { @@ -89,4 +90,23 @@ class Ruby26ActionLoopContainerTests extends BasicActionRunnerTests with WskActo | args |end |""".stripMargin) + + it should "support array result" in { + val (out, err) = withActionLoopContainer { c => + val code = """ + | def main(args) + | nums = Array["a","b"] + | nums + | end + """.stripMargin + + val (initCode, _) = c.init(initPayload(code)) + + initCode should be(200) + + val (runCode, runRes) = c.runForJsArray(JsObject()) + runCode should be(200) + runRes shouldBe Some(JsArray(JsString("a"), JsString("b"))) + } + } } From 93e2500fbb37a22d453c31c57bc36680f9631225 Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Mon, 25 Jul 2022 19:36:46 +0800 Subject: [PATCH 3/6] Use apache master branch --- core/ruby2.6ActionLoop/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/ruby2.6ActionLoop/Dockerfile b/core/ruby2.6ActionLoop/Dockerfile index c1ab5a3..a542b22 100644 --- a/core/ruby2.6ActionLoop/Dockerfile +++ b/core/ruby2.6ActionLoop/Dockerfile @@ -17,8 +17,8 @@ # build go proxy from source FROM golang:1.18 AS builder_source -ARG GO_PROXY_GITHUB_USER=ningyougang -ARG GO_PROXY_GITHUB_BRANCH=support-array-result-include-sequence-action +ARG GO_PROXY_GITHUB_USER=apache +ARG GO_PROXY_GITHUB_BRANCH=master RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \ https://github.com/${GO_PROXY_GITHUB_USER}/openwhisk-runtime-go /src ;\ cd /src ; env GO111MODULE=on CGO_ENABLED=0 go build main/proxy.go && \ From 475c7ff541d69fd90ab3fb18530b0d0e1b083651 Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Mon, 8 Aug 2022 14:24:28 +0800 Subject: [PATCH 4/6] Add another test case --- .../Ruby25ActionContainerTests.scala | 20 ++++++++++++++++- .../Ruby26ActionLoopContainerTests.scala | 22 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala b/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala index 23cdc1c..4ca1288 100644 --- a/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala +++ b/tests/src/test/scala/actionContainers/Ruby25ActionContainerTests.scala @@ -426,7 +426,7 @@ class Ruby25ActionContainerTests extends BasicActionRunnerTests with WskActorSys } } - it should "support array result" in { + it should "support return array result" in { val (out, err) = withRuby25Container { c => val code = """ | def main(args) @@ -444,4 +444,22 @@ class Ruby25ActionContainerTests extends BasicActionRunnerTests with WskActorSys runRes shouldBe Some(JsArray(JsString("a"), JsString("b"))) } } + + it should "support array as input param" in { + val (out, err) = withRuby25Container { c => + val code = """ + | def main(args) + | args + | end + """.stripMargin + + val (initCode, _) = c.init(initPayload(code)) + + initCode should be(200) + + val (runCode, runRes) = c.runForJsArray(runPayload(JsArray(JsString("a"), JsString("b")))) + runCode should be(200) + runRes shouldBe Some(JsArray(JsString("a"), JsString("b"))) + } + } } diff --git a/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala b/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala index dbe4df2..d249d51 100644 --- a/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala +++ b/tests/src/test/scala/actionContainers/Ruby26ActionLoopContainerTests.scala @@ -91,7 +91,7 @@ class Ruby26ActionLoopContainerTests extends BasicActionRunnerTests with WskActo |end |""".stripMargin) - it should "support array result" in { + it should "support return array result" in { val (out, err) = withActionLoopContainer { c => val code = """ | def main(args) @@ -104,7 +104,25 @@ class Ruby26ActionLoopContainerTests extends BasicActionRunnerTests with WskActo initCode should be(200) - val (runCode, runRes) = c.runForJsArray(JsObject()) + val (runCode, runRes) = c.runForJsArray(runPayload(JsObject())) + runCode should be(200) + runRes shouldBe Some(JsArray(JsString("a"), JsString("b"))) + } + } + + it should "support array as input param" in { + val (out, err) = withActionLoopContainer { c => + val code = """ + | def main(args) + | args + | end + """.stripMargin + + val (initCode, _) = c.init(initPayload(code)) + + initCode should be(200) + + val (runCode, runRes) = c.runForJsArray(runPayload(JsArray(JsString("a"), JsString("b")))) runCode should be(200) runRes shouldBe Some(JsArray(JsString("a"), JsString("b"))) } From 774c0d33f7520a2a9e7ac2479b87fce299947df3 Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Tue, 9 Aug 2022 08:48:36 +0800 Subject: [PATCH 5/6] Add document for support array result --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index f30a236..1ae16b1 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,38 @@ [![Twitter](https://img.shields.io/twitter/follow/openwhisk.svg?style=social&logo=twitter)](https://twitter.com/intent/follow?screen_name=openwhisk) ### Give it a try today +A very simple `hello world` function would be: + +```ruby +def main(args) + name = args["name"] || "stranger" + greeting = "Hello #{name}!" + puts greeting + { "greeting" => greeting } +end +``` + +For the return result, not only support `dictionary` but also support `array` + +So a very simple `hello array` function would be: + +```ruby +def main(args) + nums = Array["a","b"] + nums +end +``` + +And support array result for sequence action as well, the first action's array result can be used as next action's input parameter. + +So the function can be + +```ruby +def main(args) + args +end +``` + To use as a docker action ``` wsk action update myAction my_action.rb --docker openwhisk/action-ruby-v2.5 From 9ad6a5e01873c10e7f17f8b60e26fd2219e11685 Mon Sep 17 00:00:00 2001 From: "ning.yougang" Date: Mon, 15 Aug 2022 09:15:53 +0800 Subject: [PATCH 6/6] Use relase for build ruby --- core/ruby2.6ActionLoop/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/ruby2.6ActionLoop/Dockerfile b/core/ruby2.6ActionLoop/Dockerfile index a542b22..834650e 100644 --- a/core/ruby2.6ActionLoop/Dockerfile +++ b/core/ruby2.6ActionLoop/Dockerfile @@ -26,7 +26,7 @@ RUN git clone --branch ${GO_PROXY_GITHUB_BRANCH} \ # or build it from a release FROM golang:1.18 AS builder_release -ARG GO_PROXY_RELEASE_VERSION=1.18@1.19.0 +ARG GO_PROXY_RELEASE_VERSION=1.18@1.20.0 RUN curl -sL \ https://github.com/apache/openwhisk-runtime-go/archive/{$GO_PROXY_RELEASE_VERSION}.tar.gz\ | tar xzf -\ @@ -36,7 +36,7 @@ RUN curl -sL \ FROM ruby:2.6 # select the builder to use -ARG GO_PROXY_BUILD_FROM=source +ARG GO_PROXY_BUILD_FROM=release RUN \ apt-get -y update \