function_invoker.cr

Function Invocation Library for Crystal on OpenFaaS

function_invoker

Seamlessly call other OpenFaaS functions from your crystal functions.

Simply provide a function name and call it:

Function.new("echo").invoke

This will make a call to your OpenFaaS gateway to the echo function, which should be deployed :)

Optionally, provide a http method, provide data or ignore all errors:

Function.new("echo", "GET").invoke("some_string", ignore_errors: true)

Installation

  1. Have a crystal function :)
faas template store pull crystal
faas new my-crystal-function --lang crystal
  1. Add the dependency to your shard.yml:
dependencies:
  function_invoker:
    github: tpei/function_invoker.cr
  1. Run shards install

  2. Add your gateway address to you function yaml

If nothing is provided, the default of gateway:8080 is used

provider:
  name: faas
  gateway: localhost:8080
functions:
  pipe-crystal-test:
    lang: crystal
    handler: ./pipe-crystal-test
    image: pipe-crystal-test:latest
    environment:
      GATEWAY: "faas.production.tpei.com:8080"

Usage

require "function_invoker"

Optionally, put

Function = FunctionInvoker::Function

somewhere at the top of your handler class to save some typing :)

Example Function

require "json"
require "function_invoker"

class Handler
  # if FunctionInvoker::Function is too long for you ;)
  Function = FunctionInvoker::Function

  def run(req : String)
    # mistyped function call, with ignore_errors
    data = Function.new("not_a_real_function").invoke(req, ignore_errors:
true)
    data = Function.new("echo", "get").invoke(data)
    data = Function.new("echo").invoke(data.to_s + "MORE DATA")
    return data
  end
end

Handler.new.run("")

Error Handling

For easier error handling, function failures of any kind simply result in a FunctionException being thrown. The original error message is passed along to make sense of the error, however you can simply catch FunctionException if you want to handle errors. Additionally, you can also pass ignore_errors: true to #invoke if you are dealing with a non-mission-critical function that is allowed to fail.

begin
  Function.new("my_func").invoke
rescue ex : FunctionException
  puts ex
end

Function.new("my_func").invoke(ignore_errors: true)

Advanced Usage

If you want to call functions on different gateways, you can use the underlying GetFunction and PostFunction classes.

GetFunction.new("my_function", "gateway:8080").invoke(data)
GetFunction.new("my_function", "my_gateway.com:8080").invoke(data)
PostFunction.new("my_function", "another_gateway.com").invoke(data)

Contributing

  1. Fork it (https://github.com/tpei/function_invoker/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • TPei - creator and maintainer
Repository

function_invoker.cr

Owner
Statistic
  • 1
  • 0
  • 0
  • 0
  • 1
  • almost 6 years ago
  • December 13, 2018
License

MIT License

Links
Synced at

Thu, 07 Nov 2024 19:45:29 GMT

Languages