AWS SQS Listener
This package takes care of the boilerplate involved in listening to an SQS queue, as well as sending messages to a queue. Thanks to @eligro91, the package now supports python 3.6!
Installation
pip install awsPySqsListener
Listening to a queue
Here is a basic code sample:
Standard Listener
from sqs_listener import SqsListener
class MyListener(SqsListener):
def handle_message(self, body, attributes, messages_attributes):
run_my_function(body['param1'], body['param2'])
listener = MyListener('my-message-queue', error_queue='my-error-queue', region_name='us-east-1')
listener.listen()
Error Listener
from sqs_listener import SqsListener
class MyErrorListener(SqsListener):
def handle_message(self, body, attributes, messages_attributes):
save_to_log(body['exception_type'], body['error_message']
error_listener = MyErrorListener('my-error-queue')
error_listener.listen()
error_queue (str) - name of queue to push errors.
force_delete (boolean) - delete the message received from the queue, whether or not the handler function is successful. By default the message is deleted only if the handler function returns with no exceptions
interval (int) - number of seconds in between polls. Set to 60 by default
visibility_timeout (str) - Number of seconds the message will be invisible (‘in flight’) after being read. After this time interval it reappear in the queue if it wasn’t deleted in the meantime. Set to ‘600’ (10 minutes) by default
error_visibility_timeout (str) - Same as previous argument, for the error queue. Applicable only if the error_queue argument is set, and the queue doesn’t already exist.
Running as a Daemon
Logging
logger = logging.getLogger('sqs_listener')
logger.setLevel(logging.INFO)
sh = logging.StreamHandler(sys.stdout)
sh.setLevel(logging.INFO)
formatstr = '[%(asctime)s - %(name)s - %(levelname)s] %(message)s'
formatter = logging.Formatter(formatstr)
sh.setFormatter(formatter)
logger.addHandler(sh)
logger = logging.getLogger('sqs_listener')
logger.setLevel(logging.INFO)
sh = logging.FileHandler('mylog.log')
sh.setLevel(logging.INFO)
formatstr = '[%(asctime)s - %(name)s - %(levelname)s] %(message)s'
formatter = logging.Formatter(formatstr)
sh.setFormatter(formatter)
logger.addHandler(sh)
Sending messages
Launcher Example
from sqs_launcher import SqsLauncher
launcher = SqsLauncher('my-queue')
response = launcher.launch_message({'param1': 'hello', 'param2': 'world'})
Important Notes
The environment variable AWS_ACCOUNT_ID must be set, in addition to the environment having valid AWS credentials (via environment variables or a credentials file)
For both the main queue and the error queue, if the queue doesn’t exist (in the specified region), it will be created at runtime.
The error queue receives only two values in the message body: exception_type and error_message. Both are of type str
If the function that the listener executes involves connecting to a database, you should explicitly close the connection at the end of the function. Otherwise, you’re likely to get an error like this: OperationalError(2006, 'MySQL server has gone away')
Contributing
Fork the repo and make a pull request.
Metadata
Release files for awsPySqsListener 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| awsPySqsListener-0.1.2.tar.gz | 7.5 kB | Without provenance | Details for awsPySqsListener-0.1.2.tar.gz |
Release files / awsPySqsListener-0.1.2.tar.gz
| Download URL | awsPySqsListener-0.1.2.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1ffca531e625fb0b908ca8bc55135eee44fb1b05cd3a3ce98d7e10f8c5097356
|
|
BLAKE2b-256 checksum How to use checksums |
479cba750dc97b78589c54f6f204c5ce4350431c89df17c7c8b68c42ee350e26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for awsPySqsListener 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.