Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ void start_or_stop_program(ProgramSession& program_session){
std::string error;
switch (program_session.current_state()){
case ProgramState::STOPPED:
error = program_session.start_program();
error = program_session.start_program(ActionSource::UI, std::nullopt);
break;
case ProgramState::RUNNING:
error = program_session.stop_program();
error = program_session.stop_program(ActionSource::UI, std::nullopt);
break;
default:;
}
Expand Down
19 changes: 15 additions & 4 deletions SerialPrograms/Source/CommonFramework/ProgramSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ void ProgramSession::set_state(ProgramState state){
m_state.store(state, std::memory_order_relaxed);
m_listeners.run_method(&Listener::state_change, state);
}
void ProgramSession::mark_started(ActionSource source, std::optional<std::string> request_id){
const WallClock now = current_time();
m_last_state_change.store(now, std::memory_order_relaxed);
m_transitions.started(source, std::move(request_id), now);
}
void ProgramSession::set_finished(){
m_transitions.finished(current_time());
set_state(ProgramState::STOPPED);
}
void ProgramSession::push_stats(){
m_listeners.run_method(
&Listener::stats_update,
Expand All @@ -185,6 +194,7 @@ void ProgramSession::push_stats(){
);
}
void ProgramSession::push_error(const std::string& message){
m_transitions.error_reported();
m_listeners.run_method(&Listener::error, message);
}

Expand Down Expand Up @@ -227,7 +237,7 @@ void ProgramSession::update_historical_stats_with_current(){
}


std::string ProgramSession::start_program(){
std::string ProgramSession::start_program(ActionSource source, std::optional<std::string> request_id){
m_logger.log("Received Program Start Request");
std::lock_guard<Mutex> lg(m_lock);

Expand All @@ -249,7 +259,7 @@ std::string ProgramSession::start_program(){

// Now start the program.
m_logger.log("Starting program...");
m_last_state_change.store(current_time(), std::memory_order_relaxed);
mark_started(source, std::move(request_id));
set_state(ProgramState::RUNNING);
m_program_thread = GlobalThreadPools::unlimited_realtime().dispatch_now_blocking(
[this]{
Expand Down Expand Up @@ -284,7 +294,7 @@ std::string ProgramSession::start_program(){
"Invalid State: " + std::to_string((int)state)
);
}
std::string ProgramSession::stop_program(){
std::string ProgramSession::stop_program(ActionSource source, std::optional<std::string> request_id){
m_logger.log("Received Stop Request");
{
std::lock_guard<Mutex> lg(m_lock);
Expand All @@ -299,6 +309,7 @@ std::string ProgramSession::stop_program(){
return "Program is already stopped.";
case ProgramState::RUNNING:
m_logger.log("Stopping program...");
m_transitions.stop_requested(source, std::move(request_id), current_time());
set_state(ProgramState::STOPPING);
break;
case ProgramState::STOPPING:
Expand Down Expand Up @@ -449,7 +460,7 @@ void ProgramSession::run_program(){
std::lock_guard<Mutex> lg(m_lock);
push_stats();
update_historical_stats_with_current();
set_state(ProgramState::STOPPED);
set_finished();
}
}

Expand Down
22 changes: 18 additions & 4 deletions SerialPrograms/Source/CommonFramework/ProgramSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define PokemonAutomation_CommonFramework_ProgramSession_H

#include <atomic>
#include <optional>
#include <vector>
#include <string>
#include "Common/Cpp/Logging/TaggedLogger.h"
Expand All @@ -29,6 +30,7 @@
#include "CommonFramework/Globals.h"
//#include "CommonFramework/Logging/Logger.h"
#include "CommonFramework/Panels/ProgramDescriptor.h"
#include "CommonFramework/ProgramTransition.h"
#include "CommonFramework/Tools/ProgramEnvironment.h"
// #include "CommonFramework/ResourceDownload/ProgramMissingResourceTracker.h"
#include "Integrations/ProgramTrackerInterfaces.h"
Expand Down Expand Up @@ -97,6 +99,9 @@ class ProgramSession : public TrackableProgram{
std::string historical_stats() const;
virtual WallClock last_state_change() const final;

// The last start or stop and who asked for it, or nullopt before the first start.
std::optional<ProgramTransition> last_transition() const{ return m_transitions.last(); }

// Temporary for migration.
StatsTracker* current_stats_tracker(){ return m_current_stats.get(); }
const StatsTracker* historical_stats_tracker() const{ return m_historical_stats.get(); }
Expand All @@ -109,13 +114,17 @@ class ProgramSession : public TrackableProgram{

// Returns empty string on success. Otherwise returns error string.
// Note that these are asynchronous and will return before they are finished.
// `source` is who asks; `request_id` the lab request's id when the lab asks.
// last_transition() reports them.

std::string start_program(); // Returns before the program is fully started up.
std::string stop_program(); // Returns before the program is completely stopped.
// Returns before the program is fully started up.
std::string start_program(ActionSource source, std::optional<std::string> request_id);
// Returns before the program is completely stopped.
std::string stop_program(ActionSource source, std::optional<std::string> request_id);

// Temporary
virtual void async_start() override{ start_program(); }
virtual void async_stop() override{ stop_program(); }
virtual void async_start() override{ start_program(ActionSource::UI, std::nullopt); }
virtual void async_stop() override{ stop_program(ActionSource::UI, std::nullopt); }


protected:
Expand Down Expand Up @@ -145,6 +154,10 @@ class ProgramSession : public TrackableProgram{
// Everything here must be called under the lock.

void set_state(ProgramState state);
// Records the start time and who asked, before the state goes RUNNING.
void mark_started(ActionSource source, std::optional<std::string> request_id);
// The run ended: records who ended it, unless a stop was asked for, and goes STOPPED.
void set_finished();
void push_stats();
void push_error(const std::string& message);
void push_download_error(const std::string& message);
Expand All @@ -168,6 +181,7 @@ class ProgramSession : public TrackableProgram{

std::atomic<WallClock> m_last_state_change;
std::atomic<ProgramState> m_state;
ProgramTransitionLatch m_transitions;

// ProgramMissingResourceTracker m_missing_resource_tracker;

Expand Down
42 changes: 42 additions & 0 deletions SerialPrograms/Source/CommonFramework/ProgramTransition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Program Transition
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "ProgramTransition.h"

namespace PokemonAutomation{


void ProgramTransitionLatch::started(ActionSource source, std::optional<std::string> request_id, WallClock now){
std::lock_guard<Mutex> lg(m_lock);
m_stop_requested = false;
m_error = false;
m_last = ProgramTransition{ProgramAction::START, source, std::move(request_id), now};
}
void ProgramTransitionLatch::stop_requested(ActionSource source, std::optional<std::string> request_id, WallClock now){
std::lock_guard<Mutex> lg(m_lock);
m_stop_requested = true;
m_last = ProgramTransition{ProgramAction::STOP, source, std::move(request_id), now};
}
void ProgramTransitionLatch::error_reported(){
std::lock_guard<Mutex> lg(m_lock);
m_error = true;
}
void ProgramTransitionLatch::finished(WallClock now){
std::lock_guard<Mutex> lg(m_lock);
if (m_stop_requested){
return;
}
const ActionSource source = m_error ? ActionSource::PROGRAM_ERROR : ActionSource::PROGRAM;
m_last = ProgramTransition{ProgramAction::STOP, source, std::nullopt, now};
}
std::optional<ProgramTransition> ProgramTransitionLatch::last() const{
std::lock_guard<Mutex> lg(m_lock);
return m_last;
}



} // namespace PokemonAutomation
74 changes: 74 additions & 0 deletions SerialPrograms/Source/CommonFramework/ProgramTransition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Program Transition
*
* From: https://github.com/PokemonAutomation/
*
* Who started or stopped a program: the Start/Stop button, a lab request
* (with the lab server's id for it), or the program itself when it ends.
* ProgramSession passes the asker in with each start and stop and keeps
* the last transition here, so a reader such as the lab channel never has
* to guess it from the state changes. Free of Qt and of ProgramSession, so
* it is unit tested.
*
*/

#ifndef PokemonAutomation_CommonFramework_ProgramTransition_H
#define PokemonAutomation_CommonFramework_ProgramTransition_H

#include <optional>
#include <string>
#include "Common/Cpp/Time.h"
#include "Common/Cpp/Concurrency/Mutex.h"

namespace PokemonAutomation{


// Whether a transition started or stopped the program.
enum class ProgramAction{
START,
STOP,
};

// Who asked for a start or stop.
enum class ActionSource{
UI, // The Start/Stop button, or an integration (e.g. Discord).
LAB, // A lab channel request.
PROGRAM, // The program finished by itself.
PROGRAM_ERROR, // The program ended with an error.
};

// One start or stop of a program.
struct ProgramTransition{
ProgramAction action;
ActionSource source;
std::optional<std::string> request_id; // The lab request's id, if a lab request asked.
WallClock at;
};


// The last transition of one program. A stop keeps its asker from STOPPING
// through STOPPED: an error or the program's own end after a stop request
// does not overwrite who stopped it. Thread-safe.
class ProgramTransitionLatch{
public:
// The program went RUNNING because `source` asked.
void started(ActionSource source, std::optional<std::string> request_id, WallClock now);
// The program went STOPPING because `source` asked.
void stop_requested(ActionSource source, std::optional<std::string> request_id, WallClock now);
// The running program reported an error.
void error_reported();
// The program went STOPPED.
void finished(WallClock now);

std::optional<ProgramTransition> last() const;

private:
mutable Mutex m_lock;
bool m_stop_requested = false;
bool m_error = false;
std::optional<ProgramTransition> m_last;
};



} // namespace PokemonAutomation
#endif
Loading
Loading