Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: TECH skip message processing when the message parsing fails #112

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions packages/pubsub/src/Subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface ISubscriptionOptions {
name: string
id: number
}
labels?: ({ [k: string]: string } | null)
labels?: { [k: string]: string } | null
filter?: string
}

Expand All @@ -61,7 +61,7 @@ interface ISubscriptionDeadLetterPolicy {
interface ISubscriptionInitializationOptions {
deadLetterPolicy: ISubscriptionDeadLetterPolicy | null
retryPolicy: ISubscriptionRetryPolicy
labels?: ({ [k: string]: string } | null);
labels?: { [k: string]: string } | null
filter?: string
}

Expand Down Expand Up @@ -109,7 +109,9 @@ export class Subscriber<T = unknown> {

public async initialize(): Promise<void> {
try {
await this.initializeTopic(this.topicName, this.topic)
if (process.env['PUBSUB_SUBSCRIPTION_TOPIC_INIT'] === 'true') {
await this.initializeTopic(this.topicName, this.topic)
}

await this.initializeDeadLetterTopic()

Expand Down Expand Up @@ -152,7 +154,9 @@ export class Subscriber<T = unknown> {
private async parseData(message: Message): Promise<T> {
let schemaId = message.attributes['googclient_schemarevisionid']
if (!schemaId) {
this.logger?.error(`PubSub: Subscription: ${this.subscriptionName} Message does not have schema revision id`, { message })
this.logger?.error(`PubSub: Subscription: ${this.subscriptionName} Message does not have schema revision id`, {
message,
})
schemaId = await this.schemaCache.getLatestSchemaRevisionId()
}

Expand All @@ -178,7 +182,17 @@ export class Subscriber<T = unknown> {
asyncCallback: (msg: IParsedMessage<T>, info: IMessageInfo) => Promise<void>,
): (message: Message) => void {
const asyncMessageProcessor = async (message: Message) => {
const dataParsed = await this.parseData(message)
const dataParsed = await this.parseData(message).catch(e => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only relevant change. Other's are just formatting

this.logger?.error(`PubSub: Subscription: ${this.subscriptionName} Failed to parse message:`, e)
return undefined
})

// If message parsing failed, nack the message and skip processing
if (!dataParsed) {
message.nack()
return
}

const messageParsed = Object.assign(message, { dataParsed })
const info: IMessageInfo = {
id: message.id,
Expand Down Expand Up @@ -229,11 +243,17 @@ export class Subscriber<T = unknown> {
} else if (options) {
const [existingSubscription] = await subscription.getMetadata()
if ((options.filter || existingSubscription.filter) && options.filter != existingSubscription.filter) {
throw new Error(`PubSub: Subscriptions filters are immutable, they can't be changed, subscription: ${subscriptionName},` +
` currentFilter: ${existingSubscription.filter as string}, newFilter: ${options.filter as string}`)
throw new Error(
`PubSub: Subscriptions filters are immutable, they can't be changed, subscription: ${subscriptionName},` +
` currentFilter: ${existingSubscription.filter as string}, newFilter: ${options.filter as string}`,
)
}
if (this.isMetadataChanged(existingSubscription, options)) {
await subscription.setMetadata(options)
await subscription.setMetadata({
retryPolicy: options.retryPolicy,
deadLetterPolicy: options.deadLetterPolicy,
labels: options?.labels
})
this.logger?.info(`PubSub: Subscription ${subscriptionName} metadata updated`)
}
}
Expand Down Expand Up @@ -335,20 +355,28 @@ export class Subscriber<T = unknown> {
}

private isMetadataChanged(existingSubscription: ISubscription, options: ISubscriptionInitializationOptions): boolean {
if (options.retryPolicy.minimumBackoff?.seconds &&
String(options.retryPolicy.minimumBackoff.seconds) !== existingSubscription.retryPolicy?.minimumBackoff?.seconds) {
if (
options.retryPolicy.minimumBackoff?.seconds &&
String(options.retryPolicy.minimumBackoff.seconds) !== existingSubscription.retryPolicy?.minimumBackoff?.seconds
) {
return true
}
if (options.retryPolicy.maximumBackoff?.seconds &&
String(options.retryPolicy.maximumBackoff.seconds) !== existingSubscription.retryPolicy?.maximumBackoff?.seconds) {
if (
options.retryPolicy.maximumBackoff?.seconds &&
String(options.retryPolicy.maximumBackoff.seconds) !== existingSubscription.retryPolicy?.maximumBackoff?.seconds
) {
return true
}
if (!!options.deadLetterPolicy?.maxDeliveryAttempts &&
options.deadLetterPolicy.maxDeliveryAttempts !== existingSubscription.deadLetterPolicy?.maxDeliveryAttempts) {
if (
!!options.deadLetterPolicy?.maxDeliveryAttempts &&
options.deadLetterPolicy.maxDeliveryAttempts !== existingSubscription.deadLetterPolicy?.maxDeliveryAttempts
) {
return true
}
if (!!options.labels && JSON.stringify(existingSubscription.labels) !== JSON.stringify(options.labels)
|| options.labels == null && !!existingSubscription.labels && Object.keys(existingSubscription.labels).length !== 0) {
if (
(!!options.labels && JSON.stringify(existingSubscription.labels) !== JSON.stringify(options.labels)) ||
(options.labels == null && !!existingSubscription.labels && Object.keys(existingSubscription.labels).length !== 0)
) {
return true
}

Expand Down
Loading
Loading