Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / mgr / PyModuleRunner.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2016 John Spray <john.spray@redhat.com>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  */
13
14
15 #pragma once
16
17 #include "common/Thread.h"
18 #include "mgr/Gil.h"
19
20 /**
21  * Implement the pattern of calling serve() on a module in a thread,
22  * until shutdown() is called.
23  */
24 class PyModuleRunner
25 {
26 protected:
27   const std::string module_name;
28
29   // Passed in by whoever loaded our python module and looked up
30   // the symbols in it.
31   PyObject *pClass = nullptr;
32
33   // Passed in by whoever created our subinterpreter for us
34   SafeThreadState pMyThreadState = nullptr;
35
36   // Populated when we construct our instance of pClass in load()
37   PyObject *pClassInstance = nullptr;
38
39   class PyModuleRunnerThread : public Thread
40   {
41     PyModuleRunner *mod;
42
43   public:
44     PyModuleRunnerThread(PyModuleRunner *mod_)
45       : mod(mod_) {}
46
47     void *entry() override;
48   };
49
50 public:
51   int serve();
52   void shutdown();
53   void log(int level, const std::string &record);
54
55   PyModuleRunner(
56       const std::string &module_name_,
57       PyObject *pClass_,
58       const SafeThreadState &pMyThreadState_)
59     : 
60       module_name(module_name_),
61       pClass(pClass_), pMyThreadState(pMyThreadState_),
62       thread(this)
63   {
64     assert(pClass != nullptr);
65     assert(pMyThreadState.ts != nullptr);
66     assert(!module_name.empty());
67   }
68
69   ~PyModuleRunner();
70
71   PyModuleRunnerThread thread;
72
73   std::string const &get_name() const { return module_name; }
74 };
75
76