1 | /*!
|
2 | * Copyright 2019 Google Inc. All Rights Reserved.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 | /// <reference types="node" />
|
17 | /// <reference types="node" />
|
18 | import { ServiceError } from 'google-gax';
|
19 | import { EventEmitter } from 'events';
|
20 | import { BatchPublishOptions, MessageBatch } from './message-batch';
|
21 | import { PublishError } from './publish-error';
|
22 | import { Publisher, PubsubMessage, PublishCallback } from './';
|
23 | export interface PublishDone {
|
24 | (err: ServiceError | null): void;
|
25 | }
|
26 | /**
|
27 | * Queues are used to manage publishing batches of messages.
|
28 | *
|
29 | * @private
|
30 | *
|
31 | * @param {Publisher} publisher The parent publisher.
|
32 | */
|
33 | export declare abstract class MessageQueue extends EventEmitter {
|
34 | batchOptions: BatchPublishOptions;
|
35 | publisher: Publisher;
|
36 | pending?: NodeJS.Timer;
|
37 | constructor(publisher: Publisher);
|
38 | /**
|
39 | * Forces the queue to update its options from the publisher.
|
40 | * The specific queue will need to do a bit more to pass the new
|
41 | * values down into any MessageBatch.
|
42 | *
|
43 | * This is only for use by { Publisher}.
|
44 | *
|
45 | *
|
46 | */
|
47 | updateOptions(): void;
|
48 | /**
|
49 | * Adds a message to the queue.
|
50 | *
|
51 | * @abstract
|
52 | *
|
53 | * @param {object} message The message to publish.
|
54 | * @param {PublishCallback} callback The publish callback.
|
55 | */
|
56 | abstract add(message: PubsubMessage, callback: PublishCallback): void;
|
57 | /**
|
58 | * Method to initiate publishing.
|
59 | *
|
60 | * @abstract
|
61 | */
|
62 | abstract publish(): void;
|
63 | /**
|
64 | * Accepts a batch of messages and publishes them to the API.
|
65 | *
|
66 | * @param {object[]} messages The messages to publish.
|
67 | * @param {PublishCallback[]} callbacks The corresponding callback functions.
|
68 | * @param {function} [callback] Callback to be fired when publish is done.
|
69 | */
|
70 | _publish(messages: PubsubMessage[], callbacks: PublishCallback[], callback?: PublishDone): void;
|
71 | }
|
72 | /**
|
73 | * Standard message queue used for publishing messages.
|
74 | *
|
75 | * @private
|
76 | * @extends MessageQueue
|
77 | *
|
78 | * @param {Publisher} publisher The publisher.
|
79 | */
|
80 | export declare class Queue extends MessageQueue {
|
81 | batch: MessageBatch;
|
82 | constructor(publisher: Publisher);
|
83 | updateOptions(): void;
|
84 | /**
|
85 | * Adds a message to the queue.
|
86 | *
|
87 | * @param {PubsubMessage} message The message to publish.
|
88 | * {PublishCallback} callback The publish callback.
|
89 | */
|
90 | add(message: PubsubMessage, callback: PublishCallback): void;
|
91 | /**
|
92 | * Cancels any pending publishes and calls _publish immediately.
|
93 | */
|
94 | publish(callback?: PublishDone): void;
|
95 | }
|
96 | /**
|
97 | * Queue for handling ordered messages. Unlike the standard queue, this
|
98 | * ensures that batches are published one at a time and throws an exception in
|
99 | * the event that any batch fails to publish.
|
100 | *
|
101 | * @private
|
102 | * @extends MessageQueue
|
103 | *
|
104 | * @param {Publisher} publisher The publisher.
|
105 | * @param {string} key The key used to order the messages.
|
106 | */
|
107 | export declare class OrderedQueue extends MessageQueue {
|
108 | batches: MessageBatch[];
|
109 | inFlight: boolean;
|
110 | error?: null | PublishError;
|
111 | key: string;
|
112 | constructor(publisher: Publisher, key: string);
|
113 | updateOptions(): void;
|
114 | /**
|
115 | * Reference to the batch we're currently filling.
|
116 | * @returns {MessageBatch}
|
117 | */
|
118 | get currentBatch(): MessageBatch;
|
119 | /**
|
120 | * Adds a message to a batch, creating a new batch if need be.
|
121 | *
|
122 | * @param {object} message The message to publish.
|
123 | * @param {PublishCallback} callback The publish callback.
|
124 | */
|
125 | add(message: PubsubMessage, callback: PublishCallback): void;
|
126 | /**
|
127 | * Starts a timeout to publish any pending messages.
|
128 | */
|
129 | beginNextPublish(): void;
|
130 | /**
|
131 | * Creates a new {@link MessageBatch} instance.
|
132 | *
|
133 | * @returns {MessageBatch}
|
134 | */
|
135 | createBatch(): MessageBatch;
|
136 | /**
|
137 | * In the event of a publish failure, we need to cache the error in question
|
138 | * and reject all pending publish calls, prompting the user to call
|
139 | * {@link OrderedQueue#resumePublishing}.
|
140 | *
|
141 | * @param {Error} err The publishing error.
|
142 | */
|
143 | handlePublishFailure(err: ServiceError): void;
|
144 | /**
|
145 | * Publishes the messages. If successful it will prepare the next batch to be
|
146 | * published immediately after. If an error occurs, it will reject all
|
147 | * pending messages. In the event that no pending messages/batches are left,
|
148 | * a "drain" event will be fired, indicating to the publisher that it is
|
149 | * safe to delete this queue.
|
150 | *
|
151 | * @fires OrderedQueue#drain
|
152 | */
|
153 | publish(callback?: PublishDone): void;
|
154 | /**
|
155 | * Tells the queue it is ok to continue publishing messages.
|
156 | */
|
157 | resumePublishing(): void;
|
158 | }
|