Sharing function between controller and scheduler

66 views
Skip to first unread message

David Orme

unread,
Aug 17, 2017, 10:22:50 AM8/17/17
to web2py-users
Hi,

I have a function that checks an uploaded file. I want the check to get scheduled automatically on upload but I also want admin user to be able to run the check live from the website (it doesn't take too long). I currently have a single function in a model that gets shared, but then it has to cope with the  differences in the runtime environment for a controller and for a scheduler worker.

One example is that  worker does not have access to the host name from the request environment, so  `URL(..., host=True)` provides localhost (127.0.0.1). That's easy to solve by loading the host name via AppConfig(), which is in both environments.

The other one is that a worker needs to run db.commit() to get the DAL to run any updates or inserts in the function. Easy enough to stick `db.commit()` in before returning from the function, but the controller will then also commit when it is run from the website. Is this a problem, either for overhead or for the database? Are there any other issues which mean I should keep the scheduler version and controller version separate?

Thanks,
David

Leonel Câmara

unread,
Aug 17, 2017, 1:20:06 PM8/17/17
to web2py-users
That seems fine to me. Note however that you can know if the function is being run by the scheduler looking at request.is_scheduler

David Orme

unread,
Aug 17, 2017, 1:21:42 PM8/17/17
to web...@googlegroups.com
Aha. That is very helpful. Thanks very much.

Cheers,
David

On 17 Aug 2017, at 14:20, Leonel Câmara <leonel...@gmail.com> wrote:

That seems fine to me. Note however that you can know if the function is being run by the scheduler looking at request.is_scheduler

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/a7hHPXDX2OI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Aug 17, 2017, 2:30:30 PM8/17/17
to web2py-users
Also, note that it shouldn't be a problem to call db.commit() within a controller action. The only downside is if the action subsequently throws an exception and a ticket is issued, the transaction that was committed will not be rolled back.

Anthony

David Orme

unread,
Aug 17, 2017, 5:51:44 PM8/17/17
to web2py-users
I've still got some very odd differences between the controller and the scheduler. The task function uses an external module, stored in the application `modules` folder. I've updated it recently, and deleted the .pyc before doing so, so it should all be up to date. I've also restarted the apache2 webserver.

If I run the function from the controller, it works as expected, but scheduled tasks are using the older version of the external module. I've no idea where it could be finding the code - is there anywhere I should look for cached versions?

Oh hang on... Is it that the worker processes were started when the old version was in place? Do you need to restart the workers after updating python modules? I've added new modules, so maybe new modules get added, but existing ones use the version in place when the worker started?

Thanks,
David

Anthony

unread,
Aug 17, 2017, 9:31:00 PM8/17/17
to web2py-users
Yes, the worker processes are separate instances of the Python interpreter, so you must restart them for any changes in modules to take effect.

Anthony
Reply all
Reply to author
Forward
0 new messages