Skip to content

Commit

Permalink
Fixed exception when cache dir doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Dec 22, 2023
1 parent 48840e0 commit eb316b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ iobroker add trashschedule
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**

* (klein0r) Fixed exception when cache dir doesn't exist

### 3.1.1 (2023-12-22)

* (klein0r) Fixed config validation / integration test
Expand Down
4 changes: 3 additions & 1 deletion io-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"name": "trashschedule",
"version": "3.1.1",
"blockedVersions": [
"3.0.0"
"3.0.0",
"3.1.0",
"3.1.1"
],
"news": {
"3.1.1": {
Expand Down
31 changes: 20 additions & 11 deletions lib/source/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,28 @@ class BaseSource {
}

async removeOldCacheFiles() {
const files = await this.adapter.readDirAsync(this.adapter.namespace, '/');
const d = new Date();

for (const file of files) {
if (!file.isDir && file.modifiedAt && file.file.endsWith('.json')) {
const dModified = new Date(file.modifiedAt);

// Delete files created in another month
if (dModified.getUTCMonth() !== d.getUTCMonth() && dModified.getUTCFullYear() !== d.getUTCFullYear()) {
await this.adapter.delFileAsync(this.adapter.namespace, file.file);
this.adapter.log.info(`[removeOldCacheFiles] Deleted cached file: ${file.file}`);
try {
const dirExists = await this.adapter.fileExistsAsync(this.adapter.namespace, '/');
if (dirExists) {
this.adapter.log.debug(`[removeOldCacheFiles] Removing old cache files...`);

const files = await this.adapter.readDirAsync(this.adapter.namespace, '/');
const d = new Date();

for (const file of files) {
if (!file.isDir && file.modifiedAt && file.file.endsWith('.json')) {
const dModified = new Date(file.modifiedAt);

// Delete files created in another month
if (dModified.getUTCMonth() !== d.getUTCMonth() && dModified.getUTCFullYear() !== d.getUTCFullYear()) {
await this.adapter.delFileAsync(this.adapter.namespace, file.file);
this.adapter.log.info(`[removeOldCacheFiles] Deleted cached file: ${file.file}`);
}
}
}
}
} catch (err) {
this.adapter.log.warn(`[removeOldCacheFiles] ${err}`);
}
}
}
Expand Down

0 comments on commit eb316b0

Please sign in to comment.