add support heartbeat to couchdb-changes-stream
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
import webpush from "web-push";
|
||||
|
||||
import { CouchDBChangesStream } from "couchdb-changes-stream";
|
||||
import { CouchDBChangesStream } from "@hereconnect/couchdb-changes-stream";
|
||||
import { qrDb, messagesDbUrl, webPushSubscriptionsDb } from "./dbs";
|
||||
import { setupVapidDetails } from "./setupVapidDetails";
|
||||
import type { MessageDocument } from "@hereconnect/types";
|
||||
import { ServiceSeqTracker } from "./serviceSeqTracker";
|
||||
import { startHealthServer } from "./health-server";
|
||||
|
||||
const serviceSeqTracker = new ServiceSeqTracker();
|
||||
const abortController = new AbortController();
|
||||
|
||||
startHealthServer(abortController);
|
||||
|
||||
const start = async () => {
|
||||
await setupVapidDetails();
|
||||
const since = await serviceSeqTracker.getSince();
|
||||
const serviceSeqTracker = await ServiceSeqTracker.Init(
|
||||
"_local/service:message-delivery-method-web-push",
|
||||
);
|
||||
|
||||
const changesStream = new CouchDBChangesStream<MessageDocument>(
|
||||
messagesDbUrl,
|
||||
{
|
||||
since,
|
||||
abortController,
|
||||
since: serviceSeqTracker.lastSeq,
|
||||
feed: "continuous",
|
||||
include_docs: true,
|
||||
heartbeat: 1000,
|
||||
@@ -28,12 +34,12 @@ const start = async () => {
|
||||
|
||||
// Gracefully handle SIGINT
|
||||
process.on("SIGINT", async () => {
|
||||
changesStream.stop();
|
||||
changesStream?.stop();
|
||||
process.stdout.write("\n");
|
||||
process.exit(2);
|
||||
});
|
||||
|
||||
console.info("Service started from sequence", since);
|
||||
console.info("Service started from sequence", serviceSeqTracker.lastSeq);
|
||||
|
||||
for await (const change of changesStream) {
|
||||
const { seq, doc: msgDoc } = change;
|
||||
@@ -68,7 +74,7 @@ const start = async () => {
|
||||
|
||||
for (const row of rows) {
|
||||
try {
|
||||
const subscription = row.doc.subscription;
|
||||
const subscription = row.doc!.subscription;
|
||||
const topic = `chat-${msgDoc.qr_code_uri}-${msgDoc.chat}`;
|
||||
const tag = `${topic}-${(new Date(msgDoc.created_at).getTime() - new Date(qrDoc.created_at).getTime()).toString(36)}`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user