jegesh / python-sqs-listener

A simple wrapper for boto3 for listening, and sending, to an AWS SQS queue
Other
154 stars 72 forks source link

handle_message in sample_daemon does not execute? #13

Closed sayanb closed 5 years ago

sayanb commented 6 years ago

Hi,

I'm trying to run this as a Daemon, but unfortunately I am having a really hard time making it work. I don't usually code in Python, so apologies if there is a basic mistake at my end.

I have set up all the AWS credentials correctly and have enabled some logging in the sample daemon file by putting the following lines:

import logging

logger = logging.getLogger('sqs_listener')
logger.setLevel(logging.INFO)

sh = logging.FileHandler('/home/ubuntu/python_errors/error.log')
sh.setLevel(logging.INFO)

formatstr = '[%(asctime)s - %(name)s - %(levelname)s]  %(message)s'
formatter = logging.Formatter(formatstr)

sh.setFormatter(formatter)
logger.addHandler(sh)

When I run the sample daemon as

sudo python sample_daemon.py start,

It does run, as I see

Starting listener daemon Initializing listener

Then, in the log files, I see logs created by the file __init__.py, which are

[2018-02-20 13:51:30,300 - sqs_listener - INFO] 1 messages received [2018-02-20 13:51:30,300 - sqs_listener - WARNING] Unable to parse message - JSON is not formatted properly

coming from these lines

sqs_logger.info( str(len(messages['Messages'])) + " messages received")

and

sqs_logger.warning("Unable to parse message - JSON is not formatted properly")

in __init__.py

So it is able to read from the queue. All this is fine. However, no code I put in the handle_message() function of the sample_daemon executes.

For example, in my sample_daemon.py, I have added

def handle_message(self, body, attributes, messages_attributes):
        logger.info("all done")
        file = open("testfile.txt","w")
        file.write("Hello World")
        file.close() 

Just some test code, but neither can I see the text "all done" in the logfile (although the same logfile has logs created by __Init__.py), nor can I see any testfile.txt created. Please help!

jegesh commented 6 years ago

If you run the listener directly - not as a daemon - does your test code work?

On Feb 20, 2018 4:13 PM, "sayanb" notifications@github.com wrote:

Hi,

I'm trying to run this as a Daemon, but unfortunately I am having a really hard time making it work. I don't usually code in Python, so apologies if there is a basic mistake a my end.

I have set up all the AWS credentials correctly and have enabled some logging in the sample daemon file by putting the following lines:

import logging

logger = logging.getLogger('sqs_listener') logger.setLevel(logging.INFO)

sh = logging.FileHandler('/home/ubuntu/python_errors/error.log') sh.setLevel(logging.INFO)

formatstr = '[%(asctime)s - %(name)s - %(levelname)s] %(message)s' formatter = logging.Formatter(formatstr)

sh.setFormatter(formatter) logger.addHandler(sh)

When I run the sample daemon as

sudo python sample_daemon.py start,

It does run, as I see

Starting listener daemon Initializing listener

Then, in the log files, I see logs created by the ile init.py, which are

[2018-02-20 13:51:30,300 - sqs_listener - INFO] 1 messages received [2018-02-20 13:51:30,300 - sqs_listener - WARNING] Unable to parse message

  • JSON is not formatted properly

coming from these lines

sqs_logger.info( str(len(messages['Messages'])) + " messages received")

and

sqs_logger.warning("Unable to parse message - JSON is not formatted properly")

in init.py

So it is able to read from the queue. All this is fine. However, no code I put in the handle_message() function of the sample_daemon executes.

For example, in my sample_daemon.py, I have added

def handle_message(self, body, attributes, messages_attributes): logger.info("all done") file = open("testfile.txt","w") file.write("Hello World") file.close()

Just some test code, but neither can I see the text "all done" in the logfile (although the same logfile has logs created by Init.py), nor can I see any testfile.txt created. Please help!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jegesh/python-sqs-listener/issues/13, or mute the thread https://github.com/notifications/unsubscribe-auth/AGO5FsNcSNBGJJlZboyQ_vo5rhuxIPP7ks5tWtMFgaJpZM4SMA82 .

jegesh commented 6 years ago

If you read the readme more carefully, you'll notice that handle_message is a method of SqsListener that you need to override in a subclass.

On Feb 21, 2018 1:58 PM, "Yaakov Gesher" ormezuza@gmail.com wrote:

If you run the listener directly - not as a daemon - does your test code work?

On Feb 20, 2018 4:13 PM, "sayanb" notifications@github.com wrote:

Hi,

I'm trying to run this as a Daemon, but unfortunately I am having a really hard time making it work. I don't usually code in Python, so apologies if there is a basic mistake a my end.

I have set up all the AWS credentials correctly and have enabled some logging in the sample daemon file by putting the following lines:

import logging

logger = logging.getLogger('sqs_listener') logger.setLevel(logging.INFO)

sh = logging.FileHandler('/home/ubuntu/python_errors/error.log') sh.setLevel(logging.INFO)

formatstr = '[%(asctime)s - %(name)s - %(levelname)s] %(message)s' formatter = logging.Formatter(formatstr)

sh.setFormatter(formatter) logger.addHandler(sh)

When I run the sample daemon as

sudo python sample_daemon.py start,

It does run, as I see

Starting listener daemon Initializing listener

Then, in the log files, I see logs created by the ile init.py, which are

[2018-02-20 13:51:30,300 - sqs_listener - INFO] 1 messages received [2018-02-20 13:51:30,300 - sqs_listener - WARNING] Unable to parse message - JSON is not formatted properly

coming from these lines

sqs_logger.info( str(len(messages['Messages'])) + " messages received")

and

sqs_logger.warning("Unable to parse message - JSON is not formatted properly")

in init.py

So it is able to read from the queue. All this is fine. However, no code I put in the handle_message() function of the sample_daemon executes.

For example, in my sample_daemon.py, I have added

def handle_message(self, body, attributes, messages_attributes): logger.info("all done") file = open("testfile.txt","w") file.write("Hello World") file.close()

Just some test code, but neither can I see the text "all done" in the logfile (although the same logfile has logs created by Init.py), nor can I see any testfile.txt created. Please help!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jegesh/python-sqs-listener/issues/13, or mute the thread https://github.com/notifications/unsubscribe-auth/AGO5FsNcSNBGJJlZboyQ_vo5rhuxIPP7ks5tWtMFgaJpZM4SMA82 .

nilsbuer commented 3 years ago

Hi, I want to poll an SQS queue for new messages. I run the listener directly not as demaon.

I have a similar issue. I get the error :

2020-11-13 10:03:26,975 - INFO - 1 messages received 2020-11-13 10:03:26,975 - WARNING - Unable to parse message - JSON is not formatted properly

I used your code from the example you provide:

from sqs_listener import SqsListener

class MyListener(SqsListener): def handle_message(self, body, attributes, messages_attributes): pass

run_my_function(body['param1'])

       print ("handle_message")

listener = MyListener('Orders',error_queue='orders-dead-letter-queue',region_name='eu-central-1') listener.listen()

I read that your comment "override in a subclass". Can you give an example for that?

pushpik commented 3 years ago

Hi, I want to poll an SQS queue for new messages. I run the listener directly not as demaon.

I have a similar issue. I get the error :

2020-11-13 10:03:26,975 - INFO - 1 messages received 2020-11-13 10:03:26,975 - WARNING - Unable to parse message - JSON is not formatted properly

I used your code from the example you provide:

from sqs_listener import SqsListener

class MyListener(SqsListener): def handle_message(self, body, attributes, messages_attributes): pass

run_my_function(body['param1'])

print ("handle_message")

listener = MyListener('Orders',error_queue='orders-dead-letter-queue',region_name='eu-central-1') listener.listen()

I read that your comment "override in a subclass". Can you give an example for that?

I too got the same error and noticed this - As per sqs documentation, the message body is string whereas this package expect it to be a valid json.

nilsbuer commented 3 years ago

Hi, the Problem was that my message body did not have JSON format. An example for a working message example is message body: {"Name":"Peter"}

Best regards, Nils

Holen Sie sich Outlook für Androidhttps://aka.ms/ghei36


Von: Pushpendra Sijairya notifications@github.com Gesendet: Mittwoch, 18. November 2020, 22:44 An: jegesh/python-sqs-listener Cc: Nils Buer; Comment Betreff: Re: [jegesh/python-sqs-listener] handle_message in sample_daemon does not execute? (#13)

Hi, I want to poll an SQS queue for new messages. I run the listener directly not as demaon.

I have a similar issue. I get the error :

2020-11-13 10:03:26,975 - INFO - 1 messages received 2020-11-13 10:03:26,975 - WARNING - Unable to parse message - JSON is not formatted properly

I used your code from the example you provide:

from sqs_listener import SqsListener

class MyListener(SqsListener): def handle_message(self, body, attributes, messages_attributes): pass

run_my_function(body['param1'])

print ("handle_message")

listener = MyListener('Orders',error_queue='orders-dead-letter-queue',region_name='eu-central-1') listener.listen()

I read that your comment "override in a subclass". Can you give an example for that?

I too got the same error and noticed this - As per sqs documentation, the message body is string whereas this package expect it to be a valid json.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/jegesh/python-sqs-listener/issues/13#issuecomment-729974888, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AGDGUXJE7ID5YT2TCZXF4RTSQQ52BANCNFSM4ERQB43A.