Skip to content

Commit

Permalink
improve ws logging
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Dec 22, 2024
1 parent 0fbd7f9 commit fa79417
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 26 deletions.
54 changes: 35 additions & 19 deletions lib/Logging.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AnyObject, ClientSchema } from "prostgles-types";
import { AnyObject, ClientSchema, TableHandler } from "prostgles-types";
import { LocalParams } from "./DboBuilder/DboBuilder";
import { TableHandler } from "./DboBuilder/TableHandler/TableHandler";
import { NOTIF_TYPE, NotifTypeName } from "./PubSubManager/PubSubManager";
import { NotifTypeName } from "./PubSubManager/PubSubManager";

type ClientInfo = {
socketId: string | undefined;
Expand All @@ -18,7 +17,7 @@ export namespace EventTypes {
DebugInfo & {
type: "table";
tableName: string;
command: keyof TableHandler;
command: keyof TableHandler | "sync";
txInfo: AnyObject | undefined;
data: AnyObject;
localParams: LocalParams | undefined;
Expand All @@ -34,7 +33,6 @@ export namespace EventTypes {
| {
command: "syncData";
source: "client" | "trigger";
connectedSocketIds: string[];
lr: string;
}
| {
Expand All @@ -52,37 +50,55 @@ export namespace EventTypes {
socketId: string;
remainingSyncs: string;
remainingSubs: string;
connectedSocketIds: string[];
}
);

export type SyncOrSub = ClientInfo &
DebugInfo & {
type: "syncOrSub";
tableName: string;
localParams?: LocalParams;
connectedSocketIds: string[];
triggers: Record<string, string[]> | undefined;
} & (
| {
type SyncOrSubWithClientInfo = ClientInfo & {
tableName: string;
localParams?: LocalParams;
};
export type SyncOrSub = DebugInfo & {
type: "syncOrSub";
connectedSocketIds: string[];
triggers: Record<string, string[]> | undefined;
} & (
| (SyncOrSubWithClientInfo & {
command: "addTrigger";
state: "ok" | "fail";
/** If no socket id then it's a local subscribe */
socketId: string | undefined;
condition: string;
}
| {
})
| (SyncOrSubWithClientInfo & {
command: "unsubscribe";
channel_name: string;
})
| (SyncOrSubWithClientInfo & {
command: "notifListener";
notifType: NotifTypeName;
dataArr: any[];
}
| {
})
| (SyncOrSubWithClientInfo & {
command: "notifListener.Finished";
op_name: string | undefined;
condition_ids_str: string | undefined;
tableTriggers: string[] | undefined;
tableSyncs: string;
state: "ok" | "error" | "no-triggers" | "invalid_condition_ids";
})
| {
command: "pushSubData";
channel_name: string;
state:
| "sub_not_found"
| "error"
| "Emiting to socket"
| "pushed to local client"
| "no client to push data to"
| "fetch data error";
}
| {
command: "postgresNotifListenManager.create" | "postgresNotifListenManager.destroy";
}
);

Expand Down
21 changes: 18 additions & 3 deletions lib/PubSubManager/PubSubManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ export class PubSubManager {
private constructor(dboBuilder: DboBuilder) {
this.dboBuilder = dboBuilder;

this._log({
type: "syncOrSub",
command: "postgresNotifListenManager.create",
duration: 0,
connectedSocketIds: this.connectedSocketIds,
triggers: this._triggers,
});
log("Created PubSubManager");
}
appCheckFrequencyMS = 10 * 1000;
Expand All @@ -205,9 +212,14 @@ export class PubSubManager {
if (this.appCheck) {
clearInterval(this.appCheck);
}
this.subs = [];
this.syncs = [];
this.postgresNotifListenManager?.destroy();
this._log({
type: "syncOrSub",
command: "postgresNotifListenManager.destroy",
duration: 0,
connectedSocketIds: this.connectedSocketIds,
triggers: this._triggers,
});
};

getIsDestroyed = () => {
Expand Down Expand Up @@ -409,7 +421,10 @@ export class PubSubManager {
return this.dboBuilder.prostgles.connectedSockets.map((s) => s.id);
}
_log = (params: EventTypes.Sync | EventTypes.SyncOrSub) => {
return this.dboBuilder.prostgles.opts.onLog?.({ ...params });
return this.dboBuilder.prostgles.opts.onLog?.({
...params,
connectedSocketIds: this.connectedSocketIds,
});
};

syncTimeout?: ReturnType<typeof setTimeout>;
Expand Down
11 changes: 11 additions & 0 deletions lib/PubSubManager/addSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ export async function addSub(
const isMatch = s.socket?.id === socket.id && s.channel_name === channel_name;
return !isMatch;
});
this._log({
type: "syncOrSub",
command: "unsubscribe",
channel_name,
socketId: socket.id,
duration: 0,
triggers: this._triggers,
connectedSocketIds: this.connectedSocketIds,
sid: socket.id,
tableName: table_name,
});
removeListeners();
cb(null, { res });
});
Expand Down
24 changes: 24 additions & 0 deletions lib/PubSubManager/pushSubData.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import { parseLocalFuncs } from "../DboBuilder/ViewHandler/subscribe";
import { EventTypes } from "../Logging";
import { log, PubSubManager, Subscription } from "./PubSubManager";

export async function pushSubData(this: PubSubManager, sub: Subscription, err?: any) {
const { socket_id, channel_name } = sub;

const onLog = (
state: Extract<EventTypes.SyncOrSub, { type: "syncOrSub"; command: "pushSubData" }>["state"]
) => {
this._log({
type: "syncOrSub",
command: "pushSubData",
channel_name: sub.channel_name,
error: err,
state,
duration: 0,
connectedSocketIds: this.connectedSocketIds,
triggers: this._triggers,
});
};

if (!this.subs.some((s) => s.channel_name === channel_name)) {
// Might be throttling a sub that was removed
onLog("sub_not_found");
return;
}
const localFuncs = parseLocalFuncs(sub.localFuncs);

if (err) {
onLog("error");
if (socket_id) {
this.sockets[socket_id].emit(channel_name, { err });
}
Expand All @@ -24,18 +43,23 @@ export async function pushSubData(this: PubSubManager, sub: Subscription, err?:
if (data) {
if (socket_id && this.sockets[socket_id]) {
log("Pushed " + data.length + " records to sub");
onLog("Emiting to socket");
this.sockets[socket_id].emit(channel_name, { data }, () => {
resolve(data);
});
/* TO DO: confirm receiving data or server will unsubscribe
{ data }, (cb)=> { console.log(cb) });
*/
} else if (localFuncs) {
onLog("pushed to local client");
localFuncs.onData(data);
resolve(data);
} else {
onLog("no client to push data to");
}
// sub.last_throttled = Date.now();
} else {
onLog("fetch data error");
const errObj = { _err_msg: err.toString(), err };
if (socket_id && this.sockets[socket_id]) {
this.sockets[socket_id].emit(channel_name, { err: errObj });
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.198",
"version": "4.2.199",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fa79417

Please sign in to comment.