-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { Message, Subscription, Topic } from '@google-cloud/pubsub'; | ||
|
||
import type { | ||
MessageQueue, | ||
MessageQueueEnqueueOptions, | ||
MessageQueueListenOptions, | ||
} from '@fedify/fedify'; | ||
import type { Logger } from '@logtape/logtape'; | ||
|
||
export class GCloudPubSubMessageQueue implements MessageQueue { | ||
private topic: Topic; | ||
private subscription: Subscription; | ||
private logger: Logger; | ||
|
||
constructor(topic: Topic, subscription: Subscription, logger: Logger) { | ||
this.topic = topic; | ||
this.subscription = subscription; | ||
this.logger = logger; | ||
} | ||
|
||
async enqueue( | ||
message: any, | ||
options?: MessageQueueEnqueueOptions, | ||
): Promise<void> { | ||
const delay = options?.delay?.total('millisecond') ?? 0; | ||
|
||
this.logger.info( | ||
`Enqueuing message [FedifyID: ${message.id}] with delay: ${delay}ms`, | ||
); | ||
|
||
setTimeout(() => { | ||
this.topic | ||
.publishMessage({ | ||
json: message, | ||
attributes: { | ||
fedifyID: message.id, | ||
}, | ||
}) | ||
.then((messageId) => { | ||
this.logger.info( | ||
`Message [FedifyID: ${message.id}] was enqueued [PubSubID: ${messageId}]`, | ||
); | ||
}); | ||
}, delay); | ||
} | ||
|
||
async listen( | ||
handler: (message: any) => Promise<void> | void, | ||
options: MessageQueueListenOptions = {}, | ||
): Promise<void> { | ||
this.subscription.on('message', async (message: Message) => { | ||
const fedifyID = message.attributes.fedifyID ?? 'unknown'; | ||
|
||
this.logger.info( | ||
`Handling message [FedifyID: ${fedifyID}, PubSubID: ${message.id}]`, | ||
); | ||
|
||
try { | ||
const json = JSON.parse(message.data.toString()); | ||
|
||
await handler(json); | ||
|
||
message.ack(); | ||
|
||
this.logger.info( | ||
`Acknowledged message [FedifyID: ${fedifyID}, PubSubID: ${message.id}]`, | ||
); | ||
} catch (error) { | ||
this.logger.error( | ||
`Failed to handle message [FedifyID: ${fedifyID}, PubSubID: ${message.id}]: ${error}`, | ||
); | ||
} | ||
}); | ||
|
||
return await new Promise((resolve) => { | ||
options.signal?.addEventListener('abort', () => { | ||
this.subscription.removeAllListeners(); | ||
|
||
this.subscription.close().then(() => resolve()); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { PubSub, type Subscription, type Topic } from '@google-cloud/pubsub'; | ||
|
||
const TOPIC_FEDIFY = 'fedify'; | ||
|
||
export function generateSubscriptionName() { | ||
// TODO: Make this unique in case of multiple instances of the same service | ||
return `${TOPIC_FEDIFY}_subscription`; | ||
} | ||
|
||
export async function initPubSub() { | ||
// Initialise pubsub client | ||
const client = new PubSub({ | ||
projectId: process.env.GCP_PROJECT_ID, | ||
apiEndpoint: process.env.GCP_PUBSUB_HOST, | ||
emulatorMode: process.env.NODE_ENV !== 'production', | ||
}); | ||
|
||
// Initialise topic | ||
const [topics] = await client.getTopics(); | ||
|
||
let topic: Topic | undefined = topics.find( | ||
(topic) => | ||
topic.name === | ||
`projects/${process.env.GCP_PROJECT_ID}/topics/${TOPIC_FEDIFY}`, | ||
); | ||
|
||
if (topic === undefined) { | ||
[topic] = await client.createTopic(TOPIC_FEDIFY); | ||
} | ||
|
||
// Initialise subscription | ||
const subscriptionName = generateSubscriptionName(); | ||
let subscription: Subscription | undefined; | ||
|
||
const [subscriptions] = await topic.getSubscriptions(); | ||
|
||
for (const _subscription of subscriptions) { | ||
const [metadata] = await _subscription.getMetadata(); | ||
|
||
if ( | ||
metadata.name === | ||
`projects/${process.env.GCP_PROJECT_ID}/subscriptions/${subscriptionName}` | ||
) { | ||
subscription = _subscription; | ||
} | ||
} | ||
|
||
if (subscription === undefined) { | ||
[subscription] = await topic.createSubscription(subscriptionName); | ||
} | ||
|
||
// Return topic and subscription | ||
return { topic, subscription }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { generateSubscriptionName } from './gcloud-pubsub'; | ||
|
||
describe('gcloud-pubsub', () => { | ||
describe('generateSubscriptionName', () => { | ||
it('should generate a unique subscription name', () => { | ||
const result = generateSubscriptionName(); | ||
|
||
expect(result).toBe('fedify_subscription'); | ||
}); | ||
}); | ||
}); |