Centralized logic for when a task should be retried or not.