Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
expensive background process
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohanator9000 committed Dec 4, 2020
1 parent 2fce688 commit 285e012
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@material-ui/icons": "^4.9.1",
"@reach/router": "^1.3.4",
"@types/react-router-dom": "^5.1.6",
"crypto": "^1.0.1",
"graphql": "^15.3.0",
"react": "^16.14.0",
"react-dom": "^16.14.0",
Expand Down
7 changes: 7 additions & 0 deletions server/src/background-process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createHash } from 'crypto'
import { getRepository } from 'typeorm'
import { Party } from './entities/Party'

Expand All @@ -9,3 +10,9 @@ export async function deleteOldParties() {
.where('TIMESTAMPDIFF(HOUR, latestTime, CURRENT_TIMESTAMP()) > 24')
.execute()
}

// An expensive hash function to see how it affects performance. Copied from class slides.
export function expensiveFunction() {
const data = 'hash-me'
createHash('sha256').update(data).digest('base64')
}
2 changes: 1 addition & 1 deletion server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export const Config = {
backgroundService: isServiceEnabled(AppService.BACKGROUND) ? true : !isProd(),
wsUrl: process.env.WS_URL || 'ws://localhost:3000/graphqlsubscription',
adminPassword: process.env.ADMIN_PASSWORD || 'password',
backgroundProcessInterval: 86400000, // In milliseconds.
backgroundProcessInterval: 100, // In milliseconds.
}
7 changes: 3 additions & 4 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { forAwaitEach, isAsyncIterable } from 'iterall'
import path from 'path'
import 'reflect-metadata'
import { checkEqual, Unpromise } from '../../common/src/util'
import { deleteOldParties } from './background-process'
import { expensiveFunction } from './background-process'
import { Config } from './config'
import { migrate } from './db/migrate'
import { initORM } from './db/sql'
Expand Down Expand Up @@ -189,7 +189,6 @@ initORM()
.catch(err => console.error(err))

setInterval(() => {
deleteOldParties()
.then(() => console.log('Background process run succesfully.'))
.catch(err => console.error(err))
expensiveFunction()
console.log('Background process run succesfully.')
}, Config.backgroundProcessInterval)

0 comments on commit 285e012

Please sign in to comment.