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 #44
  • Loading branch information
guewen committed Feb 8, 2018
1 parent 555e362 commit c92395d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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: 12 additions & 2 deletions 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 All @@ -164,10 +167,16 @@ def _odoo_now():
dt = datetime.datetime.utcnow()
return _datetime_to_epoch(dt)

session = requests.Session()


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 Down Expand Up @@ -200,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 c92395d

Please sign in to comment.