Skip to content

Commit d1d09e6

Browse files
fix(relay): refresh watched Android activity timestamps
1 parent 6df36b6 commit d1d09e6

2 files changed

Lines changed: 49 additions & 35 deletions

File tree

‎infra/relay/scripts/android-push-smoke.ts‎

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,50 @@ const decodeDevice = Schema.decodeUnknownEffect(Schema.fromJsonString(Device));
2424
const Phase = Schema.Literals(["running", "approval", "input", "completed", "failed", "end"]);
2525
const decodePhase = Schema.decodeUnknownEffect(Phase);
2626

27-
class SmokeError extends Schema.TaggedError<SmokeError>()("SmokeError", {
28-
reason: Schema.Literals(["usage", "read-credentials", "read-device", "unregistered"]),
29-
cause: Schema.optional(Schema.Defect()),
30-
}) {
27+
class SmokeUsageError extends Schema.TaggedError<SmokeUsageError>()("SmokeUsageError", {}) {
3128
override get message() {
32-
switch (this.reason) {
33-
case "usage":
34-
return "Usage: node scripts/android-push-smoke.ts <service-account.json> <device.json> <running|approval|input|completed|failed|end>";
35-
case "read-credentials":
36-
return "Could not read service-account file.";
37-
case "read-device":
38-
return "Could not read device file.";
39-
case "unregistered":
40-
return "This device token is no longer registered with Firebase.";
41-
}
29+
return "Usage: node scripts/android-push-smoke.ts <service-account.json> <device.json> <running|approval|input|completed|failed|end>";
30+
}
31+
}
32+
33+
class SmokeCredentialReadError extends Schema.TaggedError<SmokeCredentialReadError>()(
34+
"SmokeCredentialReadError",
35+
{ cause: Schema.Defect() },
36+
) {
37+
override get message() {
38+
return "Could not read service-account file.";
39+
}
40+
}
41+
42+
class SmokeDeviceReadError extends Schema.TaggedError<SmokeDeviceReadError>()(
43+
"SmokeDeviceReadError",
44+
{ cause: Schema.Defect() },
45+
) {
46+
override get message() {
47+
return "Could not read device file.";
48+
}
49+
}
50+
51+
class SmokeUnregisteredDeviceError extends Schema.TaggedError<SmokeUnregisteredDeviceError>()(
52+
"SmokeUnregisteredDeviceError",
53+
{},
54+
) {
55+
override get message() {
56+
return "This device token is no longer registered with Firebase.";
4257
}
4358
}
4459

4560
const main = Effect.gen(function* () {
4661
const [credentialPath, devicePath, phaseArg] = process.argv.slice(2);
47-
if (!credentialPath || !devicePath || !phaseArg)
48-
return yield* new SmokeError({
49-
reason: "usage",
50-
});
62+
if (!credentialPath || !devicePath || !phaseArg) return yield* new SmokeUsageError({});
5163
const phase = yield* decodePhase(phaseArg);
5264
const credentials = yield* Effect.tryPromise({
5365
try: () => NodeFSP.readFile(credentialPath, "utf8"),
54-
catch: (cause) => new SmokeError({ reason: "read-credentials", cause }),
66+
catch: (cause) => new SmokeCredentialReadError({ cause }),
5567
});
5668
const device = yield* Effect.tryPromise({
5769
try: () => NodeFSP.readFile(devicePath, "utf8"),
58-
catch: (cause) => new SmokeError({ reason: "read-device", cause }),
70+
catch: (cause) => new SmokeDeviceReadError({ cause }),
5971
}).pipe(Effect.flatMap(decodeDevice));
6072
const title =
6173
phase === "completed"
@@ -131,10 +143,7 @@ const main = Effect.gen(function* () {
131143
),
132144
),
133145
);
134-
if (result.unregistered)
135-
return yield* new SmokeError({
136-
reason: "unregistered",
137-
});
146+
if (result.unregistered) return yield* new SmokeUnregisteredDeviceError({});
138147
yield* Effect.logInfo(
139148
`Firebase accepted the ${phase} notification. Verify delivery on the device.`,
140149
);

‎infra/relay/scripts/android-push-watch.ts‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,23 @@ const Connection = Schema.Struct({
4242
});
4343
const readFile = (path: string) => Effect.tryPromise(() => NodeFSP.readFile(path, "utf8"));
4444
const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
45-
class WatchError extends Schema.TaggedError<WatchError>()("AndroidPushWatchError", {
46-
reason: Schema.Literals(["unregistered", "stopped"]),
47-
cause: Schema.optional(Schema.Defect()),
45+
class WatchUnregisteredDeviceError extends Schema.TaggedError<WatchUnregisteredDeviceError>()(
46+
"WatchUnregisteredDeviceError",
47+
{},
48+
) {
49+
override get message() {
50+
return "Device token is no longer registered";
51+
}
52+
}
53+
54+
class WatchStoppedError extends Schema.TaggedError<WatchStoppedError>()("WatchStoppedError", {
55+
cause: Schema.Defect(),
4856
}) {
4957
override get message() {
50-
return this.reason === "unregistered"
51-
? "Device token is no longer registered"
52-
: "Android push watcher stopped. Check the private connection and Firebase configuration.";
58+
return "Android push watcher stopped. Check the private connection and Firebase configuration.";
5359
}
5460
}
61+
5562
const preferences = {
5663
notificationsEnabled: true,
5764
liveActivitiesEnabled: true,
@@ -170,9 +177,7 @@ const main = Effect.gen(function* () {
170177
nowMs: now,
171178
});
172179
const active = (aggregate?.activeCount ?? 0) > 0;
173-
const same =
174-
encodeJson([...next.values()].map(({ updatedAt: _, ...value }) => value)) ===
175-
encodeJson([...states.values()].map(({ updatedAt: _, ...value }) => value));
180+
const same = encodeJson([...next.values()]) === encodeJson([...states.values()]);
176181
states = next;
177182
if ((!active && !previouslyActive && !alert) || (same && !alert)) return;
178183
const result = yield* sender.send({
@@ -188,7 +193,7 @@ const main = Effect.gen(function* () {
188193
...alert,
189194
}),
190195
});
191-
if (result.unregistered) return yield* new WatchError({ reason: "unregistered" });
196+
if (result.unregistered) return yield* new WatchUnregisteredDeviceError({});
192197
previouslyActive = active;
193198
yield* Effect.logInfo(
194199
`Android push accepted: ${state?.phase ?? (active ? "active" : "ended")}`,
@@ -202,7 +207,7 @@ const main = Effect.gen(function* () {
202207
NodeRuntime.runMain(
203208
main.pipe(
204209
Effect.scoped,
205-
Effect.catchCause((cause) => Effect.fail(new WatchError({ reason: "stopped", cause }))),
210+
Effect.catchCause((cause) => Effect.fail(new WatchStoppedError({ cause }))),
206211
// Socket failures may contain credential-bearing request headers. Keep the
207212
// cause on the error, but print only the fixed message at the CLI boundary.
208213
Effect.tapError((error) => Effect.logError(error.message)),

0 commit comments

Comments
 (0)