Improve locking when assigning leases
Description
This MR improves two aspects of our use of locking when assigning leases. Firstly, it explicitly begins a transaction before doing anything else when using SQLite, in order to lock the database to prevent the same job being assigned multiple times. The default behaviour is to only lock when writing, so multiple concurrent SELECT
statements can return the same row, with no way of knowing that row is in the process of being updated. This has a bit of a performance impact, since locking is only possible at the database/file level in SQLite.
The second difference is to improve the FOR UPDATE
lock we use with PostgreSQL, by adding SKIP LOCKED
to the query. This will prevent concurrent queries waiting for locks on rows that are changing anyway.