Problems with logic_hooks in loop

Hello everyone,
I explain the problem:
I’m in a custom module, at the time of saving I start a logic hook that runs a php file of mine, inside I calculate the total number of hours of the records contained in the current module, this total is put in a field of the record, then at each save the total changes and consequently I have to update the totals of the other records. All this using the bean, the problem is that being already in the module, wanting to cycle the records of this module to be able to then save them, the logic hook starts again, in practice goes in a loop.
I use the before_save.
Can you tell me how to solve this problem? Thank you.

The only thing that comes to my mind is to introduce a condition on the hook to prevent the loop when certain criteria is met.

Search for answers here in the forums mentioning fetched_row

Question: Do you use something like $bean->save() method in your logic hook?

Yes, inside the logic hook I use $bean->save(), as already mentioned it is needed to update the data and I know this is why it loops, I wanted a way to avoid the loop

I solved it this way:
I have inserted in the class

static $already_ran = false;

and inside the beforeSave:

if(self::$already_ran == true) return;
		self::$already_ran = true;