-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import express, { Request, Response } from "express"; | ||
import { Resonate, Context } from "./lib/async"; | ||
|
||
async function longRunningFunction(ctx: Context): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve("Hello World"); | ||
}, 20000); | ||
}); | ||
} | ||
|
||
// Initialize a Resonate application. | ||
const resonate = new Resonate(); | ||
|
||
// Register a function as a Resonate function | ||
resonate.register("longRunningFunction", longRunningFunction, resonate.options({ timeout: 60000 })); | ||
|
||
// Start the Resonate application | ||
resonate.start(); | ||
|
||
// Initialize an Express application. | ||
const app = express().use(express.json()); | ||
|
||
// Start the process and return its id | ||
app.post("/begin/:fn/:id", async (req: Request, res: Response) => { | ||
const fn = req.params.fn; | ||
const id = req.params.id; | ||
|
||
try { | ||
// Call the resonate function | ||
let promise = resonate.run(fn, `${fn}-${id}`); | ||
// Make sure the request is reliably recorded. Important for recovery | ||
await promise.created; | ||
res.send(`/begin/${fn}/${id}`); | ||
} catch (e) { | ||
res.status(500).send(`An error occurred: ${e}`); | ||
} | ||
}); | ||
|
||
// Wait for the process to complete | ||
app.post("/await/:fn/:id", async (req: Request, res: Response) => { | ||
const fn = req.params.fn; | ||
const id = req.params.id; | ||
|
||
try { | ||
// Call the resonate function | ||
let value = await resonate.run(fn, `${fn}-${id}`); | ||
res.send(value); | ||
} catch (e) { | ||
res.status(500).send(`An error occurred: ${e}`); | ||
} | ||
}); | ||
|
||
// Start the Express application | ||
app.listen(3000, () => { | ||
console.log("Listening on port 3000"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
import { Context, Resonate } from "./lib"; | ||
|
||
import { TFC, LFC, RFC } from "./lib/core/calls"; | ||
|
||
const resonate = new Resonate({ | ||
url: "http://localhost:8001", | ||
timeout: 60000, | ||
}); | ||
|
||
resonate.register("f", async (ctx: Context) => { | ||
const a = await ctx.run(new LFC(foo, [1, 2, 3])); | ||
console.log(a); | ||
|
||
const b = await ctx.run(new RFC(foo)); | ||
console.log(b); | ||
}); | ||
|
||
function foo(ctx: Context, a: number, b: number, c: number) { | ||
console.log(a, b, c); | ||
|
||
if(a !== 0) { | ||
return ctx.run(new LFC(foo, [a - 1, b - 1, c - 1])); | ||
} | ||
|
||
return "foo"; | ||
} | ||
|
||
resonate.run(new TFC("f", "f.1")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Resonate, Context } from './lib/async'; | ||
|
||
const resonate = new Resonate({ | ||
timeout: Number.MAX_SAFE_INTEGER, | ||
}); | ||
|
||
const foo = resonate.register("foo", async (ctx: Context) => { | ||
await new Promise((resolve) => setTimeout(resolve, 10000)); | ||
return "foo"; | ||
}); | ||
|
||
async function main() { | ||
// const f = resonate.run("foo", "foo.0"); | ||
const f = foo("foo.0"); | ||
|
||
const p = await f.created; | ||
console.log(p); | ||
|
||
const r = await f; | ||
console.log(r); | ||
} | ||
|
||
main(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Resonate, Context } from './lib/async'; | ||
|
||
const resonate = new Resonate({ | ||
timeout: Number.MAX_SAFE_INTEGER, | ||
}); | ||
|
||
const foo = resonate.register("foo", async (ctx: Context) => { | ||
// await new Promise((resolve) => setTimeout(resolve, 10000)); | ||
// return "foo"; | ||
return await ctx.run(() => "foo", ctx.options({ durable: true })); | ||
}, { durable: false }); | ||
|
||
async function main() { | ||
// const f = resonate.run("foo", "foo.0"); | ||
const f = foo("foo.0"); | ||
|
||
const p = await f.created; | ||
console.log(p); | ||
|
||
console.log("1"); | ||
|
||
const r = await f; | ||
console.log("HAR 1!", r); | ||
|
||
const f2 = foo("foo.0"); | ||
console.log(f); | ||
console.log(f2); | ||
|
||
const p2 = await f2.created; | ||
console.log(p2); | ||
|
||
console.log("a"); | ||
const r2 = await f2; | ||
console.log("b"); | ||
console.log("HAR 2!", r2); | ||
} | ||
|
||
main(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Resonate } from "./lib"; | ||
|
||
// instantiate resonate | ||
const resonate = new Resonate({ | ||
url: "http://localhost:8001", | ||
}); | ||
|
||
async function main() { | ||
// create a durable promise | ||
const durablePromise = await resonate.promises.create("myPromise", Date.now() + 60000, { | ||
idempotencyKey: "myIdempotencyKey", | ||
}); | ||
|
||
// resolve the durable promise | ||
await durablePromise.resolve("Hello, World!"); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Resonate } from "./lib"; | ||
|
||
// instantiate resonate | ||
const resonate = new Resonate({ | ||
url: "http://localhost:8001", | ||
}); | ||
|
||
async function main() { | ||
// create a durable promise | ||
const durablePromise = await resonate.promises.create("myPromise", Date.now() + 60000, { | ||
idempotencyKey: "myIdempotencyKey", | ||
}); | ||
|
||
// wait for the durable promise to be resolved | ||
const v = await durablePromise.wait(30000); | ||
console.log(v); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Resonate, Context } from "./lib"; | ||
|
||
const resonate = new Resonate(); | ||
|
||
resonate.schedule("everyMinute", "* * * * *", (ctx: Context) => { | ||
console.log("invoked!") | ||
}); | ||
|
||
resonate.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Resonate, Context } from "./lib"; | ||
|
||
const resonate = new Resonate({ | ||
url: "http://localhost:8001", | ||
}); | ||
|
||
resonate.register("myFunction", async (c : Context) => { | ||
let v = await c.run("my/unique/id"); | ||
console.log(v); | ||
}); | ||
|
||
resonate.run("myFunction", "myFunction.1"); | ||
|
||
// From somewhere else in your process or an entirely different process | ||
setTimeout(() => { | ||
resonate.promises.resolve("my/unique/id", 42); | ||
}, 1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Resonate, Context } from "./lib"; | ||
|
||
const resonate = new Resonate({ | ||
timeout: 60000, | ||
url: "http://localhost:8001" | ||
}); | ||
|
||
resonate.register("sleep", async (ctx: Context) => { | ||
console.log("time to sleep"); | ||
await ctx.sleep(30000); | ||
console.log("aaand we done!") | ||
}); | ||
|
||
resonate.run("sleep", "sleep.1"); |