Skip to content

Commit a341176

Browse files
matthiasrichterktf
authored andcommitted
Adding customizable hook to be executed on workflow termination (#2196)
This defines a function callback `OnWorkflowTerminationHook` with signature `void (const char*)` and allows to set the hook using the `customize` mechanism of DPL workflows. The callback hook is invoked just before processes terminate. The hook is invoked for all childs and the main driver, the id string is passed as parameter to customize the callback action. For the main driver, the nullptr is passed.
1 parent 6025607 commit a341176

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
#ifndef CUSTOMWORKFLOWTERMINATIONHOOK_H
11+
#define CUSTOMWORKFLOWTERMINATIONHOOK_H
12+
13+
namespace o2
14+
{
15+
namespace framework
16+
{
17+
18+
/// A callback definition for a hook to be invoked when processes terminate
19+
///
20+
/// The parameter is the nullptr if the process is the main driver, for all
21+
/// child processes, the id string is passed. This allows to customize the
22+
/// hook depending on the process.
23+
/// Note that the callback hook is invoked for every process, i.e. main driver
24+
/// and all childs.
25+
///
26+
/// \par Usage:
27+
/// The customize the hook, add a function with the following signature before
28+
/// including heder file runDataProcessing.h:
29+
///
30+
/// void customize(o2::framework::OnWorkflowTerminationHook& hook)
31+
/// {
32+
/// hook = [](const char* idstring){
33+
/// if (idstring == nullptr) {
34+
/// std::cout << "hook" << std::endl;
35+
/// } else {
36+
/// std::cout << "child process " << idstring << " terminating" << std::endl;
37+
/// }
38+
/// };
39+
/// }
40+
/// #include "Framework/runDataProcessing.h"
41+
using OnWorkflowTerminationHook = std::function<void(const char*)>;
42+
43+
} // namespace framework
44+
} // namespace o2
45+
46+
#endif

Framework/Core/include/Framework/runDataProcessing.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Framework/WorkflowSpec.h"
1818
#include "Framework/ConfigContext.h"
1919
#include "Framework/BoostOptionsRetriever.h"
20+
#include "Framework/CustomWorkflowTerminationHook.h"
2021

2122
#include <boost/program_options/options_description.hpp>
2223
#include <boost/program_options/variables_map.hpp>
@@ -67,6 +68,10 @@ o2::framework::WorkflowSpec defineDataProcessing(o2::framework::ConfigContext co
6768
void defaultConfiguration(std::vector<o2::framework::ChannelConfigurationPolicy>& channelPolicies) {}
6869
void defaultConfiguration(std::vector<o2::framework::ConfigParamSpec> &globalWorkflowOptions) {}
6970
void defaultConfiguration(std::vector<o2::framework::CompletionPolicy> &completionPolicies) {}
71+
void defaultConfiguration(o2::framework::OnWorkflowTerminationHook& hook)
72+
{
73+
hook = [](const char*) {};
74+
}
7075

7176
struct UserCustomizationsHelper {
7277
template <typename T>
@@ -124,6 +129,16 @@ int main(int argc, char** argv)
124129
LOG(ERROR) << "Unknown error while setting up workflow.";
125130
}
126131

132+
char* idstring = nullptr;
133+
for (int argi = 0; argi < argc; argi++) {
134+
if (strcmp(argv[argi], "--id") == 0 && argi + 1 < argc) {
135+
idstring = argv[argi + 1];
136+
break;
137+
}
138+
}
139+
o2::framework::OnWorkflowTerminationHook onWorkflowTerminationHook;
140+
UserCustomizationsHelper::userDefinedCustomization(onWorkflowTerminationHook, 0);
141+
onWorkflowTerminationHook(idstring);
127142
LOG(INFO) << "Process " << getpid() << " is exiting.";
128143
return result;
129144
}

0 commit comments

Comments
 (0)