Skip to content

Commit

Permalink
perf(cache): improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed May 23, 2019
1 parent 79742ee commit 16bd9f8
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 461 deletions.
3 changes: 1 addition & 2 deletions .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ exclude_paths:
- src/public/js/plugins/**/*
- src/public/js/vendor/**
- src/public/js/vendor/**/*
- test/**/*
- src/cache/node-cache.js
- test/**/*
22 changes: 3 additions & 19 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,7 @@ function launchServer (db) {
// return next()
// },
function (next) {
var NodeCache = require('./src/cache/node-cache')
global.cache = new NodeCache({ checkperiod: 0 })
var fork = require('child_process').fork
var memLimit = nconf.get('memlimit') || '2048'

var env = { FORK: 1, NODE_ENV: global.env }
var cache = require('./src/cache/cache')
if (isDocker) {
var envDocker = {
TRUDESK_DOCKER: process.env.TRUDESK_DOCKER,
Expand All @@ -209,21 +204,10 @@ function launchServer (db) {
TD_MONGODB_DATABASE: process.env.TD_MONGODB_DATABASE
}

env = _.merge(env, envDocker)
cache.env = envDocker
}

var n = fork(path.join(__dirname, '/src/cache/index.js'), {
execArgv: ['--max-old-space-size=' + memLimit],
env: env
})

global.forks.push({ name: 'cache', fork: n })

n.on('message', function (data) {
if (data.cache) {
global.cache.data = data.cache.data
}
})
cache.init()

return next()
},
Expand Down
2 changes: 1 addition & 1 deletion src/backup/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ winston.add(winston.transports.Console, {
' ' +
date.toTimeString().substr(0, 8) +
' [Child:Backup:' +
global.process.pid +
process.pid +
']'
)
},
Expand Down
2 changes: 1 addition & 1 deletion src/backup/restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ winston.add(winston.transports.Console, {
' ' +
date.toTimeString().substr(0, 8) +
' [Child:Backup:' +
global.process.pid +
process.pid +
']'
)
},
Expand Down
53 changes: 53 additions & 0 deletions src/cache/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* . .o8 oooo
* .o8 "888 `888
* .o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo
* 888 `888""8P `888 `888 d88' `888 d88' `88b d88( "8 888 .8P'
* 888 888 888 888 888 888 888ooo888 `"Y88b. 888888.
* 888 . 888 888 888 888 888 888 .o o. )88b 888 `88b.
* "888" d888b `V88V"V8P' `Y8bod88P" `Y8bod8P' 8""888P' o888o o888o
* ========================================================================
* Author: Chris Brame
* Updated: 5/17/19 2:03 AM
* Copyright (c) 2014-2019. All rights reserved.
*/

var _ = require('lodash')
var NodeCache = require('node-cache')
var path = require('path')
var cache = {}

cache.init = function () {
global.cache = new NodeCache({ checkperiod: 0 })
cache.memLimit = process.env.CACHE_MEMLIMIT || '2048'
var env = { FORK: 1, NODE_ENV: global.env }
cache.env = _.merge(cache.env, env)

spawnCache()
setInterval(spawnCache, 55 * 60 * 1000)
}

function spawnCache () {
var fork = require('child_process').fork
console.log(cache.env)
var n = fork(path.join(__dirname, './index.js'), {
execArgv: ['--max-old-space-size=' + cache.memLimit],
env: cache.env
})

global.forks.push({ name: 'cache', fork: n })

n.on('message', function (data) {
if (data.cache) {
global.cache.data = data.cache.data
}
})

n.on('close', function () {
_.remove(global.forks, function (i) {
return i.name === 'cache'
})
})
}

module.exports = cache
15 changes: 6 additions & 9 deletions src/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ winston.add(winston.transports.Console, {
' ' +
date.toTimeString().substr(0, 8) +
' [Child:Cache:' +
global.process.pid +
process.pid +
']'
)
},
Expand All @@ -66,7 +66,7 @@ truCache.init = function (callback) {

truCache.refreshCache(function () {
winston.debug('Cache Loaded')
restartRefreshClock()
// restartRefreshClock()

return callback()
})
Expand Down Expand Up @@ -103,9 +103,7 @@ truCache.refreshCache = function (callback) {
[
function (done) {
var ticketStats = require('./ticketStats')
// console.time('test');
ticketStats(tickets, function (err, stats) {
// console.timeEnd('test');
if (err) return done(err)
var expire = 3600 // 1 hour
cache.set('tickets:overview:lastUpdated', stats.lastUpdated, expire)
Expand Down Expand Up @@ -135,11 +133,6 @@ truCache.refreshCache = function (callback) {
cache.set('tickets:overview:e365:responseTime', stats.e365.avgResponse, expire)
cache.set('tickets:overview:e365:graphData', stats.e365.graphData, expire)

// cache.set('tickets:overview:lifetime:ticketCount', stats.lifetime.tickets, expire);
// cache.set('tickets:overview:lifetime:closedTickets', stats.lifetime.closedTickets, expire);
// cache.set('tickets:overview:lifetime:responseTime', stats.lifetime.avgResponse, expire);
// cache.set('tickets:overview:lifetime:graphData', stats.lifetime.graphData, expire);

return done()
})
},
Expand Down Expand Up @@ -232,7 +225,9 @@ truCache.refreshCache = function (callback) {
if (err) return winston.warn(err)
// Send to parent
process.send({ cache: cache })

cache.flushAll()

if (_.isFunction(callback)) {
return callback(err)
}
Expand Down Expand Up @@ -278,6 +273,8 @@ truCache.refreshCache = function (callback) {
winston.error(err)
throw new Error(err)
}

return process.exit(0)
})
})
})()
Expand Down
Loading

0 comments on commit 16bd9f8

Please sign in to comment.