Skip to main content

Queue service built on top of mongo.

Project description

mongo_queue

Task queue built on mongo with channels and unique job id.

WebsiteautobotAI Automation Platform

Inspired from kapilt/mongoqueue

Change Log:

v0.1.2

  • Added diskUsage for larger queue dependency resolution.

v0.1.1

  • Added find_and_update for finding the next job added process to pick the next job if the previous is already locked with multiple retries..

v0.1.0

  • Added optional inc_attempt parameter for job.release. This will allow user to choose if they want to increment the attempt when releasing a job.

v0.0.9

  • Added method find_by_id to find a job by it's id.

v0.0.7

  • Added mongo backward compatibility. The aggregate function was using lookup which is only available after Mongo 3.6 (Not avaialble in the DocumentDB), Modified lookup to use old syntax.

v0.0.6

  • Added sleep and state feature while releasing a job. This provides a way to not pickup job until provided seconds and store state for long running jobs.

v0.0.5

  • Added depends_on feature. You can create dependency between jobs by supplying depends_on[] with previously created job ids.

v0.0.3

  • Added unique index with job_id and channel. This is to make sure that the same job is not added multiple times. If not job id provided an unique id generated by default.

Usage

Install the package.

pip install mongo_queue

Usage Example:

  • Create Queue Object
from mongo_queue.queue import Queue
from pymongo import MongoClient

queue = Queue(MongoClient('localhost', 27017).task_queue, consumer_id="consumer-1", timeout=300, max_attempts=3)
  • Add task to queue default channel
queue.put({"task_id": 1})
  • Add task to queue with priority to default channel
queue.put({"task_id": 1}, priority=1)
  • Add task to queue in a specific channel
queue.put({"task_id": 1}, priority=1, channel="channel_1")
  • Add task to queue with unique job_id
queue.put({"task_id": 1}, priority=1, channel="channel_1", job_id="x_job")
  • Add task with dependency
job1 = queue.put({"task_id": 1}, priority=1, channel="channel_1", job_id="x_job")
job2 = queue.put({"task_id": 2}, priority=1, channel="channel_1", job_id="x_job", depends_on=[job1])
  • Get the next job to be executed from the default channel
job = queue.next()
  • Get the next job to be executed from a specific channel
job = queue.next(channel="channel_1")
  • Update job progress for long-running jobs
job.progress(count=10)
  • Put the job back in queue, this will be picked up again later, this will update attempts after max attempts the job will not be picked up again.
  • You can also set state and sleep while releaseing a job
  • sleep in seconds. The job will not be picked up again till the sleep time expires.
  • state you can store state in the job for long running jobs.
job.release()
# or
job.release(sleep=10, state={"some": "state"})
  • Put the job back in queue with error, this will be picked up again later, this will update attempts after max attempts the job will not be picked up again.
job.error("Some error occured")
  • Complete the job. This will delete job from the database.
job.complete()

Build Steps

# Setup venv of python version 3.6 and above
python3.9 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install wheel
pip install --upgrade twine
rm -rf dist
python setup.py sdist bdist_wheel
python -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*

Local Development and Testing

export MONGO_URI=mongodb+srv://username:pwd@mongourl/test?retryWrites=true&w=majority
cd mong_queue # Root directory of the package
python3.9 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python -m unittest mongo_queue.test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongo_queue_service-0.1.2.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

mongo_queue_service-0.1.2-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file mongo_queue_service-0.1.2.tar.gz.

File metadata

  • Download URL: mongo_queue_service-0.1.2.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for mongo_queue_service-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2582733c8ec84f7678500b9c4dc956c71bb2255901e270b8e603d9b3ae80763f
MD5 87aae4f0266c942f879828b53b8b4955
BLAKE2b-256 34845b5e4ad8fe02df4af841178965de317b7f2cec7635c691bd36b5d314fa59

See more details on using hashes here.

File details

Details for the file mongo_queue_service-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mongo_queue_service-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 765a65e037cfe3e71443763768f0cd6344ca92277d8474a6989ebabf430a59f8
MD5 efb580d40cb060c0328249a0a42a6b8a
BLAKE2b-256 638a0bde24ad8d735c9503b3da8b22b086c675d775dc95346269a9dc15d98687

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page