Browse Source

Add systemd service/timer

master
Kevin Mok 5 years ago
parent
commit
f2b7a421f9
Signed by: Kevin-Mok GPG Key ID: AEA75288DC135CF5
  1. 14
      server-pages.service
  2. 9
      server-pages.timer
  3. 6
      server-status.js

14
server-pages.service

@ -0,0 +1,14 @@
[Unit]
Description=Update server page statuses
[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c 'node /home/kevin/server-pages/server-status.js > /home/kevin/my-site/public/server-apps.json'
User=kevin
Group=kevin
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/kevin/server-pages
[Install]
WantedBy=multi-user.target

9
server-pages.timer

@ -0,0 +1,9 @@
[Unit]
Description=Update server page statuses
[Timer]
OnCalendar=*:0
Persistent=true
[Install]
WantedBy=timers.target

6
server-status.js

@ -2,19 +2,17 @@ const urlMetadata = require('url-metadata')
const csv = require('csvtojson') const csv = require('csvtojson')
const log = console.log const log = console.log
const { spawnSync } = require( 'child_process' ), const { spawnSync } = require( 'child_process' ),
// uptime = spawnSync( 'uptime', [ '-p' ] )
uptime = spawnSync( 'uptime', [ '-s' ] ) uptime = spawnSync( 'uptime', [ '-s' ] )
const csvFilePath = 'server-pages.csv' const csvFilePath = 'server-pages.csv'
// const csvFilePath = 'server-pages-test.csv' // const csvFilePath = 'server-pages-test.csv'
// consider webpage to be up if stored title matches fetched title
const checkIfTitleMatches = obj => { const checkIfTitleMatches = obj => {
return urlMetadata(obj['url']).then(metadata => { // success handler return urlMetadata(obj['url']).then(metadata => { // success handler
if (metadata.title == obj['siteTitle']) { if (metadata.title == obj['siteTitle']) {
// log(obj['shortUrl'])
obj['status'] = 'up' obj['status'] = 'up'
} else { } else {
// console.log(metadata)
obj['status'] = 'down' obj['status'] = 'down'
} }
return true return true
@ -24,6 +22,7 @@ const checkIfTitleMatches = obj => {
}) })
} }
// update all sites' status in JSON
const updateSiteStatus = json => { const updateSiteStatus = json => {
const promiseArray = [] const promiseArray = []
json.forEach(async function (obj) { json.forEach(async function (obj) {
@ -37,6 +36,7 @@ const updateSiteStatus = json => {
} }
csv().fromFile(csvFilePath).then(async function (json) { csv().fromFile(csvFilePath).then(async function (json) {
// finish updating all sites' status before outputting JSON
Promise.all(updateSiteStatus(json)).then(results => { Promise.all(updateSiteStatus(json)).then(results => {
const statusJson = { const statusJson = {
"sites": json, "sites": json,

Loading…
Cancel
Save