Skip to content

Scheduler integration and external event bridges

An application may need to observe work performed by its scheduler and send navigation events to another part of the system. ExecKernel provides two connection points for these needs, with different origins and data.

An external scheduler decides when to run a task and executes it. Its integration can then publish a SchedulerEvent for listeners to receive. An event bridge receives a typed representation of selected engine events so your application can deliver it to another component.

Need Origin of the fact Publication channel Consumer
Record completion of a scheduled task Your application’s worker or scheduler. SchedulerDispatcher publishes a SchedulerEvent through a FlowEventsDispatcher. Callbacks subscribed to the scheduler event.
Report that a session entered a node An ExecKernel operation. The dispatcher transforms the runtime event and calls EventBridgeInterface::publish(). The adapter your application connected as a bridge.

Sharing event infrastructure does not make these channels an automatic chain. Publishing SCHED.TASK.COMPLETED delivers that fact to scheduler listeners; it does not itself generate an ExecKernelEvent for the bridge or execute a choice.

Suppose your application periodically refreshes its course catalogue. An existing worker runs catalog-refresh-17 and reports its duration and outcome when it finishes. This example publishes that fact and collects what a listener receives.

use ExecKernel\Domain\Engine\FlowEventsDispatcher;
use ExecKernel\Domain\Engine\SchedulerEvent;
use ExecKernel\Infrastructure\Scheduler\SchedulerDispatcher;
$received = [];
$schedulerChannel = new FlowEventsDispatcher();
$schedulerChannel->on(
'SCHED.TASK.COMPLETED',
function (array $event) use (&$received): void {
$received[] = $event;
},
);
$publisher = new SchedulerDispatcher($schedulerChannel);
$publisher->publishTaskEvent(SchedulerEvent::taskCompleted(
eventId: 'catalog-refresh-17-completed',
occurredAt: '2026-12-04T09:00:00Z',
schedulerInstanceId: 'catalog-worker-1',
taskId: 'catalog-refresh-17',
durationMs: 120,
result: 'ok',
));

The callback receives this payload:

{
"event_type": "SCHED.TASK.COMPLETED",
"event_id": "catalog-refresh-17-completed",
"occurred_at": "2026-12-04T09:00:00Z",
"scheduler_instance_id": "catalog-worker-1",
"severity": "info",
"context": {
"task_id": "catalog-refresh-17",
"duration_ms": 120,
"result": "ok"
},
"event_source": "scheduler",
"event_phase": "external",
"canonical_event": "SCHED.TASK.COMPLETED",
"timestamp": 1796374800
}

event_id identifies the external fact, and scheduler_instance_id identifies its publisher. occurred_at gives the occurrence time with an explicit timezone; the dispatcher adds the corresponding timestamp. context contains task data supplied by the scheduler. The external phase and scheduler source distinguish this fact from results calculated by the engine.

Your application chooses the callback’s work: update a catalogue indicator, record a measurement, or notify another component. The example retains the event in memory for inspection. It does not refresh the catalogue; publish the fact from the point in your worker where that work has already finished.

A catalogue task can affect the whole application and need no session. If an external fact belongs to one session, publishTaskEvent() accepts sessionId. It also accepts flowId for a numeric flow identifier. When an event includes graph_code or session_id in its context, the dispatcher uses them for graph or session subscriptions.

Keep these identities consistent. If you supply a sessionId different from the one inside the event, publication is rejected. An explicitly supplied timestamp must match occurred_at. Publish external facts outside an active engine operation.

SchedulerDispatcher has family-specific methods, including publishTaskEvent() and publishTemporalEvent(), which check the event type. Use this typed channel to publish scheduler facts; a generic dispatch('SCHED.…') call does not replace it.

The second case starts inside the engine. We will reuse Events and runtime listeners: the learning-path graph starts at node 10, and choosing the guide through edge 101 reaches terminal node 20. We want to observe those node entries through a bridge.

We will use InMemoryEventBridge, an implementation of EventBridgeInterface that retains the objects it receives. Connecting it to the factory exposes the outgoing contract without requiring a messaging service in the example.

use ExecKernel\Infrastructure\Engine\ExecKernelFactory;
use ExecKernel\Infrastructure\EventBridge\InMemoryEventBridge;
use ExecKernel\Infrastructure\Loader\JsonGraphLoader;
$bridge = new InMemoryEventBridge();
$engine = (new ExecKernelFactory())->build(eventBridge: $bridge);
$graph = (new JsonGraphLoader())->load(__DIR__ . '/learning-path.json');
$start = $engine->start(
$graph, 'en', sessionId: 'activity-demo', timestamp: 1796374800,
);
$engine->choose(
$graph, $start->state, 101, 'en',
sessionId: 'activity-demo', timestamp: 1796374801,
);

The dispatcher transforms selected runtime events into ExecKernelEvent objects. These have an ExecKernelEventType value in type, a data array, and correlation fields such as sessionId, graphCode, operationId, and eventId.

The transformation changes the representation. For example, runtime.session_started and runtime.node_visited produce typed node.enter events, with the node identifier in data['node_id']. The bridge does not receive a copy of every dispatcher name and field.

Select the received node entries:

use ExecKernel\Domain\Engine\ExecKernelEventType;
$nodeEntries = [];
foreach ($bridge->events() as $event) {
if ($event->type === ExecKernelEventType::NODE_ENTER) {
$nodeEntries[] = [
'type' => $event->type->value,
'node_id' => $event->data['node_id'],
'graph_code' => $event->graphCode,
'session_id' => $event->sessionId,
'phase' => $event->phase,
];
}
}

The result, restricted to those fields, is:

[
{
"type": "node.enter",
"node_id": 10,
"graph_code": "learning-path",
"session_id": "activity-demo",
"phase": "result"
},
{
"type": "node.enter",
"node_id": 20,
"graph_code": "learning-path",
"session_id": "activity-demo",
"phase": "result"
}
]

The same bridge can receive other projected types during these operations. The filter selects the ones our view needs. For your integration, implement EventBridgeInterface::publish(ExecKernelEvent $event): void and pass that instance through eventBridge:. Inside the method, choose how to transform and deliver data to your application’s consumer. LogEventBridge is another included implementation, intended for writing log records.

Decide when to deliver and who executes work

Section titled “Decide when to deliver and who executes work”

Result events reach the bridge when the operation’s batch is published. If your application saves state, it can use deferEvents: true and publish after storage confirms the save, as described in Events and runtime listeners. A bridge can also receive diagnostic events; select the type and phase appropriate to your integration.

The bridge call is synchronous. If its publish() method throws, the dispatcher isolates the failure; it does not revert the session calculation or automatically schedule a retry. An adapter that must deliver data to another process needs to connect the transport, retained work, and recovery required by your application. This example’s in-memory bridge does not retain events after the process ends.

Likewise, publishing a scheduler fact does not start or resume a session. If scheduled work must advance a session, your application’s worker must obtain its state, call the engine, and confirm the new result through its storage integration. A time or cron condition is evaluated when the graph is queried or executed; it does not schedule that call itself.

The external-event-channels.php download contains both complete channels and uses learning-path.json. Place both files in your project root, alongside the vendor/ directory. The script loads vendor/autoload.php from that root. The runtime and scheduler event catalogue covers the available families, fields, and projections.