Skip to content

Commit

Permalink
Improve error resiliance in logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Jul 2, 2024
1 parent e729e3c commit f3c3ff3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
12 changes: 6 additions & 6 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
},

"tasks": {
"update-deps": "deno run --allow-read=. --allow-net=jsr.io,registry.npmjs.org jsr:@check/deps",
"check": "deno fmt --check && deno lint && deno check pup.ts && deno test --allow-read --allow-write --allow-env --allow-net --allow-sys --allow-run --coverage=cov_profile && echo \"Generating coverage\" && deno coverage cov_profile --exclude=pup/test --lcov --output=cov_profile.lcov",
"check-deps": "deno run -r --allow-read=. --allow-net=jsr.io,registry.npmjs.org jsr:@check/deps",
"check": "deno fmt --check && deno lint && deno check pup.ts && deno test --trace-leaks --allow-read --allow-write --allow-env --allow-net --allow-sys --allow-run --coverage=cov_profile && echo \"Generating coverage\" && deno coverage cov_profile --exclude=pup/test --lcov --output=cov_profile.lcov",
"check-coverage": "deno task check && genhtml cov_profile.lcov --output-directory cov_profile/html && lcov --list cov_profile.lcov && deno run --allow-net --allow-read https://deno.land/std/http/file_server.ts cov_profile/html",
"build-schema": "deno run --allow-write --allow-read --allow-env=XDG_DATA_HOME,HOME tools/build-schema.ts && deno fmt",
"build-versions": "deno run --allow-read --allow-write --allow-env tools/release.ts && deno fmt",
Expand All @@ -40,20 +40,20 @@
"@cross/env": "jsr:@cross/env@~1.0.2",
"@cross/fs": "jsr:@cross/fs@~0.1.11",
"@cross/jwt": "jsr:@cross/jwt@~0.4.7",
"@cross/kv": "jsr:@cross/kv@~0.15.11",
"@cross/kv": "jsr:@cross/kv@^0.16.3",
"@cross/runtime": "jsr:@cross/runtime@~1.0.0",
"@cross/service": "jsr:@cross/service@~1.0.3",
"@cross/test": "jsr:@cross/test@~0.0.9",
"@cross/utils": "jsr:@cross/utils@~0.13.0",
"@hexagon/croner": "jsr:@hexagon/croner@^8.0.2",
"@oak/oak": "jsr:@oak/oak@~16.0.0",
"@oak/oak": "jsr:@oak/oak@^16.1.0",
"@pup/api-client": "jsr:@pup/api-client@~2.0.0",
"@pup/api-definitions": "jsr:@pup/api-definitions@~2.0.0",
"@pup/common": "jsr:@pup/common@~1.0.3",
"@pup/plugin": "jsr:@pup/plugin@~1.0.1",
"@std/assert": "jsr:@std/assert@^0.225.2",
"@std/assert": "jsr:@std/assert@^0.226.0",
"@std/async": "jsr:@std/async@~0.224.0",
"@std/encoding": "jsr:@std/encoding@~0.224.0",
"@std/encoding": "jsr:@std/encoding@^1.0.0",
"@std/io": "jsr:@std/io@~0.224.0",
"@std/path": "jsr:@std/path@^0.225.1",
"@std/semver": "jsr:@std/semver@~0.224.0",
Expand Down
15 changes: 9 additions & 6 deletions lib/core/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class Logger {

public async init(): Promise<void> {
await this.kv.open(this.storeName!)

// Forcefully unlock ledger in case of a stale lock, this can be done as there is other means of preventing multiple running instances
await this.kv.forceUnlockLedger()
}

// Used for attaching the logger hook
Expand Down Expand Up @@ -122,9 +125,9 @@ class Logger {
timeStamp: timeStamp,
}
// Append a random uuid to the key, in case two logs should arrive at the same time
await this.kv.set<LogEventData>(["logs_by_time", timeStamp, initiator, severity, crypto.randomUUID()], logObj)
} catch (e) {
console.error("Error while writing to log store", e)
await this.kv.defer(this.kv.set<LogEventData>(["logs_by_time", timeStamp, initiator, severity, crypto.randomUUID()], logObj))
} catch (_e) {
// console.error("Error while writing to log store", e)
}
}

Expand Down Expand Up @@ -256,11 +259,11 @@ class Logger {
}
}
/**
* Gracefully shut down the logger
* Gracefully shut down the logger, allowing an optional timeout
*/
public async cleanup() {
public async cleanup(timeoutMs = 5000) {
try {
await this.kv?.close()
await this.kv?.close(timeoutMs)
} catch (e) {
console.error("Error while closing kv store: " + e.message)
}
Expand Down
1 change: 1 addition & 0 deletions lib/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Runner extends BaseRunner {
*/
public kill(signal: Deno.Signal = "SIGTERM") {
try {
// @ts-ignore Wierd complaint about "SIGPOLL"
this.process?.kill(signal)
} catch (_e) {
// Ignore
Expand Down
12 changes: 12 additions & 0 deletions test/core/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ test("Logger - Attachment of External Logger", async () => {

assertEquals(externalLoggerCalled, true)
assertEquals(externalLoggerText, expectedExteralLoggerText)

await logger.cleanup()
})

test("Logger - Logging with Different Methods", async () => {
Expand All @@ -50,6 +52,8 @@ test("Logger - Logging with Different Methods", async () => {
await logger.error("test", "Testing error method")

assertEquals(true, true) // This is just to assert that the test passed if no errors are thrown

await logger.cleanup()
})

test("Logger - File Writing with writeFile Method", async () => {
Expand Down Expand Up @@ -82,6 +86,8 @@ test("Logger - getLogContents: Fetch all logs", async () => {

const logs = await logger.getLogContents()
assertEquals(logs, expectedLogs)

await logger.cleanup()
})

test("Logger - getLogContents: Fetch logs by process ID", async () => {
Expand All @@ -102,6 +108,8 @@ test("Logger - getLogContents: Fetch logs by process ID", async () => {

const logs = await logger.getLogContents(processId)
assertEquals(logs, expectedLogs)

await logger.cleanup()
})

test("Logger - getLogContents: Fetch logs by time range", async () => {
Expand All @@ -123,6 +131,8 @@ test("Logger - getLogContents: Fetch logs by time range", async () => {

const logs = await logger.getLogContents(undefined, startTimeStamp, endTimeStamp)
assertEquals(logs, expectedLogs)

await logger.cleanup()
})

test("Logger - getLogContents: Fetch logs by process ID and time range", async () => {
Expand All @@ -145,4 +155,6 @@ test("Logger - getLogContents: Fetch logs by process ID and time range", async (

const logs = await logger.getLogContents(processId, startTimeStamp, endTimeStamp)
assertEquals(logs, expectedLogs)

await logger.cleanup()
})

0 comments on commit f3c3ff3

Please sign in to comment.