-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup.js
More file actions
67 lines (54 loc) · 1.99 KB
/
Copy pathstartup.js
File metadata and controls
67 lines (54 loc) · 1.99 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
/*
run locally with:
SQLITE_PATH=/tmp/data.db && LSSQLITE_REPLICA_URL='gcs://BUCKET/data/data.db' && deno run -A --unstable startup.js
ref
https://litestream.io/guides/docker/
https://litestream.io/guides/gcs/
https://github.com/steren/litestream-cloud-run-example
https://github.com/denodrivers/sqlite3/blob/main/doc.md
* */
import { pafs, Database, exists } from "./deps.js";
const { SQLITE_PATH = '/tmp/data.db', LSSQLITE_REPLICA_URL = 'gcs://BUCKET/data/data.db' } = Deno.env.toObject();
async function _setupSqlite(path, url){
console.log(`startup.js setup sqlite "${ path }" "${ url }"`);
try{
if(true === await exists(path)){
// restore on startup
await Deno.remove(path);
}
const dir = pafs.dirname(path);
if(false === await exists(dir)){
await Deno.mkdir( dir, {recursive:true} );
};
// creates db, if replica exists
let cmd = new Deno.Command("litestream", { args: ['restore', '-v', '-if-replica-exists', '-o', path, url] });
await cmd.output();
}catch(error){
console.log(`startup.js setup failed "${error?.message ?? error}"`, {error});
}
}
await _setupSqlite(SQLITE_PATH, LSSQLITE_REPLICA_URL);
const litestream = new Deno.Command("litestream", { args: ['replicate', SQLITE_PATH, LSSQLITE_REPLICA_URL] });
const child = litestream.spawn();
const db = new Database(SQLITE_PATH);
db.exec('PRAGMA synchronous = NORMAL');
db.exec('PRAGMA wal_autocheckpoint = 0');
/*
db.exec(`CREATE TABLE IF NOT EXISTS n_time_table (id INTEGER PRIMARY KEY AUTOINCREMENT, n NUMERIC DEFAULT 0, t TEXT DEFAULT "");`);
*/
globalThis.db = db;
const controller = new AbortController();
globalThis.controller = controller;
Deno.addSignalListener('SIGTERM', () => {
// gcr sends sigterm on container shutdown (no traffic, etc)
console.log("...shutdown. sigterm. (cloud)");
controller.abort();
Deno.exit();
});
Deno.addSignalListener("SIGINT", () => {
// ^c from terminal in local development
console.log("...shutdown. sigint. (dev)");
controller.abort();
Deno.exit();
});
await import('./http.js');