-
-
Save Bogdanp/c796b15a0fa8d577a5f46ff25230baa6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define next-work-item-query | |
#<<SQL | |
select | |
id, status::text, slug::text, timezone | |
from | |
sites | |
where | |
status in ('created', 'needs-upgrade') | |
for update | |
skip locked | |
limit 1 | |
SQL | |
) | |
(define ((make-worker-thread provisioner)) | |
(define database (provisioner-database provisioner)) | |
(define (do-work conn) | |
(match (query-maybe-row conn next-work-item-query) | |
[(vector id "created" subdomain timezone) | |
(provision conn id subdomain timezone)] | |
[(vector id "needs-upgrade" subdomain timezone) | |
(upgrade conn id subdomain timezone)] | |
[else #f])) | |
(let loop () | |
(with-handlers ([exn:fail? (lambda (e) | |
(log-provisioner-error (exn-message e)) | |
(sleep 5))]) | |
(when (not (call-with-database-transaction database do-work)) | |
(define amount (max 1 (random 5))) | |
(log-provisioner-debug "No work items. Sleeping for ~as." amount) | |
(sleep amount))) | |
(loop))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment