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 11import { PubSub } from "@google-cloud/pubsub" ;
2+ import { timeout } from "../util/timeout" ;
23import { PubSubContactsMessage } from "./pubsub-contacts-message.model" ;
3- import { ServerError } from "./server-error.model" ;
44
55const PUBLISH_TIMEOUT = 10_000 ;
6+
67export class PubSubClient {
78 private client : PubSub ;
89 private topicName : string ;
@@ -17,27 +18,16 @@ export class PubSubClient {
1718 throw new Error ( "No pubsub topic name provided." ) ;
1819 }
1920
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-
3421 const json = JSON . stringify ( message ) ;
3522 const dataBuffer = Buffer . from ( json ) ;
3623 const topic = this . client . topic ( this . topicName ) ;
3724
3825 await Promise . race ( [
3926 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+ ) ,
4131 ] ) ;
4232 }
4333}
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