winss
service_process.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 Morgan Stanley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIB_WINSS_SVSCAN_SERVICE_PROCESS_HPP_
18 #define LIB_WINSS_SVSCAN_SERVICE_PROCESS_HPP_
19 
20 #include <filesystem>
21 #include <utility>
22 #include <string>
23 #include "easylogging/easylogging++.hpp"
24 #include "../handle_wrapper.hpp"
25 #include "../process.hpp"
26 #include "winss/winss.hpp"
27 
28 namespace fs = std::experimental::filesystem;
29 
30 namespace winss {
34 struct ServicePipes {
37 };
38 
48 template<typename TProcess>
50  protected:
51  fs::path service_dir;
52  TProcess proc;
54  public:
56  static constexpr const char kSuperviseExe[16] = "winss-supervise";
57 
62 
68  explicit ServiceProcessTmpl(fs::path service_dir) :
69  service_dir(std::move(service_dir)) {}
70 
71  ServiceProcessTmpl(const ServiceProcessTmpl&) = delete;
79  service_dir(std::move(p.service_dir)),
80  proc(std::move(p.proc)) {}
81 
87  virtual const fs::path& GetServiceDir() const {
88  return service_dir;
89  }
90 
96  virtual bool IsCreated() const {
97  return proc.IsCreated();
98  }
99 
106  virtual void Start(const ServicePipes& pipes, bool consumer) {
107  if (proc.IsCreated()) {
108  VLOG(3)
109  << "Process for service dir "
110  << service_dir
111  << " is already running";
112  return;
113  }
114 
115  VLOG(3) << "Starting service " << service_dir;
116 
117  std::string cmd = kSuperviseExe + std::string(SUFFIX) + ".exe"
118  " \"" + service_dir.string() + "\"";
119 
120  winss::ProcessParams params{ cmd, true };
121 
122  if (consumer) {
123  params.stdin_pipe = pipes.stdin_pipe;
124  } else {
125  params.stdout_pipe = pipes.stdout_pipe;
126  params.stderr_pipe = pipes.stdout_pipe;
127  }
128 
129  proc.Create(params);
130  }
131 
135  virtual void Close() {
136  if (proc.IsCreated()) {
137  proc.SendBreak();
138  proc.Close();
139  }
140  }
141 
143  ServiceProcessTmpl& operator=(const ServiceProcessTmpl&) = delete;
144 
152  service_dir = std::move(p.service_dir);
153  proc = std::move(p.proc);
154  return *this;
155  }
156 
161  proc.Close();
162  }
163 };
164 
169 } // namespace winss
170 
171 #endif // LIB_WINSS_SVSCAN_SERVICE_PROCESS_HPP_
fs::path service_dir
The service directory.
Definition: service_process.hpp:51
ServiceProcessTmpl(ServiceProcessTmpl &&p)
Creates a new service process and moves it from an old one.
Definition: service_process.hpp:78
A wrapper for a Windows HANDLE.
Definition: handle_wrapper.hpp:39
Parameters to start a Windows process.
Definition: process.hpp:29
TProcess proc
The supervisor process.
Definition: service_process.hpp:52
ServiceProcessTmpl()
The default service process template constructor.
Definition: service_process.hpp:61
virtual const fs::path & GetServiceDir() const
Gets the path of the service directory.
Definition: service_process.hpp:87
virtual void Close()
Closes the service process.
Definition: service_process.hpp:135
Definition: case_ignore.hpp:23
winss::HandleWrapper stdin_pipe
Definition: service_process.hpp:35
virtual bool IsCreated() const
Gets the service process is created.
Definition: service_process.hpp:96
ServiceProcessTmpl & operator=(ServiceProcessTmpl &&p)
Moves the service process object to this object.
Definition: service_process.hpp:151
virtual void Start(const ServicePipes &pipes, bool consumer)
Starts the service process.
Definition: service_process.hpp:106
virtual ~ServiceProcessTmpl()
Service process destructor which will close the process.
Definition: service_process.hpp:160
A template for a service process.
Definition: service_process.hpp:49
winss::HandleWrapper stdin_pipe
STDIN pipe.
Definition: process.hpp:35
ServiceProcessTmpl(fs::path service_dir)
Initializes the service process with the service directory.
Definition: service_process.hpp:68
winss::HandleWrapper stdout_pipe
Definition: service_process.hpp:36
ServiceProcessTmpl< winss::Process > ServiceProcess
Concrete service process implementation.
Definition: service_process.hpp:168
#define SUFFIX
Definition: winss.hpp:23
Holds the STDIN and STDOUT pipes for redirecting.
Definition: service_process.hpp:34