SabaKore

Home / Articles / AI guides

Scheduling Recurring AI Batch Jobs on a VPS

This article covers designing batch jobs on a VPS, such as having the AI process things in bulk every night, and building a mechanism that lets you notice when a job fails partway.

Ren, a junior infrastructure engineer

Ren

I want to summarize the data that piles up each day with AI overnight. How should I set that up?
Procura, a cloud AI robot who explains

Procura

A batch job, which runs automatically at a set time, suits that. On a VPS, you can use a mechanism that starts a program at a fixed time and clear all the work that can wait in one go overnight.

When batch jobs are a good fit

  • Work that doesn't need an immediate result
  • Work that is more efficient in bulk (handing over many items together is sometimes faster than calling one by one)
  • Work whose load you want to place in hours when few users are around
Ren, a junior infrastructure engineer

Ren

How is that different from processing items one at a time in real time?
Procura, a cloud AI robot who explains

Procura

The biggest difference is that users don't have to wait, but there's a time lag before results appear. It isn't suited to "I want to know right now," but it fits well for something like "I'd like the results by tomorrow morning."

A way to notice failures

You can't notice right away when a job that runs at night fails. Finding out in the morning that last night's job didn't run is too late, and you'd only learn of it after users are affected. Build in from the start a mechanism that always records whether a job succeeded or failed and sends a notification when it fails.

  • Record the start, the end, and whether the job succeeded or failed
  • Make sure a notification reaches the person in charge when it fails
  • Make it possible to notice when a job is taking longer than expected

Cautions specific to calling AI

When a batch job calls AI in large volume, even if each call costs little, the total can become a significant amount when the number of items is large. Before you start, estimate the rough total from the number of items and the cost per item, and check that it hasn't grown beyond what you expected.

Ren, a junior infrastructure engineer

Ren

What if there are so many items that they can't be finished in one night?
Procura, a cloud AI robot who explains

Procura

If you design it to carry over whatever didn't finish to the next day, you don't need to force it all to finish in one night. If the number of items keeps growing, that's also a sign to review your plan, so it helps to watch how the processing time changes as well.

Preventing duplicate runs

Another accident that tends to happen with batch jobs is that the next scheduled time arrives while the previous day's job is still unfinished, and a new job starts a second time. When the same job runs twice at once, results get duplicated and calls to the AI increase needlessly. Make sure the next job starts only after confirming the previous one has finished, or add a mechanism that allows only one to run at a time.

  • Start the next job only after checking that the previous one has finished
  • Add a control that prevents duplicate runs so the same job doesn't run several times at once
  • If the processing time keeps growing, take particular care that it doesn't overlap with the next run
Procura, a cloud AI robot who explains

Procura

A nightly batch is an unglamorous mechanism, but used well it improves both the speed users feel and the cost. Start by working out which processing doesn't need to be rushed.
Ren, a junior infrastructure engineer

Ren

So I put work that can wait into a nightly batch, and design it including how I'll notice failures.

This article is translated from the Japanese original. If the two differ, the Japanese version is the one we keep up to date.