Skip to content

Commit

Permalink
fix: prune old requst data
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 committed Oct 1, 2018
1 parent 7906612 commit d85eccd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 48 deletions.
48 changes: 0 additions & 48 deletions lib/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,12 @@ const Result = {
}

const actionHandlers = {}
const actions = {}
var nextActionId = 1

const pruneActionTimeout = 60 * 60 * 1000
const pruneInterval = 15 * 60 * 1000

module.exports = {
start: function (app) {
app.registerActionHandler = registerActionHandler
app.deRegisterActionHandler = deRegisterActionHandler

setInterval(pruneActions, pruneInterval)

app.get(apiPathPrefix + 'actions', function (req, res, next) {
res.json(actions)
})

app.get(apiPathPrefix + 'actions/:id', function (req, res, next) {
var action = actions[req.params.id]
if (!action) {
res.status(404).send()
} else {
res.json(action)
}
})

app.put(apiPathPrefix + '*', function (req, res, next) {
var path = String(req.path).replace(apiPathPrefix, '')

Expand Down Expand Up @@ -225,31 +205,3 @@ function deRegisterActionHandler (context, path, source, callback) {
debug(`de-registered action handler for ${context} ${path} ${source}`)
}
}

function asyncCallback (actionId, status) {
var action = actions[actionId]
if (action) {
action.state = status.state
action.result = status.result
action['endTime'] = new Date().toISOString()
if (status.message) {
action.message = status.message
}
if (status.percentComplete) {
action.percentComplete = status.percentComplete
}
}
}

function pruneActions () {
debug('pruning actions')
_.keys(actions).forEach(id => {
var action = actions[id]

var diff = new Date() - new Date(action['end-time'])
if (diff > pruneActionTimeout) {
delete actions[id]
debug('pruned action %d', id)
}
})
}
24 changes: 24 additions & 0 deletions lib/requestResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const _ = require('lodash')

const requests = {}

// const pruneRequestTimeout = 60 * 60 * 1000
// const pruneIntervalRate = 15 * 60 * 1000
const pruneRequestTimeout = 10000
const pruneIntervalRate = 1000
var pruneInterval

function createRequest (type, clientRequest, user, clientIp, updateCb) {
return new Promise((resolve, reject) => {
let requestId = clientRequest.requestId ? clientRequest.requestId : uuidv4()
Expand All @@ -19,6 +25,11 @@ function createRequest (type, clientRequest, user, clientIp, updateCb) {
}
requests[request.requestId] = request
debug('createRequest %j', request)

if (!pruneInterval) {
pruneInterval = setInterval(pruneRequests, pruneIntervalRate)
}

resolve(request)
})
}
Expand Down Expand Up @@ -98,6 +109,19 @@ function filterRequests (type, state) {
)
}

function pruneRequests () {
debug('pruning requests')
_.keys(requests).forEach(id => {
var request = requests[id]

var diff = new Date() - new Date(request.date)
if (diff > pruneRequestTimeout) {
delete requests[id]
debug('pruned request %s', id)
}
})
}

module.exports = {
createRequest,
updateRequest,
Expand Down

0 comments on commit d85eccd

Please sign in to comment.