Skip to content

Commit

Permalink
feat: publish AssetBundleConverted event to notify about textures rea…
Browse files Browse the repository at this point in the history
…dy (#179)
aleortega authored Dec 5, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent a346c51 commit 9523a4d
Showing 7 changed files with 937 additions and 6 deletions.
6 changes: 6 additions & 0 deletions consumer-server/.env.default
Original file line number Diff line number Diff line change
@@ -13,5 +13,11 @@ HTTP_SERVER_HOST=0.0.0.0
# reset metrics at 00:00UTC
WKC_METRICS_RESET_AT_NIGHT=false


ENV=prd
SENTRY_DSN=

AWS_SNS_ARN=arn
PLATFORM=webgl
# PLATFORM=windows
# PLATFORM=mac
3 changes: 2 additions & 1 deletion consumer-server/package.json
Original file line number Diff line number Diff line change
@@ -15,8 +15,9 @@
"semi": false
},
"dependencies": {
"@aws-sdk/client-sns": "^3.699.0",
"@dcl/cdn-uploader": "^1.4.1-20230208155013.commit-f75a6ee",
"@dcl/schemas": "^15.1.1",
"@dcl/schemas": "^15.2.0",
"@sentry/node": "^8.27.0",
"@well-known-components/env-config-provider": "^1.2.0",
"@well-known-components/http-server": "^2.1.0",
32 changes: 32 additions & 0 deletions consumer-server/src/adapters/sns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { PublishCommand, SNSClient } from '@aws-sdk/client-sns'
import { AppComponents, PublisherComponent } from '../types'

export async function createSnsComponent({ config }: Pick<AppComponents, 'config'>): Promise<PublisherComponent> {
const snsArn = await config.requireString('AWS_SNS_ARN')
const optionalEndpoint = await config.getString('AWS_SNS_ENDPOINT')

const client = new SNSClient({
endpoint: optionalEndpoint ? optionalEndpoint : undefined
})

async function publishMessage(event: any, attributes: { type: string; subType: string }): Promise<void> {
const command = new PublishCommand({
TopicArn: snsArn,
Message: JSON.stringify(event),
MessageAttributes: {
type: {
DataType: 'String',
StringValue: attributes.type
},
subType: {
DataType: 'String',
StringValue: attributes.subType
}
}
})

await client.send(command)
}

return { publishMessage }
}
5 changes: 4 additions & 1 deletion consumer-server/src/components.ts
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ import { createMemoryQueueAdapter, createSqsAdapter } from './adapters/task-queu
import { DeploymentToSqs } from '@dcl/schemas/dist/misc/deployments-to-sqs'
import { createRunnerComponent } from './adapters/runner'
import { createSentryComponent } from './adapters/sentry'
import { createSnsComponent } from './adapters/sns'

// Initialize all the components of the app
export async function initComponents(): Promise<AppComponents> {
@@ -56,6 +57,7 @@ export async function initComponents(): Promise<AppComponents> {
const cdnS3 = s3Bucket ? new AWS.S3({}) : new MockAws.S3({})

const runner = createRunnerComponent()
const publisher = await createSnsComponent({ config })

return {
config,
@@ -67,6 +69,7 @@ export async function initComponents(): Promise<AppComponents> {
taskQueue,
cdnS3,
runner,
sentry
sentry,
publisher
}
}
21 changes: 21 additions & 0 deletions consumer-server/src/service.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { setupRouter } from './controllers/routes'
import { executeConversion, executeLODConversion } from './logic/conversion-task'
import checkDiskSpace from 'check-disk-space'
import { AppComponents, GlobalContext, TestComponents } from './types'
import { AssetBundleConvertedEvent, Events } from '@dcl/schemas'

// this function wires the business logic (adapters & controllers) with the components (ports)
export async function main(program: Lifecycle.EntryPointParameters<AppComponents | TestComponents>) {
@@ -26,6 +27,10 @@ export async function main(program: Lifecycle.EntryPointParameters<AppComponents
const logger = components.logs.getLogger('main-loop')

components.runner.runTask(async (opt) => {
const platform = (await components.config.requireString('PLATFORM')).toLocaleLowerCase() as
| 'windows'
| 'mac'
| 'webgl'
while (opt.isRunning) {
if (await machineRanOutOfSpace(components)) {
logger.warn('Stopping program due to lack of disk space')
@@ -47,6 +52,22 @@ export async function main(program: Lifecycle.EntryPointParameters<AppComponents
job.animation
)
}

const eventToPublish: AssetBundleConvertedEvent = {
type: Events.Type.ASSET_BUNDLE,
subType: Events.SubType.AssetBundle.CONVERTED,
key: `${job.entity.entityId}-${platform}`,
timestamp: Date.now(),
metadata: {
platform: platform,
entityId: job.entity.entityId
}
}

await components.publisher.publishMessage(eventToPublish, {
type: Events.Type.ASSET_BUNDLE,
subType: Events.SubType.AssetBundle.CONVERTED
})
} finally {
components.metrics.decrement('ab_converter_running_conversion')
}
5 changes: 5 additions & 0 deletions consumer-server/src/types.ts
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ export type BaseComponents = {
cdnS3: S3
runner: IRunnerComponent
sentry: SentryComponent
publisher: PublisherComponent
}

// components used in runtime
@@ -53,3 +54,7 @@ export type HandlerContextWithPath<
>

export type Context<Path extends string = any> = IHttpServerComponent.PathAwareContext<GlobalContext, Path>

export type PublisherComponent = {
publishMessage(event: any, attributes: { type: string; subType: string }): Promise<void>
}
871 changes: 867 additions & 4 deletions consumer-server/yarn.lock

Large diffs are not rendered by default.

0 comments on commit 9523a4d

Please sign in to comment.