diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-30 02:37:44 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-30 02:55:08 +0100 |
commit | 7db370fb877bfaa7bc7cd57764895fb4ab6d493c (patch) | |
tree | c1559d49a3343e3cc59c795a3f3cb781e36d7667 /packages/main/src/services/ConfigPersistenceService.ts | |
parent | build: Switch to esbuild (diff) | |
download | sophie-7db370fb877bfaa7bc7cd57764895fb4ab6d493c.tar.gz sophie-7db370fb877bfaa7bc7cd57764895fb4ab6d493c.tar.zst sophie-7db370fb877bfaa7bc7cd57764895fb4ab6d493c.zip |
refactor: Improve logging
Diffstat (limited to 'packages/main/src/services/ConfigPersistenceService.ts')
-rw-r--r-- | packages/main/src/services/ConfigPersistenceService.ts | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/packages/main/src/services/ConfigPersistenceService.ts b/packages/main/src/services/ConfigPersistenceService.ts index ee5eb9f..b2109f6 100644 --- a/packages/main/src/services/ConfigPersistenceService.ts +++ b/packages/main/src/services/ConfigPersistenceService.ts | |||
@@ -24,13 +24,17 @@ import throttle from 'lodash-es/throttle'; | |||
24 | import { join } from 'path'; | 24 | import { join } from 'path'; |
25 | 25 | ||
26 | import type { ConfigSnapshotOut } from '../stores/Config'; | 26 | import type { ConfigSnapshotOut } from '../stores/Config'; |
27 | import { Disposer } from '../utils'; | 27 | import { Disposer, getLogger } from '../utils'; |
28 | |||
29 | const log = getLogger('configPersistence'); | ||
28 | 30 | ||
29 | export type ReadConfigResult = { found: true; data: unknown; } | { found: false; }; | 31 | export type ReadConfigResult = { found: true; data: unknown; } | { found: false; }; |
30 | 32 | ||
31 | export class ConfigPersistenceService { | 33 | export class ConfigPersistenceService { |
32 | private readonly configFilePath: string; | 34 | private readonly configFilePath: string; |
33 | 35 | ||
36 | private writingConfig = false; | ||
37 | |||
34 | private timeLastWritten: Date | null = null; | 38 | private timeLastWritten: Date | null = null; |
35 | 39 | ||
36 | constructor( | 40 | constructor( |
@@ -47,10 +51,12 @@ export class ConfigPersistenceService { | |||
47 | configStr = await readFile(this.configFilePath, 'utf8'); | 51 | configStr = await readFile(this.configFilePath, 'utf8'); |
48 | } catch (err) { | 52 | } catch (err) { |
49 | if ((err as NodeJS.ErrnoException).code === 'ENOENT') { | 53 | if ((err as NodeJS.ErrnoException).code === 'ENOENT') { |
54 | log.debug('Config file', this.configFilePath, 'was not found'); | ||
50 | return { found: false }; | 55 | return { found: false }; |
51 | } | 56 | } |
52 | throw err; | 57 | throw err; |
53 | } | 58 | } |
59 | log.info('Read config file', this.configFilePath); | ||
54 | return { | 60 | return { |
55 | found: true, | 61 | found: true, |
56 | data: JSON5.parse(configStr), | 62 | data: JSON5.parse(configStr), |
@@ -61,24 +67,42 @@ export class ConfigPersistenceService { | |||
61 | const configJson = JSON5.stringify(configSnapshot, { | 67 | const configJson = JSON5.stringify(configSnapshot, { |
62 | space: 2, | 68 | space: 2, |
63 | }); | 69 | }); |
64 | await writeFile(this.configFilePath, configJson, 'utf8'); | 70 | this.writingConfig = true; |
65 | const stats = await stat(this.configFilePath); | 71 | try { |
66 | this.timeLastWritten = stats.mtime; | 72 | await writeFile(this.configFilePath, configJson, 'utf8'); |
73 | const { mtime } = await stat(this.configFilePath); | ||
74 | log.trace('Config file', this.configFilePath, 'last written at', mtime); | ||
75 | this.timeLastWritten = mtime; | ||
76 | } finally { | ||
77 | this.writingConfig = false; | ||
78 | } | ||
79 | log.info('Wrote config file', this.configFilePath); | ||
67 | } | 80 | } |
68 | 81 | ||
69 | watchConfig(callback: () => Promise<void>, throttleMs: number): Disposer { | 82 | watchConfig(callback: () => Promise<void>, throttleMs: number): Disposer { |
83 | log.debug('Installing watcher for', this.userDataDir); | ||
84 | |||
70 | const configChanged = throttle(async () => { | 85 | const configChanged = throttle(async () => { |
71 | let mtime: Date; | 86 | let mtime: Date; |
72 | try { | 87 | try { |
73 | const stats = await stat(this.configFilePath); | 88 | const stats = await stat(this.configFilePath); |
74 | mtime = stats.mtime; | 89 | mtime = stats.mtime; |
90 | log.trace('Config file last modified at', mtime); | ||
75 | } catch (err) { | 91 | } catch (err) { |
76 | if ((err as NodeJS.ErrnoException).code === 'ENOENT') { | 92 | if ((err as NodeJS.ErrnoException).code === 'ENOENT') { |
93 | log.debug('Config file', this.configFilePath, 'was deleted after being changed'); | ||
77 | return; | 94 | return; |
78 | } | 95 | } |
79 | throw err; | 96 | throw err; |
80 | } | 97 | } |
81 | if (this.timeLastWritten === null || mtime > this.timeLastWritten) { | 98 | if (!this.writingConfig |
99 | && (this.timeLastWritten === null || mtime > this.timeLastWritten)) { | ||
100 | log.debug( | ||
101 | 'Found a config file modified at', | ||
102 | mtime, | ||
103 | 'whish is newer than last written', | ||
104 | this.timeLastWritten, | ||
105 | ); | ||
82 | return callback(); | 106 | return callback(); |
83 | } | 107 | } |
84 | }, throttleMs); | 108 | }, throttleMs); |
@@ -96,6 +120,9 @@ export class ConfigPersistenceService { | |||
96 | } | 120 | } |
97 | }); | 121 | }); |
98 | 122 | ||
99 | return () => watcher.close(); | 123 | return () => { |
124 | log.trace('Removing watcher for', this.configFilePath); | ||
125 | watcher.close(); | ||
126 | }; | ||
100 | } | 127 | } |
101 | } | 128 | } |