Skip to content

Commit

Permalink
Obtain a session with a pre-request
Browse files Browse the repository at this point in the history
This fix the problem explained in the previous commit:
When we run a job, we don't wait for the answer. So when we don't have
any cookie yet, we send a GET on a very fast controller route which will
return a cookie, used for all the subsequents calls to runjob.

This effectively prevent to create one session file per job.

It still needs intensive testing.

Fixes OCA#44
  • Loading branch information
guewen authored and eantones committed Oct 16, 2020
1 parent 09cf08f commit 1ef902d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions queue_job/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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
11 changes: 11 additions & 0 deletions queue_job/jobrunner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,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 @@ -180,6 +183,13 @@ def _odoo_now():


def _async_http_get(scheme, host, port, user, password, 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 Down Expand Up @@ -215,6 +225,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 1ef902d

Please sign in to comment.