Skip to content

Commit

Permalink
Merge pull request #51 from syleam/11.0-allow-other-than-localhost
Browse files Browse the repository at this point in the history
[FIX] Allow to run jobs when Odoo is not listening on localhost
  • Loading branch information
guewen committed Apr 5, 2018
2 parents 221dd95 + 0f9833d commit 7810948
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
19 changes: 17 additions & 2 deletions queue_job/jobrunner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,23 @@ class QueueJobRunnerThread(Thread):
def __init__(self):
Thread.__init__(self)
self.daemon = True
port = os.environ.get('ODOO_QUEUE_JOB_PORT') or config['http_port']
self.runner = QueueJobRunner(port or 8069)
scheme = (os.environ.get('ODOO_QUEUE_JOB_SCHEME') or
config.misc.get("queue_job", {}).get('scheme'))
host = (os.environ.get('ODOO_QUEUE_JOB_HOST') or
config.misc.get("queue_job", {}).get('host') or
config['http_interface'])
port = (os.environ.get('ODOO_QUEUE_JOB_PORT') or
config.misc.get("queue_job", {}).get('port') or
config['http_port'])
user = (os.environ.get('ODOO_QUEUE_JOB_HTTP_AUTH_USER') or
config.misc.get("queue_job", {}).get('http_auth_user'))
password = (os.environ.get('ODOO_QUEUE_JOB_HTTP_AUTH_PASSWORD') or
config.misc.get("queue_job", {}).get('http_auth_password'))
self.runner = QueueJobRunner(scheme or 'http',
host or 'localhost',
port or 8069,
user,
password)

def run(self):
# sleep a bit to let the workers start at ease
Expand Down
53 changes: 42 additions & 11 deletions queue_job/jobrunner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
* Optionally adjust your configuration through environment variables:
- set ``ODOO_QUEUE_JOB_CHANNELS=root:4`` (or any other channels
configuration) if you don't want the default ``root:1``.
- if ``xmlrpc-port`` is not set, you can set it for the jobrunner only with:
``ODOO_QUEUE_JOB_PORT=8069``.
- ``ODOO_QUEUE_JOB_CHANNELS=root:4`` (or any other channels
configuration), default ``root:1``.
- ``ODOO_QUEUE_JOB_SCHEME=https``, default ``http``.
- ``ODOO_QUEUE_JOB_HOST=load-balancer``, default ``localhost``.
- ``ODOO_QUEUE_JOB_PORT=443``, default ``xmlrpc-port`` or 8069.
- ``ODOO_QUEUE_JOB_HTTP_AUTH_USER=jobrunner``, default empty.
- ``ODOO_QUEUE_JOB_HTTP_AUTH_PASSWORD=s3cr3t``, default empty.
* Alternatively, configure the channels through the Odoo configuration
file, like:
Expand All @@ -37,6 +39,11 @@
[queue_job]
channels = root:4
scheme = https
host = load-balancer
port = 443
http_auth_user = jobrunner
http_auth_password = s3cr3t
* Or, if using ``anybox.recipe.odoo``, add this to your buildout configuration:
Expand All @@ -46,6 +53,11 @@
recipe = anybox.recipe.odoo
(...)
queue_job.channels = root:4
queue_job.scheme = https
queue_job.host = load-balancer
queue_job.port = 443
queue_job.http_auth_user = jobrunner
queue_job.http_auth_password = s3cr3t
* Start Odoo with ``--load=web,web_kanban,queue_job``
and ``--workers`` greater than 1 [2]_, or set the ``server_wide_modules``
Expand Down Expand Up @@ -165,7 +177,7 @@ def _odoo_now():
return _datetime_to_epoch(dt)


def _async_http_get(port, db_name, job_uuid):
def _async_http_get(scheme, host, port, user, password, db_name, job_uuid):
# Method to set failed job (due to timeout, etc) as pending,
# to avoid keeping it as enqueued.
def set_job_pending():
Expand All @@ -183,12 +195,15 @@ def set_job_pending():
# if this was python3 I would be doing this with
# asyncio, aiohttp and aiopg
def urlopen():
url = ('http://localhost:%s/queue_job/runjob?db=%s&job_uuid=%s' %
(port, db_name, job_uuid))
url = ('%s://%s:%s/queue_job/runjob?db=%s&job_uuid=%s' %
(scheme, host, port, db_name, job_uuid))
try:
auth = None
if user:
auth = (user, password)
# 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 = requests.get(url, timeout=1, auth=auth)

# raise_for_status will result in either nothing, a Client Error
# for HTTP Response codes between 400 and 500 or a Server Error
Expand Down Expand Up @@ -281,8 +296,18 @@ def set_job_enqueued(self, uuid):

class QueueJobRunner(object):

def __init__(self, port=8069, channel_config_string=None):
def __init__(self,
scheme='http',
host='localhost',
port=8069,
user=None,
password=None,
channel_config_string=None):
self.scheme = scheme
self.host = host
self.port = port
self.user = user
self.password = password
self.channel_manager = ChannelManager()
if channel_config_string is None:
channel_config_string = _channels()
Expand Down Expand Up @@ -328,7 +353,13 @@ def run_jobs(self):
_logger.info("asking Odoo to run job %s on db %s",
job.uuid, job.db_name)
self.db_by_name[job.db_name].set_job_enqueued(job.uuid)
_async_http_get(self.port, job.db_name, job.uuid)
_async_http_get(self.scheme,
self.host,
self.port,
self.user,
self.password,
job.db_name,
job.uuid)

def process_notifications(self):
for db in self.db_by_name.values():
Expand Down

0 comments on commit 7810948

Please sign in to comment.