@@ -322,13 +322,24 @@ def mark_import_as_done(self, cycle, msg):
322322 :return:
323323 """
324324 self .ensure_one ()
325- cycle .is_locked = False
326- cycle .locked_reason = None
327- cycle .message_post (body = msg )
325+ cycle .write ({"is_locked" : False , "locked_reason" : False })
326+ try :
327+ cycle .message_post (body = msg )
328+ except Exception :
329+ _logger .exception ("Failed to post completion chatter on cycle %s" , cycle .id )
328330
329331 # Refresh statistics after bulk operations
330332 cycle .refresh_statistics ()
331333
334+ def mark_import_as_failed (self , cycle , msg ):
335+ """Run via on_error() when async beneficiary import fails."""
336+ self .ensure_one ()
337+ cycle .write ({"is_locked" : False , "locked_reason" : False })
338+ try :
339+ cycle .message_post (body = msg )
340+ except Exception :
341+ _logger .exception ("Failed to post failure chatter on cycle %s" , cycle .id )
342+
332343 def mark_prepare_entitlement_as_done (self , cycle , msg ):
333344 """Complete the preparation of entitlements.
334345 Base :meth:`mark_prepare_entitlement_as_done`.
@@ -340,13 +351,24 @@ def mark_prepare_entitlement_as_done(self, cycle, msg):
340351 :return:
341352 """
342353 self .ensure_one ()
343- cycle .is_locked = False
344- cycle .locked_reason = None
345- cycle .message_post (body = msg )
354+ cycle .write ({"is_locked" : False , "locked_reason" : False })
355+ try :
356+ cycle .message_post (body = msg )
357+ except Exception :
358+ _logger .exception ("Failed to post completion chatter on cycle %s" , cycle .id )
346359
347360 # Update Statistics
348361 cycle ._compute_entitlements_count ()
349362
363+ def mark_prepare_entitlement_as_failed (self , cycle , msg ):
364+ """Run via on_error() when async entitlement preparation fails."""
365+ self .ensure_one ()
366+ cycle .write ({"is_locked" : False , "locked_reason" : False })
367+ try :
368+ cycle .message_post (body = msg )
369+ except Exception :
370+ _logger .exception ("Failed to post failure chatter on cycle %s" , cycle .id )
371+
350372 def mark_check_eligibility_as_done (self , cycle ):
351373 """Complete the enrollment of eligible beneficiaries.
352374 Base :meth:`mark_check_eligibility_as_done`.
@@ -356,13 +378,25 @@ def mark_check_eligibility_as_done(self, cycle):
356378 :param cycle: A recordset of cycle
357379 :return:
358380 """
359- cycle .is_locked = False
360- cycle .locked_reason = None
361- cycle .message_post (body = _ ("Eligibility check finished." ))
381+ self .ensure_one ()
382+ cycle .write ({"is_locked" : False , "locked_reason" : False })
383+ try :
384+ cycle .message_post (body = _ ("Eligibility check finished." ))
385+ except Exception :
386+ _logger .exception ("Failed to post completion chatter on cycle %s" , cycle .id )
362387
363388 # Compute Statistics
364389 cycle ._compute_members_count ()
365390
391+ def mark_check_eligibility_as_failed (self , cycle ):
392+ """Run via on_error() when async eligibility check fails."""
393+ self .ensure_one ()
394+ cycle .write ({"is_locked" : False , "locked_reason" : False })
395+ try :
396+ cycle .message_post (body = _ ("Eligibility check failed." ))
397+ except Exception :
398+ _logger .exception ("Failed to post failure chatter on cycle %s" , cycle .id )
399+
366400
367401class DefaultCycleManager (models .Model ):
368402 _name = "spp.cycle.manager.default"
@@ -535,6 +569,7 @@ def _check_eligibility_async(self, cycle, beneficiaries_count):
535569 )
536570 main_job = group (* jobs )
537571 main_job .on_done (self .delayable (channel = "statistics_refresh" ).mark_check_eligibility_as_done (cycle ))
572+ main_job .on_error (self .delayable (channel = "statistics_refresh" ).mark_check_eligibility_as_failed (cycle ))
538573 main_job .delay ()
539574
540575 def _check_eligibility (
@@ -624,6 +659,11 @@ def _prepare_entitlements_async(self, cycle, beneficiaries_count):
624659 cycle , _ ("Entitlement Ready." )
625660 )
626661 )
662+ main_job .on_error (
663+ self .delayable (channel = "statistics_refresh" ).mark_prepare_entitlement_as_failed (
664+ cycle , _ ("Entitlement preparation failed." )
665+ )
666+ )
627667 main_job .delay ()
628668
629669 def _prepare_entitlements (self , cycle , offset = 0 , limit = None , min_id = None , max_id = None , do_count = False ):
@@ -870,6 +910,9 @@ def _add_beneficiaries_async(self, cycle, beneficiaries, state):
870910 main_job .on_done (
871911 self .delayable (channel = "statistics_refresh" ).mark_import_as_done (cycle , _ ("Beneficiary import finished." ))
872912 )
913+ main_job .on_error (
914+ self .delayable (channel = "statistics_refresh" ).mark_import_as_failed (cycle , _ ("Beneficiary import failed." ))
915+ )
873916 main_job .delay ()
874917
875918 def _add_beneficiaries (self , cycle , beneficiaries , state = "draft" , do_count = False ):
0 commit comments