membership watcher

This commit is contained in:
2020-09-20 13:32:29 +02:00
parent 94dfbbe731
commit 641a2db322
4 changed files with 63 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ import {Watcher} from "./types/state";
import {headWatcher} from "./watchers/bma/head-watcher";
import {jsonWatcher} from "./watchers/wotwizard/json-watcher";
import {initConfMail} from './mail'
import {membershipWatcher} from './watchers/bma/membership-watcher'
export async function dwatch(confFile: string) {
@@ -22,5 +23,6 @@ export async function dwatch(confFile: string) {
(await Promise.all((conf.webDiffServers || []).map(webDiffWatcher(conf)))).forEach(w => watchers.push(w));
(await Promise.all((conf.headServers || []).map(headWatcher(conf)))).forEach(w => watchers.push(w));
(await Promise.all((conf.wwMeta || []).map(jsonWatcher(conf)))).forEach(w => watchers.push(w));
(await Promise.all((conf.memberships || []).map(membershipWatcher(conf)))).forEach(w => watchers.push(w));
return watchers
}

View File

@@ -8,6 +8,7 @@ export interface Conf {
webDiffServers: ConfWebDiff[]
headServers: ConfHead[]
wwMeta: ConfWWMeta[]
memberships: ConfMembership[]
mail: ConfMail
}
@@ -36,6 +37,13 @@ export interface ConfBMA extends ConfURL {
maxLate?: number
}
export interface ConfMembership extends ConfURL {
pubkey: string
memberAlias: string
currency: string
mustRemain: number
}
export interface ConfHead extends ConfURL {
observedPubkey: string
maxLateBlocks: number

View File

@@ -0,0 +1,41 @@
import {Conf, ConfMembership} from "../../types/conf";
import {urlWatcher, UrlWatcherResult} from '../abstract/url-watcher'
export function membershipWatcher(conf: Conf) {
const URL_PATH = '/wot/requirements/'
return async (confMS: ConfMembership) => {
function getSubjectTitle(state: string) {
return `State ${state} MS of ${confMS.memberAlias} (${confMS.currency})`
}
let state = 'INIT'
return urlWatcher(conf, async (data) => {
const json = data as { identities: {membershipExpiresIn: number}[] }
// On se base sur la clé publique donc a priori une seule identité OK
const idty = json.identities[0]
if (idty.membershipExpiresIn < confMS.mustRemain) {
if (idty.membershipExpiresIn > 0) {
state = 'WARNING'
const remainingDays = Math.floor(idty.membershipExpiresIn / (3600 * 24))
return UrlWatcherResult.ko(`Membership is going to expire in ${remainingDays.toFixed(0)} days`)
} else {
state = 'ERROR'
return UrlWatcherResult.ko(`Membership expired`)
}
}
state = 'OK'
return UrlWatcherResult.ok()
})({
name: `membership ${confMS.pubkey}`,
address: confMS.address + URL_PATH + confMS.pubkey,
frequency: confMS.frequency
},
() => getSubjectTitle(state),
() => getSubjectTitle(state),
() => getSubjectTitle(state))
}
}