Skip to content

Commit

Permalink
Update docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
dfarr committed May 17, 2024
1 parent 2cbe666 commit 87b8098
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
57 changes: 57 additions & 0 deletions a.ts
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");
});
29 changes: 29 additions & 0 deletions app.ts
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"));
24 changes: 24 additions & 0 deletions app2.ts
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();

39 changes: 39 additions & 0 deletions app3.ts
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();

18 changes: 18 additions & 0 deletions bar.ts
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();
19 changes: 19 additions & 0 deletions foo.ts
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();
2 changes: 1 addition & 1 deletion lib/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class Context {
*
* @template T The return type of the remote function.
* @param fc An instance of a function call.
* @returns A promise that resolves to the resolved value of the remote function.
* @returns A promise that resolves to the return value of the function or the resolved value of the remote function.
*/
run<T>(fc: LFC | RFC): ResonatePromise<T>;

Expand Down
8 changes: 8 additions & 0 deletions lib/resonate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ export abstract class ResonateBase {
* @returns A promise that resolve to the function return value.
*/
run<T>(name: string, id: string, ...argsWithOpts: [...any, PartialOptions?]): ResonatePromise<T>;

/**
* Invoke a function.
*
* @template T The return type of the remote function.
* @param fc An instance of a function call.
* @returns A promise that resolve to the function return value.
*/
run<T>(fc: TFC): ResonatePromise<T>;
run(nameOrFc: string | TFC, ...argsWithOpts: [...any, PartialOptions?]): ResonatePromise<any> {
// extract id
Expand Down
9 changes: 9 additions & 0 deletions schedule.ts
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();
17 changes: 17 additions & 0 deletions slack.ts
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);
14 changes: 14 additions & 0 deletions sleep.ts
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");

0 comments on commit 87b8098

Please sign in to comment.