Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
109 lines (93 loc) · 2.67 KB
/
Copy pathindex.ts
File metadata and controls
109 lines (93 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import run, { Temporal } from "@absurd-sqlite/bun-worker";
import { Database } from "bun:sqlite";
import { existsSync, readdirSync } from "node:fs";
import { join } from "node:path";
configureBunSqlite();
await run(async (absurd) => {
await absurd.createQueue("default");
absurd.registerTask(
{
name: "hello",
},
async (params, ctx) => {
await ctx.step("init", async () => {
console.log("init step");
ctx.emitEvent("progress", { message: "Initialization complete" });
return {};
});
await ctx.sleepFor("back off 15s", Temporal.Duration.from({ seconds: 15 }));
await ctx.step("process", async () => {
console.log("process step");
ctx.emitEvent("progress", { message: "Processing complete" });
return {};
});
const name = params.name || "world";
console.log(`Saying hello to ${name}`);
return { greeting: `Hello, ${name}!` };
}
);
setInterval(async () => {
console.log("spawning tasks");
const spawnedTask = await absurd.spawn(
"hello",
{ name: "Alice" },
{ queue: "default" }
);
console.log("Spawned task ID:", spawnedTask.taskID);
}, 3000);
});
function configureBunSqlite(): void {
if (process.platform !== "darwin") {
return;
}
const customSQLite = resolveCustomSQLitePath();
if (!customSQLite) {
throw new Error(
"Bun's SQLite build on macOS does not support extensions. " +
"Install sqlite via Homebrew and set ABSURD_SQLITE_CUSTOM_SQLITE_PATH " +
"to the libsqlite3.dylib path."
);
}
Database.setCustomSQLite(customSQLite);
}
function resolveCustomSQLitePath(): string | null {
const envPath = process.env.ABSURD_SQLITE_CUSTOM_SQLITE_PATH;
if (envPath && existsSync(envPath)) {
return envPath;
}
const prefixes = [
process.env.HOMEBREW_PREFIX,
"/opt/homebrew",
"/usr/local",
].filter(Boolean) as string[];
for (const prefix of prefixes) {
const optPath = join(prefix, "opt", "sqlite", "lib", "libsqlite3.dylib");
if (existsSync(optPath)) {
return optPath;
}
const cellarPath = join(prefix, "Cellar", "sqlite");
if (!existsSync(cellarPath)) {
continue;
}
try {
const entries = readdirSync(cellarPath, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) {
continue;
}
const candidate = join(
cellarPath,
entry.name,
"lib",
"libsqlite3.dylib"
);
if (existsSync(candidate)) {
return candidate;
}
}
} catch {
// fall through to other prefixes
}
}
return null;
}