jruizgit / rules

Durable Rules Engine
MIT License
1.14k stars 209 forks source link

Event consequence gets called multiple times for lengthy operation #362

Open honglujiang opened 3 years ago

honglujiang commented 3 years ago

Hi Jesus, I'm trying to use durable rules in Python to evaluate a list of data records. Each record has multiple fields that I use to write the conditions. The consequence function could be a lengthy database operation. I loop through the record list and call post('rule_set', record, call_back) on each record to post event. I found that if I have about 65 records in my list and each of them trigger the consequence that involves lengthy database operation, the consequence on the same record could be triggered multiple times which result in duplicate records being created in my database. Please note that I gave each record a different sid before I post the event. Could you tell me how I can avoid the consequence being called multiple times on one record when I post the event?

Thanks, Hong Lu

jruizgit commented 3 years ago

Hi, thanks for posting the question. Assuming you are using asynchronous actions to execute the DB operation? If so, you can override the 5 second timeout on the action. Please see docs for more info on async actions:

https://github.com/jruizgit/rules/blob/master/docs/py/reference.md#async-actions

honglujiang commented 3 years ago

Hi Jesus, Yes I was using asynchronous actions to execute the DB operation. I changed it to call post event synchronously without the callback function parameter. But the consequence is still triggered multiple times. I wonder what is causing this to happen.

Thanks, Hong Lu

jruizgit commented 3 years ago

When the rules engine dispatches an action, it takes a lock on the context state, the lock will be released if the action is not completed within 30 seconds. You can renew the action lease by calling get_host().renew_action_lease('rule_set').