Run the workers (operator)
A worker takes one survey at a time from the queue, runs it, stores the result and sends the email. The API never runs a survey itself, so without at least one worker nothing leaves the queue. Several workers can run at once, on one server or several; the database gives each job to exactly one of them.
Getting started (operator)
Apply migrations
0003_queue.sqland0004_waitlist_email.sqlafter0001and0002, then run the verification queries in each header.The worker reads the same environment variables as the API (see Run the API):
DATABASE_URL,MARKETSIM_PROVIDER,ANTHROPIC_API_KEY,MARKETSIM_EMBEDDER,FREE_DAILY_BUDGET_USD,FREE_TIER_PAUSED,PUBLIC_BASE_URL, and the four mail variables. It does not need the sign-in secret. Optional:WORKER_ID, a name for this copy (default: host name and process number).Start one:
python -m marketsim_workerCheck it:
GET /admin/queueas an operator showsworkers_aliveat 1 within a few seconds.Start a second copy the same way for more throughput.
How it behaves
| Behaviour | Detail |
|---|---|
| Pace | One survey per worker at a time, 8 interviews in parallel inside it. Two workers finish roughly 40 free surveys an hour |
| Heartbeat | Every 15 seconds while working, and on every poll while idle |
| A worker dies mid-survey | After 120 seconds of silence its job goes back to the queue and another worker takes it. The dead worker's late result, if any, is not accepted |
| Retries | A failed survey is queued again, up to 3 attempts. After the third it is marked failed, the account's free survey and the committed budget are given back, and the "did not finish" email is sent once |
| Missing respondents | If more than 20 percent of the panel gave no answer (provider refusals or errors), the attempt counts as failed and is retried |
| Daily budget | A job queued when the day's free budget was full waits for the next day and commits its budget when it is claimed. If that day is full too, it waits another day |
| Pause | With FREE_TIER_PAUSED=1 workers claim nothing; queued studies wait |
| Waitlist and welcome emails | On every pass a worker admits the day's share of the waitlist and sends each account its "on the list" and "you are in" email, once. A refused email is tried again on the next pass |
| Paid surveys | Claimed before free ones, 24 interviews in parallel (PAID_INTERVIEW_WORKERS). One that fails three times is refunded by the worker; see Payments setup |
| Identity and alerts | On start a worker records its host, process and version; on a clean stop it says goodbye, so a crash can be told from a stop. On every pass it checks the whole system and raises or clears alerts, mailing new ones to ALERT_EMAIL. See The operator console |
| Bulk surveys | A survey marked for bulk is sent to the provider's batch service a stage at a time and then holds no worker. Each pass a worker looks at one waiting survey that has not been looked at for a minute. See Bulk surveys |
| Stopping | On a stop signal the worker finishes the survey in hand, then exits. Give the service a stop grace period of several minutes. If it is killed anyway, the job is retried |
Reading the queue
GET /admin/queue (operators only) returns: queued, queued_for_later (waiting for tomorrow's
budget), running, oldest_wait_seconds, workers_alive, done_last_hour, failed_last_day,
free_tier_paused.
Limits
- Email is sent through SendGrid when
SENDGRID_API_KEYandMAIL_FROMare set on the worker (see Email setup); otherwise messages stay in memory and nothing is sent. - There is no alert yet when the queue grows or no worker is alive; look at
/admin/queue. - The worker has been tested with the fake provider only. A live provider run through the queue has not been done.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
workers_alive is 0 and studies stay queued |
No worker process is running, or it cannot reach the database | Start a worker; check DATABASE_URL in its environment |
| "DATABASE_URL is not set" at start | The worker was started without its environment | Set the variables or put them in .env beside where it starts |
| Studies stay queued although a worker is alive | FREE_TIER_PAUSED is on, or the jobs are waiting for tomorrow's budget (queued_for_later) |
Unset the pause; raise FREE_DAILY_BUDGET_USD if the budget is too tight |
| A study shows "This survey did not finish." | Three attempts failed; the reason is on the study and in jobs.error |
Fix the cause (for example the provider key), then the user presses Run it again |
| Log line "N of M respondents gave no answer (...)" | The provider refused or failed more than 20 percent of the interviews, often a rate limit | Run fewer workers, or wait; the job is retried by itself |
failed_last_day keeps rising |
Every run is failing for the same reason | Set FREE_TIER_PAUSED=1 to hold the queue while you fix it; nothing queued is lost |
A job shows running for a long time |
Its worker died less than 120 seconds ago, or the survey is large | Wait; it is handed on by itself |