2021-02-26 04:28:11 -05:00
|
|
|
import { queue, QueueObject } from 'async'
|
2020-12-15 07:34:58 -05:00
|
|
|
import { logger } from '@server/helpers/logger'
|
|
|
|
import { SCHEDULER_INTERVALS_MS } from '@server/initializers/constants'
|
|
|
|
import { MActorDefault, MActorSignature } from '@server/types/models'
|
|
|
|
import { Activity } from '@shared/models'
|
|
|
|
import { StatsManager } from '../stat-manager'
|
2021-02-26 04:28:11 -05:00
|
|
|
import { processActivities } from './process'
|
2020-12-15 07:34:58 -05:00
|
|
|
|
|
|
|
type QueueParam = {
|
|
|
|
activities: Activity[]
|
|
|
|
signatureActor?: MActorSignature
|
|
|
|
inboxActor?: MActorDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
class InboxManager {
|
|
|
|
|
|
|
|
private static instance: InboxManager
|
|
|
|
|
2021-02-26 04:28:11 -05:00
|
|
|
private readonly inboxQueue: QueueObject<QueueParam>
|
2020-12-15 07:34:58 -05:00
|
|
|
|
|
|
|
private constructor () {
|
|
|
|
this.inboxQueue = queue<QueueParam, Error>((task, cb) => {
|
|
|
|
const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor }
|
|
|
|
|
|
|
|
processActivities(task.activities, options)
|
|
|
|
.then(() => cb())
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('Error in process activities.', { err })
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
setInterval(() => {
|
2021-02-26 04:28:11 -05:00
|
|
|
StatsManager.Instance.updateInboxWaiting(this.getActivityPubMessagesWaiting())
|
2021-10-22 04:28:00 -04:00
|
|
|
}, SCHEDULER_INTERVALS_MS.UPDATE_INBOX_STATS)
|
2020-12-15 07:34:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
addInboxMessage (options: QueueParam) {
|
|
|
|
this.inboxQueue.push(options)
|
2021-02-03 03:33:05 -05:00
|
|
|
.catch(err => logger.error('Cannot add options in inbox queue.', { options, err }))
|
2020-12-15 07:34:58 -05:00
|
|
|
}
|
|
|
|
|
2021-02-18 08:44:12 -05:00
|
|
|
getActivityPubMessagesWaiting () {
|
|
|
|
return this.inboxQueue.length() + this.inboxQueue.running()
|
|
|
|
}
|
|
|
|
|
2020-12-15 07:34:58 -05:00
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
InboxManager
|
|
|
|
}
|