]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/PyModuleRunner.h
import ceph 14.2.5
[ceph.git] / 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 "common/LogClient.h"
19 #include "mgr/Gil.h"
20
21 #include "PyModule.h"
22
23 /**
24 * Implement the pattern of calling serve() on a module in a thread,
25 * until shutdown() is called.
26 */
27 class PyModuleRunner
28 {
29 public:
30 // Info about the module we're going to run
31 PyModuleRef py_module;
32
33 protected:
34 // Populated by descendent class
35 PyObject *pClassInstance = nullptr;
36
37 LogChannelRef clog;
38
39 class PyModuleRunnerThread : public Thread
40 {
41 PyModuleRunner *mod;
42
43 public:
44 explicit PyModuleRunnerThread(PyModuleRunner *mod_)
45 : mod(mod_) {}
46
47 void *entry() override;
48 };
49
50 std::string thread_name;
51
52 public:
53 int serve();
54 void shutdown();
55 void log(int level, const std::string &record);
56
57 const char *get_thread_name() const
58 {
59 return thread_name.c_str();
60 }
61
62 PyModuleRunner(
63 const PyModuleRef &py_module_,
64 LogChannelRef clog_)
65 :
66 py_module(py_module_),
67 clog(clog_),
68 thread(this)
69 {
70 // Shortened name for use as thread name, because thread names
71 // required to be <16 chars
72 thread_name = py_module->get_name().substr(0, 15);
73
74 ceph_assert(py_module != nullptr);
75 }
76
77 ~PyModuleRunner();
78
79 PyModuleRunnerThread thread;
80
81 std::string const &get_name() const { return py_module->get_name(); }
82 };
83
84