Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 57a5747a55c840c7c13adead74bca8aae7259dec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * Delta Publisher
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of liza.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Publish delta message to a queue
 */

import { AmqpPublisher } from './AmqpPublisher';
import { Delta } from '../bucket/delta';
import { EventEmitter } from 'events';
import { DocumentMeta } from '../document/Document';
import { context } from '../error/ContextError';
import { AmqpError } from '../error/AmqpError';
import { MessageWriter } from './MessageWriter';

import { AmqpConnection } from './amqp/AmqpConnection';


export class DeltaPublisher implements AmqpPublisher
{
    /**
     * Delta publisher
     *
     * @param _emitter - event emitter instance
     * @param _ts_ctr  - a timestamp constructor
     * @param _conn    - the amqp connection
     * @param _writer  - message writer
     */
    constructor(
        private readonly _emitter: EventEmitter,
        private readonly _ts_ctr:  () => UnixTimestamp,
        private readonly _conn:    AmqpConnection,
        private readonly _writer:  MessageWriter,
    ) {}


    /**
     * Publish quote message to exchange post-rating
     *
     * @param meta     - document meta data
     * @param delta    - delta
     * @param bucket   - bucket
     * @param ratedata - rate data bucket
    */
    publish(
        meta:     DocumentMeta,
        delta:    Delta<any>,
        bucket:   Record<string, any>,
        ratedata: Record<string, any>,
    ): Promise<void>
    {
        const ts          = this._ts_ctr();
        const headers     = { version: 1, created: ts };

        return this._writer.write(
            ts,
            meta,
            delta,
            bucket,
            ratedata
        ).then( ( avro_buffer: Buffer ) =>
            {
                const channel = this._conn.getAmqpChannel();

                if ( !channel )
                {
                    throw context(
                        new AmqpError( 'Error sending message: No channel' ),
                        {
                            doc_id:     meta.id,
                            delta_type: delta.type,
                            delta_ts:   delta.timestamp,
                        },
                    );
                }

                // we don't use a routing key; fanout exchange
                const published_successfully = channel.publish(
                    this._conn.getExchangeName(),
                    '',
                    avro_buffer,
                    { headers: headers },
                );

                if ( !published_successfully )
                {
                    throw context(
                        new Error ( 'Delta publish failed' ),
                        {
                            doc_id:     meta.id,
                            delta_type: delta.type,
                            delta_ts:   delta.timestamp,
                        }
                    );
                }
            } )
            .then( ( _: any ) =>
            {
                this._emitter.emit(
                    'delta-publish',
                    {
                        delta: delta,
                        exchange: this._conn.getExchangeName(),
                    }
                );
            } );
    }
}