File tree Expand file tree Collapse file tree 2 files changed +12
-16
lines changed Expand file tree Collapse file tree 2 files changed +12
-16
lines changed Original file line number Diff line number Diff line change 1
1
import { PubSub } from "@google-cloud/pubsub" ;
2
+ import { timeout } from "../util/timeout" ;
2
3
import { PubSubContactsMessage } from "./pubsub-contacts-message.model" ;
3
- import { ServerError } from "./server-error.model" ;
4
4
5
5
const PUBLISH_TIMEOUT = 10_000 ;
6
+
6
7
export class PubSubClient {
7
8
private client : PubSub ;
8
9
private topicName : string ;
@@ -17,27 +18,16 @@ export class PubSubClient {
17
18
throw new Error ( "No pubsub topic name provided." ) ;
18
19
}
19
20
20
- const timeout = ( ms : number ) =>
21
- new Promise ( ( _ , reject ) =>
22
- setTimeout (
23
- ( ) =>
24
- reject (
25
- new ServerError (
26
- 500 ,
27
- "Could not publish message in time, did you forget to run gcloud auth application-default login?"
28
- )
29
- ) ,
30
- ms
31
- )
32
- ) ;
33
-
34
21
const json = JSON . stringify ( message ) ;
35
22
const dataBuffer = Buffer . from ( json ) ;
36
23
const topic = this . client . topic ( this . topicName ) ;
37
24
38
25
await Promise . race ( [
39
26
topic . publishMessage ( { data : dataBuffer } ) ,
40
- timeout ( PUBLISH_TIMEOUT ) ,
27
+ timeout (
28
+ PUBLISH_TIMEOUT ,
29
+ "Could not publish message in time. Did you forget to authenticate with GCP? (gcloud auth application-default login)"
30
+ ) ,
41
31
] ) ;
42
32
}
43
33
}
Original file line number Diff line number Diff line change
1
+ import { ServerError } from "../models" ;
2
+
3
+ export const timeout = ( timeMs : number , errorMessage : string ) =>
4
+ new Promise ( ( _ , reject ) =>
5
+ setTimeout ( ( ) => reject ( new ServerError ( 500 , errorMessage ) ) , timeMs )
6
+ ) ;
You can’t perform that action at this time.
0 commit comments