vapor/queues

How to wait for one job to complete before starting the next one?

henrypratt opened this issue · 8 comments

I am using an AsyncScheduledJob to queue up 4 jobs, but I need them to be run one after another instead of in parallel. I can't find any information online about how to do this.

You can create a parent job that executes those child jobs one by one.

That is what I am trying to do, but can't seem to figure out how.

In the run of one job, when you get to the end, queue the next job up

But jobs are dispatched asynchronously and as far as I know there is no way to do that, unless you mean dispatching the next job from within the previous job? Here is a sample of what I am doing:

// Dispatch job 1
try await context
    .queue
    .dispatch(
        AsyncJob1.self,
        .init(""),
        maxRetryCount: 3
    )

// Dispatch job 2
try await context
    .queue
    .dispatch(
        AsyncJob2.self,
        .init(""),
        maxRetryCount: 3
    )

// Dispatch job 3
try await context
    .queue
    .dispatch(
        AsyncJob3.self,
        .init(""),
        maxRetryCount: 3
    )

// Dispatch job 4
try await context
    .queue
    .dispatch(
        AsyncJob4.self,
        .init(""),
        maxRetryCount: 3
    )

// At this point all jobs are dispatched but none have been completed.

Even though I am using await, the next job gets queued up immediately. Shouldn't it wait for the completion of the job? This is all from within an AsyncScheduledJob by the way.

unless you mean dispatching the next job from within the previous job

That's exactly what I mean. Jobs are not designed to be sequential or one at a time, a worker will process them as it's available to

Ok thanks. So I think at this point it just makes more sense to not run the work as jobs.

Or just run everything in one job - either works

Yeah I am going to run it all in a scheduled job. Thanks!