-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.ts
154 lines (142 loc) · 4.33 KB
/
daemon.ts
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
import { powar } from "./deps.ts";
import { TextLineStream, slugify } from "./deps.ts";
import { Action, Binding, Command } from "./base.ts";
import { Ultra, UltraOpts } from "./mod.ts";
import { CommonActions } from "./common_actions.ts";
import { CommonBindings } from "./common_bindings.ts";
export interface UltraDaemonOpts<Ds> extends UltraOpts {
daemons: (U: UltraDaemon<Ds>, p: powar.ModuleApi) => Ds;
}
export interface UltraDaemon<Ds> extends powar.Module {
commonActions: CommonActions;
commonBindings: CommonBindings;
opts: UltraDaemonOpts<Ds>;
}
// deno-lint-ignore no-explicit-any
export function daemon<Ds extends Daemon<any>[]>(
opts: UltraDaemonOpts<Ds>
): UltraDaemon<Ds> {
return new UltraDaemonImpl(opts);
}
class UltraDaemonImpl<Ds extends Daemon<unknown>[]> implements UltraDaemon<Ds> {
name: string;
path: string;
dependsOn?: string[];
commonActions: CommonActions;
commonBindings: CommonBindings;
daemons: UltraDaemonOpts<Ds>["daemons"];
constructor(public opts: UltraDaemonOpts<Ds>) {
this.name = this.opts.name;
this.path = this.opts.path;
this.dependsOn = this.opts.dependsOn;
this.daemons = this.opts.daemons;
this.commonBindings = new CommonBindings();
this.commonActions = new CommonActions(opts, this.commonBindings);
}
action = async (p: powar.ModuleApi): Promise<void> => {
await Promise.all(
this.daemons(this, p).map(async (d) => {
try {
await launchDaemon(d, this, p);
} catch (e) {
p.warn(`Error when launching daemon ${d.name}: ${e}`);
}
})
);
};
}
export type ExecutionTarget = Exclude<Action, Binding>;
export interface Daemon<D> {
name: string;
startup?: <Ds>(U: UltraDaemon<Ds>, p: powar.ModuleApi) => Promise<void>;
handleMessage: <Ds>(
message: D,
U: UltraDaemon<Ds>,
p: powar.ModuleApi
) => Promise<void>;
}
export function sendToDaemon<D>(spec: Daemon<D>, data: D): Command {
return {
kind: "command",
command: `echo ${JSON.stringify(
JSON.stringify(data)
)} | socat - UNIX-CONNECT:${getUnixSocketPathForDaemon(spec)}`,
};
}
export async function launchDaemon<D, Ds>(
spec: Daemon<D>,
U: UltraDaemon<Ds>,
p: powar.ModuleApi
): Promise<never> {
const listener = await getUnixSocketConnForDaemon(spec);
p.info(`Listening for messages on ${getUnixSocketPathForDaemon(spec)}`);
if (typeof spec.startup !== "undefined") {
await spec.startup(U, p);
p.info(`Ran startup for daemon ${spec.name}`);
}
while (true) {
try {
const conn = await listener.accept();
(async () => {
for await (const line of conn.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())) {
try {
const jsonData = JSON.parse(line);
await spec.handleMessage(jsonData, U, p);
} catch (e) {
p.warn(`Error when handling message: ${e}`);
}
}
})();
} catch (e) {
p.warn(`Daemon error: ${e}`);
}
}
}
function getUnixSocketPathForDaemon<D>(spec: Daemon<D>): string {
return `/tmp/${slugify(spec.name)}.ultra_daemon`;
}
async function getUnixSocketConnForDaemon<D>(
spec: Daemon<D>
): Promise<Deno.Listener> {
const path = getUnixSocketPathForDaemon(spec);
await powar.execute(`rm -f ${path}`);
return await Deno.listen({
transport: "unix",
path: getUnixSocketPathForDaemon(spec),
});
}
export async function installDaemonLaunch(
p: powar.ModuleApi,
name: string,
launchPath: string
): Promise<void> {
const daemonPlist = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${name}</string>
<key>ProgramArguments</key>
<array>
<string>/bin/zsh</string>
<string>-c</string>
<string>${launchPath}</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/daemon-${name}-out.log</string>
<key>StandardOutPath</key>
<string>/tmp/daemon-${name}-err.log</string>
</dict>
</plist>
`;
const plistPath = `$HOME/Library/LaunchAgents/${name}.plist`;
await p.installContents([[daemonPlist, plistPath]]);
await p.exec(`launchctl unload -w "${plistPath}" || true`);
await p.exec(`launchctl load -w "${plistPath}" || exit`);
}