forked from rocicorp/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzero-cache-dev.ts
More file actions
168 lines (147 loc) · 4.58 KB
/
zero-cache-dev.ts
File metadata and controls
168 lines (147 loc) · 4.58 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env node
/* eslint-disable no-console */
import '@dotenvx/dotenvx/config';
import {resolver} from '@rocicorp/resolver';
import chalk from 'chalk';
import {watch} from 'chokidar';
import {spawn, type ChildProcess} from 'node:child_process';
import {parseOptionsAdvanced} from '../../shared/src/options.ts';
import {
ZERO_ENV_VAR_PREFIX,
zeroOptions,
} from '../../zero-cache/src/config/zero-config.ts';
import {deployPermissionsOptions} from '../../zero-cache/src/scripts/permissions.ts';
const deployPermissionsScript = 'zero-deploy-permissions';
const zeroCacheScript = 'zero-cache';
function killProcess(childProcess: ChildProcess | undefined) {
if (!childProcess || childProcess.exitCode !== null) {
return Promise.resolve();
}
const {resolve, promise} = resolver();
childProcess.on('exit', resolve);
// Use SIGQUIT in particular since this will cause
// a fast zero-cache shutdown instead of a graceful drain.
childProcess.kill('SIGQUIT');
return promise;
}
function log(msg: string) {
console.log(chalk.green('> ' + msg));
}
function logWarn(msg: string) {
console.log(chalk.yellow('> ' + msg));
}
function logError(msg: string) {
console.error(chalk.red('> ' + msg));
}
async function main() {
const {config} = parseOptionsAdvanced(
{
...deployPermissionsOptions,
...zeroOptions,
},
process.argv.slice(2),
ZERO_ENV_VAR_PREFIX,
false,
true, // allowPartial, required by server/runner/config.ts
);
const {unknown: zeroCacheArgs} = parseOptionsAdvanced(
deployPermissionsOptions,
process.argv.slice(2),
ZERO_ENV_VAR_PREFIX,
true,
);
const {unknown: deployPermissionsArgs} = parseOptionsAdvanced(
zeroOptions,
process.argv.slice(2),
ZERO_ENV_VAR_PREFIX,
true,
);
const {path} = config.schema;
let permissionsProcess: ChildProcess | undefined;
let zeroCacheProcess: ChildProcess | undefined;
// Ensure child processes are killed when the main process exits
process.on('exit', () => {
permissionsProcess?.kill('SIGQUIT');
zeroCacheProcess?.kill('SIGQUIT');
});
async function deployPermissions(): Promise<boolean> {
if (config.upstream.type !== 'pg') {
logWarn(
`Skipping permissions deployment for ${config.upstream.type} upstream`,
);
return true;
}
permissionsProcess?.removeAllListeners('exit');
await killProcess(permissionsProcess);
permissionsProcess = undefined;
log(`Running ${deployPermissionsScript}.`);
permissionsProcess = spawn(
deployPermissionsScript,
deployPermissionsArgs ?? [],
{
stdio: 'inherit',
shell: true,
},
);
const {promise: code, resolve} = resolver<number>();
permissionsProcess.on('exit', resolve);
if ((await code) === 0) {
log(`${deployPermissionsScript} completed successfully.`);
return true;
}
logError(`Failed to deploy permissions from ${path}.`);
return false;
}
async function deployPermissionsAndStartZeroCache() {
zeroCacheProcess?.removeAllListeners('exit');
await killProcess(zeroCacheProcess);
zeroCacheProcess = undefined;
if (await deployPermissions()) {
log(
`Running ${zeroCacheScript} at\n\n\thttp://localhost:${config.port}\n`,
);
zeroCacheProcess = spawn(zeroCacheScript, zeroCacheArgs || [], {
env: {
// Set some low defaults so as to use fewer resources and not trip up,
// e.g. developers sharing a database.
['ZERO_NUM_SYNC_WORKERS']: '3',
['ZERO_CVR_MAX_CONNS']: '6',
['ZERO_UPSTREAM_MAX_CONNS']: '6',
// But let the developer override any of these dev defaults.
...process.env,
},
stdio: 'inherit',
shell: true,
});
zeroCacheProcess.on('exit', () => {
logError(`${zeroCacheScript} exited. Exiting.`);
process.exit(-1);
});
}
}
await deployPermissionsAndStartZeroCache();
// Watch for file changes
const watcher = watch(path, {
ignoreInitial: true,
awaitWriteFinish: {stabilityThreshold: 500, pollInterval: 100},
});
const onFileChange = async () => {
log(`Detected ${path} change.`);
await deployPermissions();
};
watcher.on('add', onFileChange);
watcher.on('change', onFileChange);
watcher.on('unlink', onFileChange);
}
process.on('unhandledRejection', reason => {
logError('Unexpected unhandled rejection.');
console.error(reason);
logError('Exiting');
process.exit(-1);
});
main().catch(e => {
logError(`Unexpected unhandled error.`);
console.error(e);
logError('Exiting.');
process.exit(-1);
});