Skip to content

Commit 7de0696

Browse files
committed
backend/logger: tighter typing
1 parent b497866 commit 7de0696

File tree

6 files changed

+51
-45
lines changed

6 files changed

+51
-45
lines changed

src/packages/backend/logger.ts

+28-26
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

+11-7
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";
@@ -197,11 +197,11 @@ export async function cleanup_old_projects_data(
197197
L2(`delete all project files`);
198198
await deleteProjectFiles(L2, project_id);
199199

200-
L2(`deleting all shared files`);
201200
try {
202201
// this is something like /shared/projects/${project_id}
203202
const shared_path = pathToFiles(project_id, "");
204-
await fs.rm(shared_path, { recursive: true, force: true });
203+
L2(`deleting all shared files in ${shared_path}`);
204+
//await fs.rm(shared_path, { recursive: true, force: true });
205205
} catch (err) {
206206
L2(`Unable to delete shared files: ${err}`);
207207
}
@@ -233,7 +233,7 @@ export async function cleanup_old_projects_data(
233233
}
234234

235235
async function delete_associated_project_data(
236-
L2,
236+
L2: WinstonLogger["debug"],
237237
project_id: string,
238238
): Promise<number> {
239239
let total = 0;
@@ -295,18 +295,22 @@ async function delete_associated_project_data(
295295
return total;
296296
}
297297

298-
async function deleteProjectFiles(L2, project_id: string) {
299-
// TODO: this only works on-prem, and requires the project files to be mounted
298+
async function deleteProjectFiles(
299+
L2: WinstonLogger["debug"],
300+
project_id: string,
301+
) {
302+
// $MOUNTED_PROJECTS_ROOT is for OnPrem and homePath only works in dev/single-user
300303
const projects_root =
301304
process.env["MOUNTED_PROJECTS_ROOT"] ?? homePath(project_id);
302305
if (!projects_root) return;
303306
const project_dir = join(projects_root, project_id);
307+
L2(`attempting to delete all files in ${project_dir}`);
304308
try {
305309
await fs.access(project_dir, fs.constants.F_OK | fs.constants.R_OK);
306310
const stats = await fs.lstat(project_dir);
307311
if (stats.isDirectory()) {
308312
L2(`deleting all files in ${project_dir}`);
309-
await fs.rm(project_dir, { recursive: true, force: true });
313+
//await fs.rm(project_dir, { recursive: true, force: true });
310314
} else {
311315
L2(`is not a directory: ${project_dir}`);
312316
}

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

+6-5
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

+4-4
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

+1-2
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

+1-1
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)