Skip to content

Commit

Permalink
fix: metrics cron (#1486)
Browse files Browse the repository at this point in the history
1. Switches to using `pg.Pool` so multiple SQL commands can be run concurrently!
1. Adds some logging to the metrics cron job to make debugging easier.
1. Increase the timeout by 5 mins!
  • Loading branch information
Alan Shaw authored Feb 28, 2022
1 parent 19600aa commit 8d27e91
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cron-metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Cron Metrics

on:
schedule:
- cron: '*/10 * * * *'
- cron: '*/15 * * * *'
workflow_dispatch:

jobs:
Expand All @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
env: ['staging', 'production']
timeout-minutes: 10
timeout-minutes: 15
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand Down
38 changes: 33 additions & 5 deletions packages/cron/src/jobs/metrics.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import settle from 'p-settle'
import debug from 'debug'
import {
UPLOAD_TYPES,
PIN_SERVICES,
PIN_STATUSES,
} from '../../../api/src/utils/db-client.js'

const log = debug('metrics:updateMetrics')

/**
* @typedef {import('pg').Client} Client
* @typedef {{ name: string, value: number }} Metric
Expand All @@ -17,7 +20,7 @@ const COUNT_UPLOADS = 'SELECT COUNT(*) AS total FROM upload WHERE type = $1'
const COUNT_PINS =
'SELECT COUNT(*) AS total FROM pin WHERE service = $1 AND status = $2'

const SUM_CONTENT_DAG_SIZE = `SELECT SUM(c.dag_size) AS "total" FROM content c`
const SUM_CONTENT_DAG_SIZE = 'SELECT SUM(c.dag_size) AS "total" FROM content c'

const UPDATE_METRIC = `
INSERT INTO metric (name, value, updated_at)
Expand All @@ -33,11 +36,21 @@ ON CONFLICT (name) DO UPDATE
*/
export async function updateMetrics({ roPg, rwPg }) {
const results = await settle([
updateUsersCount(roPg, rwPg),
updateContentRootDagSizeSum(roPg, rwPg),
...UPLOAD_TYPES.map((t) => updateUploadsCount(roPg, rwPg, t)),
withTimeLog('updateUsersCount', () => updateUsersCount(roPg, rwPg)),
withTimeLog('updateContentRootDagSizeSum', () =>
updateContentRootDagSizeSum(roPg, rwPg)
),
...UPLOAD_TYPES.map((t) =>
withTimeLog(`updateUploadsCount[${t}]`, () =>
updateUploadsCount(roPg, rwPg, t)
)
),
...PIN_SERVICES.map((svc) =>
PIN_STATUSES.map((s) => updatePinsCount(roPg, rwPg, svc, s))
PIN_STATUSES.map((s) =>
withTimeLog(`updatePinsCount[${svc}][${s}]`, () =>
updatePinsCount(roPg, rwPg, svc, s)
)
)
).flat(),
])

Expand Down Expand Up @@ -100,3 +113,18 @@ async function updatePinsCount(roPg, rwPg, service, status) {
rows[0].total,
])
}

/**
* @template T
* @param {string} name
* @param {() => Promise<T>} fn
* @returns {Promise<T>}
*/
async function withTimeLog(name, fn) {
const start = Date.now()
try {
return await fn()
} finally {
log(`${name} took: ${Date.now() - start}ms`)
}
}
2 changes: 1 addition & 1 deletion packages/cron/src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ export function getPg(env, mode = 'rw') {
mode === 'rw' ? env.DATABASE_CONNECTION : env.RO_DATABASE_CONNECTION
}
if (!connectionString) throw new Error('missing Postgres connection string')
return new pg.Client({ connectionString })
return new pg.Pool({ connectionString })
}

0 comments on commit 8d27e91

Please sign in to comment.