All MicroEvals
You are working in an empty repository. Build a small but pr...
Create MicroEval
Header image for You are working in an empty repository. Build a small but pr...

You are working in an empty repository. Build a small but pr...

Prompt

You are working in an empty repository. Build a small but production-oriented persistent job queue using **Python 3.12**. ## Objective Create a command-line application that stores jobs in SQLite and processes them safely using multiple worker processes. Use only the Python standard library. Do not use third-party packages. ## Functional requirements A job must contain: * A unique job ID * A job type * A JSON payload * A priority * A creation timestamp * A scheduled execution timestamp * A status * An attempt counter * A configurable maximum number of attempts * An optional idempotency key * The most recent error message Support these statuses: * `pending` * `running` * `completed` * `retrying` * `failed` Implement the following CLI commands: ```text python -m jobqueue init --database jobs.db python -m jobqueue enqueue \ --database jobs.db \ --type print \ --payload '{"message":"hello"}' \ --priority 10 \ --max-attempts 3 \ --idempotency-key example-1 python -m jobqueue worker \ --database jobs.db \ --workers 4 \ --lease-seconds 30 python -m jobqueue list --database jobs.db python -m jobqueue retry-failed --database jobs.db ``` ## Processing behavior Implement at least these built-in job types: * `print`: prints the value of `payload["message"]` * `sleep`: waits for `payload["seconds"]` * `fail`: intentionally raises an exception Workers must safely claim jobs using SQLite transactions so that two workers cannot process the same valid lease simultaneously. When a worker claims a job: 1. Set its status to `running`. 2. Record a lease expiration timestamp. 3. Increment its attempt counter. 4. Commit the claim before executing the handler. If a worker crashes or stops after claiming a job, another worker must be able to reclaim it after the lease expires. On execution failure: * Retry the job when attempts remain. * Use exponential backoff based on the attempt number. * Cap the backoff at 60 seconds. * Store a useful error message. * Mark the job as `failed` after the final attempt. When an idempotency key is supplied, enqueueing the same key more than once must not create duplicate jobs. This guarantee must remain valid under concurrent enqueue attempts. The worker command must handle `Ctrl+C` gracefully: * Stop claiming new jobs. * Allow currently executing jobs a reasonable opportunity to finish. * Exit without corrupting the database. Use UTC timestamps consistently. ## Engineering requirements Organize the project into clear modules rather than placing everything in one file. Include: * Type hints * Useful error handling * Database schema initialization * Schema constraints and indexes * Atomic job-claiming logic * A concise README * Automated tests using `unittest` * Commands for running the application and tests Tests must cover at least: 1. Enqueueing and completing a job 2. Idempotency-key deduplication 3. Retrying a failed job 4. Moving a job to `failed` 5. Reclaiming an expired lease 6. Two workers competing for the same job 7. Priority ordering 8. Scheduled jobs not running early 9. Invalid JSON payload handling Concurrency tests must have finite timeouts and must not hang indefinitely. ## Deliverables Complete the implementation in the repository. At the end, report: 1. The architecture and important design decisions 2. The files created or modified 3. The commands used to run the tests 4. The test results 5. Any known limitations or assumptions Do not merely describe the solution. Implement it and run the tests.

Drag to resize
Drag to resize