Bugfix PRESIDECMS-2566: checking your tasks

We have just released a bugfix PRESIDECMS-2566 for the adhoc task system. The fix addresses an issue where some tasks created with runnow=true failed to run at all and are now stuck in a pending state forever. This article addresses how to check for issues and deal with the situation.

Check your project for problems

You can run the following SQL against your project's database to see whether or not you are effected by this issue:

select   event, count(1) as n 
from     psys_taskmanager_adhoc_task 
where    next_attempt_date is null 
and      status = 'pending'
and      DateDiff( datecreated, Now() ) < 0 
group by event order by n desc

If you get back an empty result set, then you have no issues to resolve. You may get a resultset looking something like this:

event                                              |n  |
---------------------------------------------------+---+
crmMergeCandidates._checkForMergeCandidatesOnUpdate|525|
crmMergeCandidates._checkForMergeCandidatesOnInsert|505|
admin.systemAlerts.runCheckInBackgroundThread      | 33|

In which case you have some work to do..

Resolve issues

We have deliberately not dealt with this issue automatically by re-running tasks in order to not suddenly overwhelm your applications with a queue of tasks that may have significant impact. For each of your "failed-to-start" tasks, you need to decide:

  1. Can I just delete it and ignore it? (great for trivial tasks)
  2. Can I just queue it for running at the earliest opportunity?

In deciding on this, you may require inspecting individual tasks if they are particularly critical. There is an event_args json field on the psys_taskmanager_adhoc_task table with the custom data for the task that may aid you with the decision.

Deleting trivial tasks

The tasks in the example above are all trivial. We would/could delete them with:

delete from psys_taskmanager_adhoc_task 
where    next_attempt_date is null 
and      status = 'pending'
and      DateDiff( datecreated, Now() ) < 0 
and event in ( 'crmMergeCandidates._checkForMergeCandidatesOnUpdate', 'crmMergeCandidates._checkForMergeCandidatesOnInsert' , 'admin.systemAlerts.runCheckInBackgroundThread');

Requeuing non-trivial tasks

To queue a task for running at the next opportunity:

update psys_taskmanager_adhoc_task 
set next_attempt_date = Now()
where    next_attempt_date is null 
and      status = 'pending'
and      DateDiff( datecreated, Now() ) < 0 
and      event = 'my.important.event'