Forums

pymongo authentication error in Always on Task

My always on task is constanly producing the following error

Jun  5 15:50:57 Traceback (most recent call last):
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/pool.py", line 1278, in _get_socket
Jun  5 15:50:57     sock_info = self.sockets.popleft()
Jun  5 15:50:57 IndexError: pop from an empty deque
Jun  5 15:50:57 
Jun  5 15:50:57 During handling of the above exception, another exception occurred:
Jun  5 15:50:57 
Jun  5 15:50:57 Traceback (most recent call last):
Jun  5 15:50:57   File "/home/influrocket4/mysite/keyword-api/webflow/cms-manager/haro/queue_manager.py", line 43, in <module>
Jun  5 15:50:57     if intermediate_email_collection.estimated_document_count() != 0:
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/collection.py", line 1704, in estimated_document_count
Jun  5 15:50:57     return self._count(cmd)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/collection.py", line 1664, in _count
Jun  5 15:50:57     return self.__database.client._retryable_read(
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1464, in _retryable_read
Jun  5 15:50:57     with self._slaveok_for_server(read_pref, server, session,
Jun  5 15:50:57   File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
Jun  5 15:50:57     return next(self.gen)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1309, in _slaveok_for_server
Jun  5 15:50:57     with self._get_socket(server, session, exhaust=exhaust) as sock_info:
Jun  5 15:50:57   File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
Jun  5 15:50:57     return next(self.gen)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1246, in _get_socket
Jun  5 15:50:57     with server.get_socket(
Jun  5 15:50:57   File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
Jun  5 15:50:57     return next(self.gen)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/pool.py", line 1231, in get_socket
Jun  5 15:50:57     sock_info = self._get_socket(all_credentials)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/pool.py", line 1281, in _get_socket
Jun  5 15:50:57     sock_info = self.connect(all_credentials)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/pool.py", line 1197, in connect
Jun  5 15:50:57     sock_info.check_auth(all_credentials)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/pool.py", line 793, in check_auth
Jun  5 15:50:57     self.authenticate(credentials)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/pool.py", line 810, in authenticate
Jun  5 15:50:57     auth.authenticate(credentials, self)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/auth.py", line 673, in authenticate
Jun  5 15:50:57     auth_func(credentials, sock_info)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/auth.py", line 591, in _authenticate_default
Jun  5 15:50:57     return _authenticate_scram(credentials, sock_info, 'SCRAM-SHA-1')
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/auth.py", line 295, in _authenticate_scram
Jun  5 15:50:57     res = sock_info.command(source, cmd)
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/pool.py", line 683, in command
Jun  5 15:50:57     return command(self, dbname, spec, slave_ok,
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/network.py", line 159, in command
Jun  5 15:50:57     helpers._check_command_response(
Jun  5 15:50:57   File "/home/influrocket4/.virtualenvs/env/lib/python3.8/site-packages/pymongo/helpers.py", line 164, in _check_command_response
Jun  5 15:50:57     raise OperationFailure(errmsg, code, response, max_wire_version)
Jun  5 15:50:57 pymongo.errors.OperationFailure: Authentication failed., full error: {'ok': 0, 'errmsg': 'Authentication failed.', 'code': 8000, 'codeName': 'AtlasError'}

I've checked virtual environment and there's no problem there.

Infact when I execute the always on task command in the pythonanywhere bash console my task works just fine without any errors. my task command is

workon env && python /home/influrocket4/mysite/keyword-api/webflow/cms-manager/haro/queue_manager.py

This command works in bash console.

I use the same mongo connector url/method in my web app which is also working. I execute my task on my machine and it's working here too.

Why is my script not working as an always on task?

[edit by admin: formatting]

first this error occurs : IndexError: pop from an empty deque

Then At this line

if some_collection.estimated_document_count() != 0:

comes the authentication error check in the above posted logs for details

.

db = MongoClient("mongodb+srv://randomcred@cluster0-utgos.mongodb.net/test?retryWrites=true&w=majority")

here's a line from my script to esatblish connection

[edit by admin: formatting]

I had problems previously with my connection and after I added the extra parameters on the help page it solved my issue, it sure if that’ll be of any help.

Well as per the help page the extra parameters are only for Web Apps, right?? The help page says use regular Pymongo for scheduled tasks

Tried extra params no change

How exactly are you running it when you run it from a bash console? Are you running the specific command python /home/influrocket4/mysite/keyword-api/webflow/cms-manager/haro/queue_manager.py, or are you using cd to navigate to a directory and then running python XXXX where the XXXX is a subset of that full path?

If it's the latter, you may be hitting a problem resulting from your working directory not being what you expect; you can put cd commands into your always-on task command separated from other commands with &&, just like the one you already have. For example, if you normally cd to /home/influrocket4/mysite/keyword-api/webflow/cms-manager/haro/ and then run python queue_manager.py, you could change your always-on task command to be:

workon env && cd /home/influrocket4/mysite/keyword-api/webflow/cms-manager/haro/ && python queue_manager.py

hey giles, In the bash console I ran the program with the absolute full path.

Found the answer on reddit . Adding authSource to MongoClient solved it. Wasted 18 hours on this forum.

Glad you found a solution eventually.