bruseB
Convex Communityβ€’4mo agoβ€’
28 replies
bruse

Mutation without a write still triggers query (python client)

Hi! Just curious about the dynamics of mutations.

I have a mutation that is supposed to claim the next pending job, but exits early if there are no such jobs. When it returns early it just returns null, and when there is a job to claim it will patch that entry to update a state variable, plus a few more things.

My listening queries in other clients seem to get triggered even if the mutation exited early though. Is this expected? I would have thought that nothing would be part of that mutations write set, thus not triggering any reads/queries.

Here's the mutation in question:
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 }
  },
})


Sorry, I'm sure this has been asked before, but I honestly couldn't find previous questions about it.
Was this page helpful?