summaryrefslogtreecommitdiffstats
path: root/src/mgr/PyModuleRunner.h
blob: 88d9f755a1d705fb426222003f4e638e94e4d154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2016 John Spray <john.spray@redhat.com>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 */


#pragma once

#include "common/Thread.h"
#include "common/LogClient.h"
#include "mgr/Gil.h"

#include "PyModule.h"

/**
 * Implement the pattern of calling serve() on a module in a thread,
 * until shutdown() is called.
 */
class PyModuleRunner
{
public:
  // Info about the module we're going to run
  PyModuleRef py_module;

protected:
  // Populated by descendent class
  PyObject *pClassInstance = nullptr;

  LogChannelRef clog;

  class PyModuleRunnerThread : public Thread
  {
    PyModuleRunner *mod;

  public:
    explicit PyModuleRunnerThread(PyModuleRunner *mod_)
      : mod(mod_) {}

    void *entry() override;
  };

  bool is_dead() const { return dead; }

  std::string thread_name;

public:
  int serve();
  void shutdown();
  void log(const std::string &record);

  const char *get_thread_name() const
  {
    return thread_name.c_str();
  }

  PyModuleRunner(
      const PyModuleRef &py_module_,
      LogChannelRef clog_)
    : 
      py_module(py_module_),
      clog(clog_),
      thread(this)
  {
    // Shortened name for use as thread name, because thread names
    // required to be <16 chars
    thread_name = py_module->get_name().substr(0, 15);

    ceph_assert(py_module != nullptr);
  }

  ~PyModuleRunner();

  PyModuleRunnerThread thread;

  std::string const &get_name() const { return py_module->get_name(); }

private:
  bool dead = false;
};