Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to make plugin unsubscribe from FairMQ at right time #507

Merged
merged 1 commit into from
Nov 10, 2023
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
5 changes: 4 additions & 1 deletion occ/plugin/OccFMQCommon.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ std::tuple<OccLite::nopb::TransitionResponse, ::grpc::Status> doTransition(fair:

m_pluginServices->SubscribeToDeviceStateChange(id, onDeviceStateChange);
DEFER({
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
if ( !newStates.empty() && newStates.back() != "EXITING") {
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
}
});

try {
Expand Down Expand Up @@ -220,6 +222,7 @@ std::tuple<OccLite::nopb::TransitionResponse, ::grpc::Status> doTransition(fair:
}

if (newStates.back() == "EXITING") {
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
m_pluginServices->ReleaseDeviceControl(FMQ_CONTROLLER_NAME);
OLOG(debug) << "releasing device control";
}
Expand Down
6 changes: 5 additions & 1 deletion occ/plugin/OccLiteServer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ OccLite::Service::EventStream(::grpc::ServerContext* context, const OccLite::nop
std::mutex writer_mu;
std::condition_variable finished;
std::mutex finished_mu;
std::string last_known_state;

auto onDeviceStateChange = [&](fair::mq::PluginServices::DeviceState reachedState) {
std::lock_guard<std::mutex> lock(writer_mu);
auto state = fair::mq::PluginServices::ToStr(reachedState);
last_known_state = state;

OLOG(debug) << "[EventStream] new state: " << state;

Expand All @@ -147,7 +149,9 @@ OccLite::Service::EventStream(::grpc::ServerContext* context, const OccLite::nop

m_pluginServices->SubscribeToDeviceStateChange(id, onDeviceStateChange);
DEFER({
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
if (last_known_state == "EXITING") {
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
}
});

{
Expand Down
12 changes: 10 additions & 2 deletions occ/plugin/OccPluginServer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ OccPluginServer::EventStream(grpc::ServerContext* context,
std::mutex writer_mu;
std::condition_variable finished;
std::mutex finished_mu;
std::string last_known_state;

auto onDeviceStateChange = [&](fair::mq::PluginServices::DeviceState reachedState) {
std::lock_guard<std::mutex> lock(writer_mu);
auto state = fair::mq::PluginServices::ToStr(reachedState);
last_known_state = state

OLOG(debug) << "[EventStream] new state: " << state;

Expand All @@ -83,7 +85,9 @@ OccPluginServer::EventStream(grpc::ServerContext* context,

m_pluginServices->SubscribeToDeviceStateChange(id, onDeviceStateChange);
DEFER({
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
if (last_known_state == "EXITING") {
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
}
});

{
Expand All @@ -106,10 +110,12 @@ OccPluginServer::StateStream(grpc::ServerContext* context,
std::mutex writer_mu;
std::condition_variable finished;
std::mutex finished_mu;
std::string last_known_state;

auto onDeviceStateChange = [&](fair::mq::PluginServices::DeviceState reachedState) {
std::lock_guard<std::mutex> lock(writer_mu);
auto state = fair::mq::PluginServices::ToStr(reachedState);
last_known_state = state
pb::StateType sType = isIntermediateFMQState(state) ? pb::STATE_INTERMEDIATE : pb::STATE_STABLE;

pb::StateStreamReply response;
Expand All @@ -132,7 +138,9 @@ OccPluginServer::StateStream(grpc::ServerContext* context,

m_pluginServices->SubscribeToDeviceStateChange(id, onDeviceStateChange);
DEFER({
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
if (last_known_state == "EXITING") {
m_pluginServices->UnsubscribeFromDeviceStateChange(id);
}
});

{
Expand Down
Loading