-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathconfig.ts
More file actions
186 lines (166 loc) · 5.04 KB
/
Copy pathconfig.ts
File metadata and controls
186 lines (166 loc) · 5.04 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Heartbeat Configuration
*
* Parses and manages heartbeat.yml configuration.
*/
import fs from "fs";
import path from "path";
import YAML from "yaml";
import type { HeartbeatEntry, HeartbeatConfig, AutomatonDatabase } from "../types.js";
import { getAutomatonDir } from "../identity/wallet.js";
import { createLogger } from "../observability/logger.js";
const logger = createLogger("heartbeat.config");
const USDC_TOPUP_ENTRY_NAME = "check_usdc_balance";
const USDC_TOPUP_FAST_SCHEDULE = "*/5 * * * *";
const USDC_TOPUP_OLD_SCHEDULE = "0 */12 * * *";
const DEFAULT_HEARTBEAT_CONFIG: HeartbeatConfig = {
entries: [
{
name: "heartbeat_ping",
schedule: "*/15 * * * *",
task: "heartbeat_ping",
enabled: true,
},
{
name: "check_credits",
schedule: "0 */6 * * *",
task: "check_credits",
enabled: true,
},
{
name: "check_usdc_balance",
schedule: USDC_TOPUP_FAST_SCHEDULE,
task: "check_usdc_balance",
enabled: true,
},
{
name: "check_for_updates",
schedule: "0 */4 * * *",
task: "check_for_updates",
enabled: true,
},
{
name: "health_check",
schedule: "*/30 * * * *",
task: "health_check",
enabled: true,
},
{
name: "check_social_inbox",
schedule: "*/2 * * * *",
task: "check_social_inbox",
enabled: true,
},
{
name: "happy_paisa_poke",
schedule: "*/5 * * * *",
task: "happy_paisa_poke",
enabled: true,
},
],
defaultIntervalMs: 60_000,
lowComputeMultiplier: 4,
};
/**
* Load heartbeat config from YAML file, falling back to defaults.
*/
export function loadHeartbeatConfig(configPath?: string): HeartbeatConfig {
const filePath =
configPath || path.join(getAutomatonDir(), "heartbeat.yml");
if (!fs.existsSync(filePath)) {
return DEFAULT_HEARTBEAT_CONFIG;
}
try {
const raw = fs.readFileSync(filePath, "utf-8");
const parsed = YAML.parse(raw) || {};
const parsedEntries = (parsed.entries || []).map((e: any) => ({
name: e.name,
schedule: e.schedule,
task: e.task,
enabled: e.enabled !== false,
params: e.params,
})) as HeartbeatEntry[];
const entries = mergeWithDefaults(parsedEntries);
return {
entries,
defaultIntervalMs:
parsed.defaultIntervalMs ?? DEFAULT_HEARTBEAT_CONFIG.defaultIntervalMs,
lowComputeMultiplier:
parsed.lowComputeMultiplier ??
DEFAULT_HEARTBEAT_CONFIG.lowComputeMultiplier,
};
} catch (error: any) {
logger.error("Failed to parse YAML config", error instanceof Error ? error : undefined);
// Continue with defaults, but log the error
return DEFAULT_HEARTBEAT_CONFIG;
}
}
/**
* Save heartbeat config to YAML file.
*/
export function saveHeartbeatConfig(
config: HeartbeatConfig,
configPath?: string,
): void {
const filePath =
configPath || path.join(getAutomatonDir(), "heartbeat.yml");
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
}
fs.writeFileSync(filePath, YAML.stringify(config), { mode: 0o600 });
}
/**
* Write the default heartbeat.yml file.
*/
export function writeDefaultHeartbeatConfig(configPath?: string): void {
saveHeartbeatConfig(DEFAULT_HEARTBEAT_CONFIG, configPath);
}
/**
* Sync heartbeat entries from YAML config into the database.
*/
export function syncHeartbeatToDb(
config: HeartbeatConfig,
db: AutomatonDatabase,
): void {
for (const entry of config.entries) {
db.upsertHeartbeatEntry(entry);
}
}
function mergeWithDefaults(entries: HeartbeatEntry[]): HeartbeatEntry[] {
const defaults = DEFAULT_HEARTBEAT_CONFIG.entries.map((entry) => ({ ...entry }));
const defaultsByName = new Map(defaults.map((entry) => [entry.name, entry]));
const mergedByName = new Map(defaultsByName);
for (const entry of entries) {
if (!entry?.name) continue;
const fallback = defaultsByName.get(entry.name);
mergedByName.set(entry.name, {
...(fallback || {}),
...entry,
enabled: entry.enabled !== false,
task: entry.task || fallback?.task || "",
schedule: entry.schedule || fallback?.schedule || "",
});
}
const fallbackTopup = defaultsByName.get(USDC_TOPUP_ENTRY_NAME);
if (fallbackTopup) {
const current = mergedByName.get(USDC_TOPUP_ENTRY_NAME) || fallbackTopup;
const migratedSchedule = current.schedule?.trim() === USDC_TOPUP_OLD_SCHEDULE
? USDC_TOPUP_FAST_SCHEDULE
: current.schedule || fallbackTopup.schedule;
mergedByName.set(USDC_TOPUP_ENTRY_NAME, {
...fallbackTopup,
...current,
task: current.task || fallbackTopup.task,
schedule: migratedSchedule,
});
}
const orderedDefaultEntries = defaults.map(
(defaultEntry) => mergedByName.get(defaultEntry.name) || defaultEntry,
);
const knownNames = new Set(defaults.map((entry) => entry.name));
const customEntries = [...mergedByName.values()].filter(
(entry) => !knownNames.has(entry.name),
);
return [...orderedDefaultEntries, ...customEntries];
}