csima
csima

Reputation: 325

One-time jobs on Azure workers

What is the best way to do a one-time job on Azure?

Say we want to extend a table in the associated database with a double column. All the new entries will have this value computed by the worker(s) at insertion, but somebody has to take care of the entries that are already in the table. I thought of two alternatives:

  1. a method called by the worker only if a database entry (say, "JobRun") is set to true, and the method would flip the entry to false.
  2. a separate app that does the job, and which is downloaded and run manually using remote desktop (I cannot connect the local app to the Azure SQL server).

The first alternative is messy (how should I deal with the code at the next deployment? delete it? comment it? leave it there? also, what if I will have another job in the future? create a new database entry "Job2Run"?). The second one looks like a cheap hack. I am sure that there is a better way I could not think of.

Upvotes: 1

Views: 190

Answers (1)

Sandrino Di Mattia
Sandrino Di Mattia

Reputation: 24895

If you want to run a job once you'll need to take into account the following:

  • Concurrency: While the job is running, make sure no other worker picks up the job and runs it at the same time (you can use leases for this. More info here).
  • Once the job is done, you'll need to keep track (in Table Storage, SQL Azure, ...) that the job completed successfully. The next time a worker tries to pick up the job, it will look in Table Storage / SQL Azure / ..., it will see that the job completed and skip the job.
  • Failure: Maybe your worker crashes during the job which should allow another worker to pick up the job without any issue.

In your specific use case I would also consider using a tool like dbup to manage updates to your schema and existing data with SQL Scripts. Tools like these keep track of which scripts have been executed by adding them in a table in the database.

Upvotes: 1

Related Questions