Skip to content

Commit

Permalink
Merge pull request #53 from guewen/10.0-fix-too-much-anonymous-session
Browse files Browse the repository at this point in the history
[10.0] Use a requests session to limit opened sessions
  • Loading branch information
lmignon committed May 25, 2018
2 parents 13bdb25 + c31a61d commit 896cff6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions queue_job/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ def _try_perform_job(self, env, job):
http.request.env.cr.commit()
_logger.debug('%s done', job)

@http.route('/queue_job/session', type='http', auth="none")
def session(self):
""" Used by the jobrunner to spawn a session
The queue jobrunner uses anonymous sessions when it calls
``/queue_job/runjob``. To avoid having thousands of anonymous
sessions, before running jobs, it creates a ``requests.Session``
and does a GET on ``/queue_job/session``, providing it a cookie
which will be used for subsequent calls to runjob.
"""
return ''

@http.route('/queue_job/runjob', type='http', auth='none')
def runjob(self, db, job_uuid, **kw):
http.request.session.db = db
Expand Down
14 changes: 13 additions & 1 deletion queue_job/jobrunner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
_logger = logging.getLogger(__name__)


session = requests.Session()


# Unfortunately, it is not possible to extend the Odoo
# server command line arguments, so we resort to environment variables
# to configure the runner (channels mostly).
Expand Down Expand Up @@ -166,6 +169,14 @@ def _odoo_now():


def _async_http_get(port, db_name, job_uuid):

if not session.cookies:
# obtain an anonymous session
_logger.info("obtaining an anonymous session for the job runner")
url = ('http://localhost:%s/queue_job/session' % (port,))
response = session.get(url, timeout=30)
response.raise_for_status()

# Method to set failed job (due to timeout, etc) as pending,
# to avoid keeping it as enqueued.
def set_job_pending():
Expand All @@ -188,7 +199,7 @@ def urlopen():
try:
# we are not interested in the result, so we set a short timeout
# but not too short so we trap and log hard configuration errors
response = requests.get(url, timeout=1)
response = session.get(url, timeout=1)

# raise_for_status will result in either nothing, a Client Error
# for HTTP Response codes between 400 and 500 or a Server Error
Expand All @@ -198,6 +209,7 @@ def urlopen():
set_job_pending()
except:
_logger.exception("exception in GET %s", url)
session.cookies.clear()
set_job_pending()
thread = threading.Thread(target=urlopen)
thread.daemon = True
Expand Down

0 comments on commit 896cff6

Please sign in to comment.