Try ERPNext Buy Support Partners Foundation

RuntimeError: no object bound to session

Hi all,

need help on an error raised by a background job. Basically i’m testing a new scheduler using rq-scheduler and I want to display a realtime message to all site users using:

frappe.publish_realtime(event='msgprint', message='Welcome to all'))

on bash window:

08:16:45 worker_default.1 | 08:16:45 RuntimeError: no object bound to session
08:16:45 worker_default.1 | Traceback (most recent call last):
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/rq/worker.py", line 713, in perform_job
08:16:45 worker_default.1 |     rv = job.perform()
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/rq/job.py", line 558, in perform
08:16:45 worker_default.1 |     self._result = self._execute()
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/rq/job.py", line 564, in _execute
08:16:45 worker_default.1 |     return self.func(*self.args, **self.kwargs)
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/background_jobs.py", line 210, in test_job
08:16:45 worker_default.1 |     frappe.publish_realtime(event='msgprint', message='sleeping for {0} secs ...'.format(s))
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1285, in publish_realtime
08:16:45 worker_default.1 |     return frappe.async.publish_realtime(*args, **kwargs)
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/apps/frappe/frappe/async.py", line 76, in publish_realtime
08:16:45 worker_default.1 |     user = frappe.session.user
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/werkzeug/local.py", line 347, in __getattr__
08:16:45 worker_default.1 |     return getattr(self._get_current_object(), name)
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/werkzeug/local.py", line 310, in _get_current_object
08:16:45 worker_default.1 |     raise RuntimeError('no object bound to %s' % self.__name__)
08:16:45 worker_default.1 | RuntimeError: no object bound to session
08:16:45 worker_default.1 | Traceback (most recent call last):
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/rq/worker.py", line 713, in perform_job
08:16:45 worker_default.1 |     rv = job.perform()
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/rq/job.py", line 558, in perform
08:16:45 worker_default.1 |     self._result = self._execute()
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/rq/job.py", line 564, in _execute
08:16:45 worker_default.1 |     return self.func(*self.args, **self.kwargs)
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/background_jobs.py", line 210, in test_job
08:16:45 worker_default.1 |     frappe.publish_realtime(event='msgprint', message='sleeping for {0} secs ...'.format(s))
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1285, in publish_realtime
08:16:45 worker_default.1 |     return frappe.async.publish_realtime(*args, **kwargs)
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/apps/frappe/frappe/async.py", line 76, in publish_realtime
08:16:45 worker_default.1 |     user = frappe.session.user
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/werkzeug/local.py", line 347, in __getattr__
08:16:45 worker_default.1 |     return getattr(self._get_current_object(), name)
08:16:45 worker_default.1 |   File "/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/werkzeug/local.py", line 310, in _get_current_object
08:16:45 worker_default.1 |     raise RuntimeError('no object bound to %s' % self.__name__)
08:16:45 worker_default.1 | RuntimeError: no object bound to session

any hint?

You need to connect first with frappe.connect()

I’m connecting:

def enqueue_events_for_site(site, function, queue, event, job_id, **kwargs):
    try:
        frappe.init(site=site)
        if frappe.local.conf.maintenance_mode:
            return

        if frappe.local.conf.pause_scheduler:
            return

        frappe.connect()
        if is_scheduler_disabled():
            return

        print("kwargs", kwargs, function, queue, None, event, True, job_id)

        enqueue(function, queue, None, event, True, job_id, False, **kwargs)

        frappe.logger(__name__).debug('Queued events for site {0}'.format(site))

    except:
        frappe.logger(__name__).error('Exception in Enqueue Events for Site {0}'.format(site) +
                                      '\n' + frappe.get_traceback())
        raise

    finally:
        frappe.destroy()

then the flow should be the same as it’s enqueued using frappe.background_jobs.enqueue

What i’m doing wrong?

@rmehta i’d like to send a pull request to integrate a “new” scheduler based on rq-scheduler, any of the team can help?