Maciej Wisniowski wrote:
Hi

I want to use lovely.remotetask in Z3 application.
Use case is very simple:
User views page, this causes long running function to be started
and Ajax calls from the page check every second if there is a result.

I have:
1. content object (Folderish) with instance of remotetask.TaskService()
    called 'monitor_service' inside

2. mytask = remotetask.task.SimpleTask(myLongRunningFunction)

3. named utility for 'mytask'

The question is how should I start and stop remotetask jobs?
Currently, when user enters a page I call (every time):

jobid = self['monitor_service'].add(u'mytask', appdict)
self['monitor_service'].startProcessing()

Then ajax is repeadetly checking for result and when
status is Completed I call

self['monitor_service'].stopProcessing()

It works nice, but after looking into service.py code I see
it will break when I'll have few concurrent users.

Seems that I should call startProcessing() on zope startup
and stopProcessing on zope shutdown?

For startup do this :

@component.adapter(IDatabaseOpenedEvent)
def startRemoteTask(event):
    """Start the amazon remotetask on startup."""
    db = event.database
    connection = db.open()
    root = connection.root()
    root_folder = root.get(ZopePublication.root_name, None)
    for site in root_folder.values():
        service = component.queryUtility(
            ITaskService, context=site)
        if service is not None:
            if not service.isProcessing():
                service.startProcessing()

I assume here that the remote tasks are located in one of the site directly contained in root.


For shutdown :

I have no solution for this right now.

_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to