Skip to content

Commit 5a18bf1

Browse files
committed
hydrate pico.rulesets on startup before install
1 parent e4fa8ba commit 5a18bf1

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Pico.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ export class Pico {
6060
channels: { [eci: string]: Channel } = {};
6161
rulesets: {
6262
[rid: string]: {
63-
instance: RulesetInstance;
6463
config: RulesetConfig;
64+
65+
// instance may be null when it's pending startup
66+
instance: RulesetInstance | null;
67+
startupError?: any;
6568
};
6669
} = {};
6770

@@ -340,6 +343,7 @@ export class Pico {
340343
const ctx = createRulesetContext(this.pf, this, { rid, config });
341344
const instance = await rs.init(ctx, this.pf.environment);
342345
this.rulesets[rid].instance = instance;
346+
delete this.rulesets[rid].startupError;
343347
}
344348
}
345349

@@ -492,7 +496,7 @@ export class Pico {
492496
try {
493497
while ((this.current = this.schedule.shift())) {
494498
const rs = this.rulesets[this.current.rid];
495-
if (rs && rs.instance.event) {
499+
if (rs?.instance?.event) {
496500
// must process one event at a time to maintain the pico's single-threaded guarantee
497501
const response = await rs.instance.event(
498502
this.current.event,
@@ -510,7 +514,7 @@ export class Pico {
510514
if (!rs) {
511515
throw new Error(`Pico doesn't have ${txn.query.rid} installed.`);
512516
}
513-
const qfn = rs.instance.query && rs.instance.query[txn.query.name];
517+
const qfn = rs?.instance?.query && rs.instance.query[txn.query.name];
514518
if (!qfn) {
515519
throw new Error(
516520
`Ruleset ${txn.query.rid} does not have query function "${txn.query.name}"`

src/PicoFramework.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,18 @@ export class PicoFramework {
9797
await dbRange(this.db, { prefix: ["pico-ruleset"] }, async (data) => {
9898
const picoId = data.key[1];
9999
const rid = data.key[2];
100+
const config = data.value.config;
100101
const pico = this.picos.find((pico) => pico.id === picoId);
101102
if (!pico) {
102103
throw new Error(`Missing picoId ${picoId}`);
103104
}
105+
// load ruleset map so when rulesets startup they can see all the available ruleset+config on the pico
106+
pico.rulesets[rid] = { config, instance: null };
104107
try {
105108
const rs = await this.rulesetLoader(picoId, rid, data.value.config);
106109
toInstall.push({ pico, rs, config: data.value.config });
107110
} catch (error) {
111+
pico.rulesets[rid] = { config, instance: null, startupError: error };
108112
this.emit({
109113
type: "startupRulesetInitError",
110114
picoId,

0 commit comments

Comments
 (0)