Using Worker Service

Hi,

Hi,

I would like to use Worker Service mentioned here,

And I found this article on how to set worker in trytond.conf file

My current trytond.conf is like below:

[web]
listen = domain:port
root = /somedir/www/
[database]
uri = postgresql://dbuser:secret@something:port/dbname
[queue]
worker = True

Can anybody show me how to utilize this from any python class programmatically?
A link to a git code would be appreciated too.

Regards
Bromo

Submitting task to the queue is explained in Task Queue — Tryton server

1 Like

This is what I did:

    @classmethod
    def write(cls, records, values, *args):
        result = super().write(records, values, *args)
        cls.launch(0)
        return result

    @classmethod
    def launch(cls, records):
        cls.__queue__.process(records)

    def process(self, values):
        logger.info(">>"*50)
        logger.info(">>>>>>>>>>>>>>>>>>>> PROCESS RUNNING...")

I put a call to cls.launch(0) from inside write method, but when I executed update data that triggers write method.. the process(self, values) doesn’t run.. where am I missing?

You need to have a worker running.

1 Like

Hi, may I know how to turn on a worker? … I really thought that the config enough, sorry for this rookie question.

See How to start the server — Tryton server

1 Like

Ok what I did:

In trytond.conf file:

[web]
listen = localhost:8000
root = /var/www/
[database]
uri = postgresql://tryton:secret@localhost:5432/tryton
[queue]
worker = True

In python class:

    @classmethod
    def write(cls, records, values, *args):
		#--- a lot of code -----
		
        cls.__queue__.process(list([1]))
        return result

    def process(cls):
        logger.info(f">>>>>>>>>>>>>>>>>>>>>>>>> EXECUTING PROCESS")
        #--- perform database operations ---

In command line:

trytond -c trytond.conf --logconf logging.conf
trytond-worker -c trytond.conf -d tryton --logconf logging.conf

I did all of the above.. but still the database operations codes inside process method not executed

Where else I missed?

Bromo

Do you find your job in the ir_queue table? What are the values?

This is the content of ir.queue table.. yes it is correct that the worker is running.. but it doesn’t perform the database operation that is inside the process method.

The real code inside def process(cls) method is like below:

def process(cls):
    logger.info(f">>>>>>>>>>>>>>>>>>>>>>>>> EXECUTING PROCESS")
    pool = Pool()
    ProjectAllocation = pool.get('afx.project.allocation')
    ProjectAllocation.update_project_allocation_records()
    

The method of ProjectAllocation.update_project_allocation_records() is not executed.
The initial reason is: I want ProjectAllocation.update_project_allocation_records() executed async and non blocking.. because the execution of it takes more than 30 seconds.. and I am hoping by doing this async via Task Queue.. I can execute it on different thread.

Or am I doing it wrongly?

Bromo

I guess there should be a traceback in the log of the worker because you put a call for process with a list of id but the process method does not accept arguments.

Okay, I changed the process method and add arguments as below:

    @classmethod
    def write(cls, records, values, *args):
		#--- a lot of code -----
		
        cls.__queue__.process(list([1]))
        return result

    def process(cls, values):
        logger.info(f">>>>>>>>>>>>>>>>>>>>>>>>> EXECUTING PROCESS {values}")
        pool = Pool()

        #--- Below method call are the most important thing
        #--- because the whole idea of using this queue is to
        #--- delegate the execution of below class methods
        #--- outside the main thread

        ProjectAllocation = pool.get('afx.project.allocation')
        ProjectAllocation.update_project_allocation_records()


and now the record in ir.queue become like below:

{"args": [], "user": 1, "model": "afx.project", "kwargs": {}, "method": "process",
 "context": {"client": "863512e1-9e70-4d99-889b-d578e191d62d", 
"groups": [1, 772, 773, 774, 701, 702, 466, 464, 467, 703, 704, 472, 465, 
468, 469, 778, 470, 471], "company": 1, "_request": {"scheme": "http", 
"http_host": "localhost:8000", "is_secure": false, "remote_addr": "127.0.0.1"},
 "employee": 1, "company_filter": "one", "click_my_projects": "1", 
"company_work_time": {"M": 576000, "Y": 6912000, "d": 28800, "h": 3600, "m": 60,
 "s": 1, "w": 144000}, "language_direction": "ltr"}, "instances": [1]}

But event thou this process method may be called via Queue… the codes within the process method aren’t executed, which is the most important thing.

Bromo

I kind of deduce that the jobs are correctly dequeued because it seems from the screenshot that the column has been filled.
So I suspect that it is working but just that you do not look at the right thing.