Skip to content

Commit

Permalink
delete token traits via worker
Browse files Browse the repository at this point in the history
  • Loading branch information
justraman committed Oct 4, 2024
1 parent 07ba230 commit 2ac7fa4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
29 changes: 29 additions & 0 deletions src/job-handlers/delete-traits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Queue from 'bull'
import connection from '../connection'
import { JobData } from '../jobs/delete-traits'

export default async (job: Queue.Job<JobData>, done: Queue.DoneCallback) => {
if (!connection.isInitialized) {
await connection.initialize().catch((err) => {
throw err
})
}

const em = connection.manager

await em.query(
`
DELETE FROM trait_token WHERE token_id = $1
`,
[job.data.id]
)

await em.query(
`
DELETE FROM token_rarity WHERE token_id = $1
`,
[job.data.id]
)

done()
}
2 changes: 2 additions & 0 deletions src/job-handlers/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { traitsQueue } from '../jobs/compute-traits'
import { fetchCollectionExtraQueue } from '../jobs/fetch-collection-extra'
import { invalidateExpiredListings } from '../jobs/invalidate-expired-listings'
import { rarityQueue } from '../jobs/rarity-ranker'
import { deleteTraitsQueue } from '../jobs/delete-traits'

async function main() {
if (!connection.isInitialized) {
Expand All @@ -35,6 +36,7 @@ async function main() {
fetchBalanceQueue.process(5, `${__dirname}/fetch-balance.js`)
fetchCollectionExtraQueue.process(5, `${__dirname}/fetch-collection-extra.js`)
invalidateExpiredListings.process(1, `${__dirname}/invalidate-expired-listings.js`)
deleteTraitsQueue.process(1, `${__dirname}/delete-traits.js`)

traitsQueue.on('global:failed', (job, err) => {
throwError(`traitsQueue:Job ${job.id} failed with error: ${err.message}`, 'warning')
Expand Down
20 changes: 20 additions & 0 deletions src/jobs/delete-traits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Queue from 'bull'
import { redisConfig } from './common'

export type JobData = { id: string }

export const deleteTraitsQueue = new Queue<JobData>('deleteTraits', {
defaultJobOptions: { removeOnComplete: true },
redis: redisConfig,
settings: {
maxStalledCount: 1,
},
})

export const deleteTokenTraits = async (id: string) => {
deleteTraitsQueue.add({ id }).catch(() => {
// eslint-disable-next-line no-console
console.log('Closing connection as Redis is not available')
deleteTraitsQueue.close(true)
})
}
17 changes: 2 additions & 15 deletions src/server-extension/resolvers/delete_token_traits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Query, Resolver, Arg } from 'type-graphql'
import 'reflect-metadata'
import { EntityManager } from 'typeorm'
import { deleteTokenTraits } from '../../jobs/delete-traits'

@Resolver()
export class DeleteTokenTraitsResolver {
Expand All @@ -14,21 +15,7 @@ export class DeleteTokenTraitsResolver {
})
tokenId: string
): Promise<boolean> {
const manager = await this.tx()

await manager.query(
`
DELETE FROM trait_token WHERE token_id = $1
`,
[tokenId]
)

await manager.query(
`
DELETE FROM token_rarity WHERE token_id = $1
`,
[tokenId]
)
await deleteTokenTraits(tokenId)

return true
}
Expand Down

0 comments on commit 2ac7fa4

Please sign in to comment.