Swiftpack.co -  suborbital/reactr as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
suborbital/reactr
Function scheduler for Go & WebAssembly
.package(url: "https://github.com/suborbital/reactr.git", from: "v0.10.0")

logo_transparent_wide

Reactr is a fast, performant function scheduling system. Reactr is designed to be flexible, with the ability to run embedded in your Go applications or as a standalone FaaS server, and has first-class support for WebAssembly.

Reactr runs functions called Runnables, and transparently spawns workers to process jobs. Each worker processes jobs in sequence, using Runnables to execute them. Reactr jobs are arbitrary data, and they return arbitrary data (or an error). Jobs are scheduled, and their results can be retreived at a later time.

Wasm

Reactr has support for Wasm-packaged Runnables. The rwasm package contains a multi-tenant Wasm scheduler, an API to grant capabilities to Wasm Runnables, and support for several languages including Rust (stable), TypeScript/AssemblyScript (beta), and Swift (alpha). See wasm and the subo CLI for details.

FaaS

Reactr has early (read: alpha) support for acting as a Functions-as-a-Service system. Reactr can be run as a server, accepting jobs from HTTP/S and making the job results available to be fetched later. See faas for details.

The Basics

First, install Reactr's core package rt:

go get github.com/suborbital/reactr/rt

And then get started by defining something Runnable:

type generic struct{}

// Run runs a generic job
func (g generic) Run(job rt.Job, ctx *rt.Ctx) (interface{}, error) {
	fmt.Println("doing job:", job.String()) // get the string value of the job's data

	// do your work here

	return fmt.Sprintf("finished %s", job.String()), nil
}

// OnChange is called when Reactr starts or stops a worker to handle jobs,
// and allows the Runnable to set up before receiving jobs or tear down if needed.
func (g generic) OnChange(change rt.ChangeEvent) error {
	return nil
}

A Runnable is something that can take care of a job, all it needs to do is conform to the Runnable interface as you see above.

Once you have a Runnable, create a Reactr instance, register it, and Do some work:

package main

import (
	"fmt"
	"log"

	"github.com/suborbital/reactr/rt"
)

func main() {
	r := rt.New()

	r.Register("generic", generic{})

	res := r.Do(r.Job("generic", "hard work"))

	res, err := res.Then()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("done!", res.(string))
}

When you Do some work, you get a Result. A result is like a Rust future or a JavaScript promise, it is something you can get the job's result from once it is finished.

Calling Then() will block until the job is complete, and then give you the return value from the Runnable's Run. Cool, right?

Reactr has some very powerful capabilities, visit the Reactr guide to learn more.

Reactr is being actively developed and has planned improvements, including optimized memory usage, library stability, data persistence, and more. Cheers!

Copyright Suborbital contributors 2021

GitHub

link
Stars: 116
Last commit: 20 hours ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Beta-10
4 days ago

AssemblyScript / TypeScript support is here!

Welcome to Beta-10; this release contains support for AssemblyScript! It's been a while in the making (mostly due to lack of time to work on it), but it's finally here! A few notes about the release: - There is a breaking change; rt.Handle has been renamed to rt.Register - AssemblyScript doesn't currently support setting response headers, accessing static files, or returning errors. Those will come soon. - AssemblyScript Runnable API docs will be coming alongside the next Atmo release, so stay tuned.

You can use subo to create and build AS runnables as of version 0.0.16 with subo create runnable awesomesauce --lang assemblyscript (typescript works also).

This release will be the basis for Atmo v0.2.3, and it will also support AS/TS.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API