export const claimNextPending = mutation({
args: { apiToken: v.string(), workerId: v.string() },
handler: async (ctx, { apiToken, workerId }) => {
if (apiToken !== process.env.NARRATOR_API_TOKEN)
throw new Error('Unauthorized')
const next = await ctx.db
.query('articles')
.withIndex('status', q => q.eq('status', 'pending'))
.first()
if (!next) {
console.debug('No pending articles to claim')
return null
}
// Mark as processing with a lease
await ctx.db.patch(next._id, {
status: 'processing',
claimedBy: workerId,
claimedAt: Date.now(),
lastError: undefined,
})
return { _id: next._id, url: next.url }
},
})
export const claimNextPending = mutation({
args: { apiToken: v.string(), workerId: v.string() },
handler: async (ctx, { apiToken, workerId }) => {
if (apiToken !== process.env.NARRATOR_API_TOKEN)
throw new Error('Unauthorized')
const next = await ctx.db
.query('articles')
.withIndex('status', q => q.eq('status', 'pending'))
.first()
if (!next) {
console.debug('No pending articles to claim')
return null
}
// Mark as processing with a lease
await ctx.db.patch(next._id, {
status: 'processing',
claimedBy: workerId,
claimedAt: Date.now(),
lastError: undefined,
})
return { _id: next._id, url: next.url }
},
})