Move the logic of recording of states to the MDS from ProgramLifeCycleHttpHandler to the ProgramRunner classes.
Program can be started by various means as:
1. Trigger by Schedule (ScheduleTaskRunner)
2. Explicitly started by rest endpoint/UI (ProgramLifeCycleHttpHandler)
3. Triggered from Workflow (AbstractProgramWorkflowRunner)
The logic to record the state changes need to be duplicated in the places from where program can be started. Instead of this, the logic can be moved to the ProgramRunner classes, so that irrespective of how the program was started, its state will be recorded correctly.
For MapReduce program the fix is done as a part of jira - https://issues.cask.co/browse/CDAP-1293.
The same should be done for other programs(SPARK, FLOW etc.) as well.
To refactor program states, I'll take this on first, then switch out the runtime store for a program status publisher.
A more robust way of recording state changes would be to rely on transactional messaging. This is a platform capability that is being added in 4.0. Once that is in place, we can address this properly. Moving to 4.2