- 
                Notifications
    
You must be signed in to change notification settings  - Fork 51
 
Description
This issue was described in Marco Arena's SObjectizer Tales – 28. If I had a magic wand…. The problem is that when SObjectizer's shutdown is initiated then all agents will handle all demands already in agents' queues. It might take a long time and there is no way at the moment to drop such demands to speed up the shutdown procedure.
I like the idea of telling SObjectizer that an agent agrees to skip remaining demands on shutdown:
class my_agent final : public so_5::agent_t
{
public:
    my_agent(so_5::agent_context_t ctx)
        : agent_t(ctx + drop_events_at_shutdown), ...The question is "how to implement it?"
I'm afraid there is no way to tell something like "drop all demands except evt_finish for all bound agents" to every dispatcher. It's because there are dispatchers that do not have their own demand queues (like Asio's based dispatchers from so5extra companion project). Moreover I don't want to ask a dispatcher to analyze extracted demand because it would make writing of custom dispatchers harder.
But it's possible to speed up demand handling when shutdown is in progress. It means that demands will be extracted from queues the usual way, but they will be skipped (except for evt_finish).
The simplest way is to switch an agent to a special state (maybe the state used for so_deactivate_agent() is appropriate for this purpose too). In such a state an agent has no subscriptions and every demand (except evt_finish) will be ignored the usual way. There is no need to add some new flags to the agents and check those flags during the demand handling procedure.
But switching the agent to that special state will lead to calling on_exit handler for the current agent state (even states if an agent is a hierarchical FSM). I don't know if it is good or not.
Another approach is to just drop all agent's subscriptions and filters but do not change its current state. In such a case demands will be ignored, but on_exit handlers won't be called.
At the moment of writing I prefer an approach with reusing a special state from so_deactivate_agent. And the required behavior of an agent may be described as:
class my_agent final : public so_5::agent_t
{
public:
    my_agent(so_5::agent_context_t ctx)
        : agent_t(ctx + auto_deactivate_at_shutdown), ...That means that an agent asks the SObjectizer to automatically deactivate the agent at the shutdown.
Such an approach avoids introducing a new concept to SObjectizer, because we already have such a thing as the "deactivated agent" and the method so_deactivate_agent.
I would like to collect some feedback before I start work on this feature.