Collect & simulate hooks

This commit is contained in:
Lynn Smeria 2020-03-18 01:30:56 +02:00
parent cc2cf0201f
commit 0e380a26ba
4 changed files with 99 additions and 6 deletions

38
hooks.js Normal file
View file

@ -0,0 +1,38 @@
const fs = require('fs').promises;
const ms = require('ms');
const os = require('os');
const path = require('path');
const getHooksRoot = () => path.join(os.homedir(), '/.syncthing-hooks');
const readHooksRoot = async root => {
try {
const files = await fs.readdir(root);
return files;
} catch {
return [];
}
};
const parseHooks = (root, hooks) =>
hooks
.map(x => ({
path: path.join(root, x),
match: x.match(/(?<folder>.{11})-(?<time>.*)/),
}))
.filter(x => x.match)
.map(x => ({
path: x.path,
folder: x.match.groups.folder,
time: ms(x.match.groups.time),
}));
const collectHooks = async () => {
const root = getHooksRoot();
const hooks = await readHooksRoot(root);
return parseHooks(root, hooks);
};
module.exports = {
collectHooks,
};

View file

@ -1,13 +1,62 @@
const { fetchNewEvents } = require('./api'); const { fetchNewEvents } = require('./api');
const { collectHooks } = require('./hooks');
require('dotenv').config(); require('dotenv').config();
const state = {}; const state = {
seenIds: null,
mostRecentEventForFolder: null,
};
const getMostRecentEvents = (events, monitoredFolders) => {
const mostRecentEventForFolder = events
.filter(x => x.data && x.data.folder)
.filter(x => monitoredFolders.has(x.data.folder))
.reduce(
(acc, x) => {
const date = new Date(x.time);
const existingDate = acc[x.data.folder];
acc[x.data.folder] =
existingDate && existingDate > date ? existingDate : date;
return acc;
},
state.mostRecentEventForFolder
? { ...state.mostRecentEventForFolder }
: {}
);
state.mostRecentEventForFolder = mostRecentEventForFolder;
return mostRecentEventForFolder;
};
const convertRecentEventDatesToDelta = () => {
const now = new Date().getTime();
return Object.entries(state.mostRecentEventForFolder)
.map(([folder, date]) => [folder, now - date.getTime()])
.reduce((acc, [path, delta]) => {
acc[path] = delta;
return acc;
}, {});
};
setInterval(async () => { setInterval(async () => {
const { events, seenIds } = await fetchNewEvents(state.seenIds); const { events, seenIds } = await fetchNewEvents(state.seenIds);
state.seenIds = seenIds; const hooks = await collectHooks();
events.forEach(event => { const monitoredFolders = new Set(hooks.map(x => x.folder));
console.log(event); const deltaForFolders =
getMostRecentEvents(events, monitoredFolders) &&
convertRecentEventDatesToDelta();
hooks
.filter(x => deltaForFolders[x.folder])
.forEach(hook => {
const timeToWait = hook.time - deltaForFolders[hook.folder];
console.log(timeToWait);
if (timeToWait < 0) {
delete state.mostRecentEventForFolder[hook.folder];
console.log('running hook', hook);
}
}); });
}, 1000);
console.warn(deltaForFolders);
state.seenIds = seenIds;
}, 2000);

View file

@ -10,6 +10,7 @@
}, },
"dependencies": { "dependencies": {
"dotenv": "8.2.0", "dotenv": "8.2.0",
"got": "10.6.0" "got": "10.6.0",
"ms": "2.1.2"
} }
} }

View file

@ -164,6 +164,11 @@ mimic-response@^2.0.0, mimic-response@^2.1.0:
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-2.1.0.tgz#d13763d35f613d09ec37ebb30bac0469c0ee8f43" resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-2.1.0.tgz#d13763d35f613d09ec37ebb30bac0469c0ee8f43"
integrity sha512-wXqjST+SLt7R009ySCglWBCFpjUygmCIfD790/kVbiGmUgfYGuB14PiTd5DwVxSV4NcYHjzMkoj5LjQZwTQLEA== integrity sha512-wXqjST+SLt7R009ySCglWBCFpjUygmCIfD790/kVbiGmUgfYGuB14PiTd5DwVxSV4NcYHjzMkoj5LjQZwTQLEA==
ms@2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
normalize-url@^4.1.0: normalize-url@^4.1.0:
version "4.5.0" version "4.5.0"
resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-4.5.0.tgz#453354087e6ca96957bd8f5baf753f5982142129" resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-4.5.0.tgz#453354087e6ca96957bd8f5baf753f5982142129"