Skip to content

Commit 55d2a11

Browse files
committed
backend/logger: tighter typing
1 parent b497866 commit 55d2a11

File tree

6 files changed

+52
-44
lines changed

6 files changed

+52
-44
lines changed

src/packages/backend/logger.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ process.env.DEBUG_HIDE_DATE = "yes"; // since we supply it ourselves
1212
// otherwise, maybe stuff like this works: (debug as any).inspectOpts["hideDate"] = true;
1313

1414
import debug, { Debugger } from "debug";
15-
import { mkdirSync, createWriteStream, statSync, ftruncate } from "fs";
16-
import { format } from "util";
17-
import { dirname, join } from "path";
15+
16+
import { createWriteStream, ftruncate, mkdirSync, statSync } from "node:fs";
17+
import { dirname, join } from "node:path";
18+
import { format } from "node:util";
19+
1820
import { logs } from "./data";
1921

2022
const MAX_FILE_SIZE_BYTES = 20 * 1024 * 1024; // 20MB
@@ -25,12 +27,12 @@ let _trimLogFileSizePath = "";
2527
export function trimLogFileSize() {
2628
// THIS JUST DOESN'T REALLY WORK!
2729
return;
28-
30+
2931
if (!_trimLogFileSizePath) return;
3032
let stats;
3133
try {
3234
stats = statSync(_trimLogFileSizePath);
33-
} catch(_) {
35+
} catch (_) {
3436
// this happens if the file doesn't exist, which is fine since "trimming" it would be a no-op
3537
return;
3638
}
@@ -141,27 +143,27 @@ function initTransports() {
141143

142144
initTransports();
143145

144-
const DEBUGGERS = {
145-
error: COCALC.extend("error"),
146-
warn: COCALC.extend("warn"),
147-
info: COCALC.extend("info"),
148-
http: COCALC.extend("http"),
149-
verbose: COCALC.extend("verbose"),
150-
debug: COCALC.extend("debug"),
151-
silly: COCALC.extend("silly"),
152-
};
153-
154-
type Level = keyof typeof DEBUGGERS;
155-
156-
const LEVELS: Level[] = [
146+
const LEVELS = [
157147
"error",
158148
"warn",
159149
"info",
160150
"http",
161151
"verbose",
162152
"debug",
163153
"silly",
164-
];
154+
] as const;
155+
156+
type Level = (typeof LEVELS)[number];
157+
158+
const DEBUGGERS: { [key in Level]: Debugger } = {
159+
error: COCALC.extend("error"),
160+
warn: COCALC.extend("warn"),
161+
info: COCALC.extend("info"),
162+
http: COCALC.extend("http"),
163+
verbose: COCALC.extend("verbose"),
164+
debug: COCALC.extend("debug"),
165+
silly: COCALC.extend("silly"),
166+
} as const;
165167

166168
class Logger {
167169
private name: string;
@@ -194,13 +196,13 @@ class Logger {
194196
}
195197

196198
export interface WinstonLogger {
197-
error: Function;
198-
warn: Function;
199-
info: Function;
200-
http: Function;
201-
verbose: Function;
202-
debug: Function;
203-
silly: Function;
199+
error: Debugger;
200+
warn: Debugger;
201+
info: Debugger;
202+
http: Debugger;
203+
verbose: Debugger;
204+
debug: Debugger;
205+
silly: Debugger;
204206
extend: (name: string) => WinstonLogger;
205207
isEnabled: (level: Level) => boolean;
206208
}

src/packages/database/postgres/delete-projects.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { promises as fs } from "node:fs";
1111
import { join } from "node:path";
1212

1313
import { pathToFiles } from "@cocalc/backend/files/path-to-files";
14-
import getLogger from "@cocalc/backend/logger";
14+
import getLogger, { WinstonLogger } from "@cocalc/backend/logger";
1515
import { newCounter } from "@cocalc/backend/metrics";
1616
import { homePath } from "@cocalc/backend/misc";
1717
import getPool from "@cocalc/database/pool";
@@ -22,6 +22,8 @@ import { minutes_ago } from "@cocalc/util/misc";
2222
import { bulkDelete } from "./bulk-delete";
2323
import { PostgreSQL } from "./types";
2424

25+
const { F_OK, R_OK, W_OK } = fs.constants;
26+
2527
const log = getLogger("db:delete-projects");
2628

2729
const delete_projects_prom = newCounter(
@@ -197,10 +199,10 @@ export async function cleanup_old_projects_data(
197199
L2(`delete all project files`);
198200
await deleteProjectFiles(L2, project_id);
199201

200-
L2(`deleting all shared files`);
201202
try {
202203
// this is something like /shared/projects/${project_id}
203204
const shared_path = pathToFiles(project_id, "");
205+
L2(`deleting all shared files in ${shared_path}`);
204206
await fs.rm(shared_path, { recursive: true, force: true });
205207
} catch (err) {
206208
L2(`Unable to delete shared files: ${err}`);
@@ -233,7 +235,7 @@ export async function cleanup_old_projects_data(
233235
}
234236

235237
async function delete_associated_project_data(
236-
L2,
238+
L2: WinstonLogger["debug"],
237239
project_id: string,
238240
): Promise<number> {
239241
let total = 0;
@@ -295,14 +297,18 @@ async function delete_associated_project_data(
295297
return total;
296298
}
297299

298-
async function deleteProjectFiles(L2, project_id: string) {
299-
// TODO: this only works on-prem, and requires the project files to be mounted
300+
async function deleteProjectFiles(
301+
L2: WinstonLogger["debug"],
302+
project_id: string,
303+
) {
304+
// $MOUNTED_PROJECTS_ROOT is for OnPrem and homePath only works in dev/single-user
300305
const projects_root =
301306
process.env["MOUNTED_PROJECTS_ROOT"] ?? homePath(project_id);
302307
if (!projects_root) return;
303308
const project_dir = join(projects_root, project_id);
309+
L2(`attempting to delete all files in ${project_dir}`);
304310
try {
305-
await fs.access(project_dir, fs.constants.F_OK | fs.constants.R_OK);
311+
await fs.access(project_dir, F_OK | R_OK | W_OK);
306312
const stats = await fs.lstat(project_dir);
307313
if (stats.isDirectory()) {
308314
L2(`deleting all files in ${project_dir}`);

src/packages/project/project-status/server.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@ status updates.
1515
Hence in particular, information like cpu, memory and disk are smoothed out and throttled.
1616
*/
1717

18-
import { getLogger } from "@cocalc/project/logger";
19-
import { how_long_ago_m, round1 } from "@cocalc/util/misc";
20-
import { version as smcVersion } from "@cocalc/util/smc-version";
2118
import { delay } from "awaiting";
2219
import { EventEmitter } from "events";
2320
import { isEqual } from "lodash";
24-
import { get_ProjectInfoServer, ProjectInfoServer } from "../project-info";
21+
2522
import { ProjectInfo } from "@cocalc/comm/project-info/types";
2623
import {
2724
ALERT_DISK_FREE,
@@ -36,6 +33,10 @@ import {
3633
ProjectStatus,
3734
} from "@cocalc/comm/project-status/types";
3835
import { cgroup_stats } from "@cocalc/comm/project-status/utils";
36+
import { getLogger } from "@cocalc/project/logger";
37+
import { how_long_ago_m, round1 } from "@cocalc/util/misc";
38+
import { version as smcVersion } from "@cocalc/util/smc-version";
39+
import { get_ProjectInfoServer, ProjectInfoServer } from "../project-info";
3940

4041
// TODO: only return the "next" value, if it is significantly different from "prev"
4142
//function threshold(prev?: number, next?: number): number | undefined {
@@ -83,7 +84,7 @@ export class ProjectStatusServer extends EventEmitter {
8384
constructor(testing = false) {
8485
super();
8586
this.testing = testing;
86-
this.dbg = (...msg) => winston.debug(...msg);
87+
this.dbg = (...msg) => winston.debug(msg[0], ...msg.slice(1));
8788
this.project_info = get_ProjectInfoServer();
8889
}
8990

src/packages/project/usage-info/server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ from the ProjectInfoServer (which collects data about everything)
1212
*/
1313

1414
import { delay } from "awaiting";
15+
import { throttle } from "lodash";
1516
import { EventEmitter } from "node:events";
1617

17-
import { getLogger } from "../logger";
18-
import { ProjectInfoServer, get_ProjectInfoServer } from "../project-info";
1918
import { Process, ProjectInfo } from "@cocalc/comm/project-info/types";
2019
import type { UsageInfo } from "@cocalc/util/types/project-usage-info";
21-
import { throttle } from "lodash";
20+
import { getLogger } from "../logger";
21+
import { ProjectInfoServer, get_ProjectInfoServer } from "../project-info";
2222

2323
const L = getLogger("usage-info:server").debug;
2424

25-
const throttled_dbg = throttle((...args) => L(...args), 10000);
25+
const throttled_dbg = throttle(L, 10000);
2626

2727
function is_diff(prev: UsageInfo, next: UsageInfo, key: keyof UsageInfo) {
2828
// we assume a,b >= 0, hence we leave out Math.abs operations

src/packages/server/software-envs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ async function readConfig(purpose: Purpose): Promise<SoftwareEnvConfig | null> {
6565
// parse the content of softwareFn as json
6666
try {
6767
const software = JSON.parse((await readFile(softwareFn)).toString());
68-
const dbg = (...msg) => L(...msg);
69-
const sanitized = sanitizeSoftwareEnv({ software, registry, purpose }, dbg);
68+
const sanitized = sanitizeSoftwareEnv({ software, registry, purpose }, L);
7069
return sanitized;
7170
} catch (err) {
7271
W(`WARNING: ${softwareFn} is not a valid JSON file -- ${err}`);

src/packages/util/db-schema/site-defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ export const site_settings_conf: SiteSettings = {
655655
},
656656
delete_project_data: {
657657
name: "Delete Project Data",
658-
desc: "When a project has been marked as deleted, also actually delete associated data from the database and (OnPrem only) also its files.",
658+
desc: "When a project has been marked as deleted, also actually delete associated data from the database and – for OnPrem and single-user dev mode only also its files.",
659659
default: "no",
660660
valid: only_booleans,
661661
to_val: to_bool,

0 commit comments

Comments
 (0)