diff options
author | Chocobozzz <me@florianbigard.com> | 2020-12-15 13:34:58 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-12-15 13:34:58 +0100 |
commit | 99afa081bc6ae7f34b2105075bd43e3625434fa8 (patch) | |
tree | 0caea8591b3d3688a133cf33edcad616bf276375 /server/lib/activitypub | |
parent | 48586fe070c2a59e9febb62a7f41ebb384e1d20e (diff) | |
download | PeerTube-99afa081bc6ae7f34b2105075bd43e3625434fa8.tar.gz PeerTube-99afa081bc6ae7f34b2105075bd43e3625434fa8.tar.zst PeerTube-99afa081bc6ae7f34b2105075bd43e3625434fa8.zip |
Add AP stats
Diffstat (limited to 'server/lib/activitypub')
-rw-r--r-- | server/lib/activitypub/inbox-manager.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/server/lib/activitypub/inbox-manager.ts b/server/lib/activitypub/inbox-manager.ts new file mode 100644 index 000000000..19e112f91 --- /dev/null +++ b/server/lib/activitypub/inbox-manager.ts | |||
@@ -0,0 +1,55 @@ | |||
1 | import { AsyncQueue, queue } from 'async' | ||
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { SCHEDULER_INTERVALS_MS } from '@server/initializers/constants' | ||
4 | import { MActorDefault, MActorSignature } from '@server/types/models' | ||
5 | import { Activity } from '@shared/models' | ||
6 | import { processActivities } from './process' | ||
7 | import { StatsManager } from '../stat-manager' | ||
8 | |||
9 | type QueueParam = { | ||
10 | activities: Activity[] | ||
11 | signatureActor?: MActorSignature | ||
12 | inboxActor?: MActorDefault | ||
13 | } | ||
14 | |||
15 | class InboxManager { | ||
16 | |||
17 | private static instance: InboxManager | ||
18 | |||
19 | private readonly inboxQueue: AsyncQueue<QueueParam> | ||
20 | |||
21 | private messagesProcessed = 0 | ||
22 | |||
23 | private constructor () { | ||
24 | this.inboxQueue = queue<QueueParam, Error>((task, cb) => { | ||
25 | const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor } | ||
26 | |||
27 | this.messagesProcessed++ | ||
28 | |||
29 | processActivities(task.activities, options) | ||
30 | .then(() => cb()) | ||
31 | .catch(err => { | ||
32 | logger.error('Error in process activities.', { err }) | ||
33 | cb() | ||
34 | }) | ||
35 | }) | ||
36 | |||
37 | setInterval(() => { | ||
38 | StatsManager.Instance.updateInboxStats(this.messagesProcessed, this.inboxQueue.length()) | ||
39 | }, SCHEDULER_INTERVALS_MS.updateInboxStats) | ||
40 | } | ||
41 | |||
42 | addInboxMessage (options: QueueParam) { | ||
43 | this.inboxQueue.push(options) | ||
44 | } | ||
45 | |||
46 | static get Instance () { | ||
47 | return this.instance || (this.instance = new this()) | ||
48 | } | ||
49 | } | ||
50 | |||
51 | // --------------------------------------------------------------------------- | ||
52 | |||
53 | export { | ||
54 | InboxManager | ||
55 | } | ||