示例 10.3 包含报告和扩展对象的 MySQL Shell 插件
这个例子定义了一个函数
show_processes()
来显示当前正在运行的进程,以及一个函数
kill_process()
来杀死具有指定 ID 的进程。show_processes()
将成为 MySQL Shell 报告,并将kill_process()
成为扩展对象提供的功能。
该代码使用该
方法注册show_processes()
为 MySQL Shell 报告。要注册
为
,代码会检查全局对象和扩展对象是否已经存在,如果不存在则创建并注册它们。然后将该
函数作为成员添加到
扩展对象。
proc
shell.register_report()
kill_process()
ext.process.kill()
ext
process
kill_process()
process
插件代码保存为文件
~/.mysqlsh/plugins/ext/process/init.py
. 启动时,MySQL Shell 会遍历plugins 文件夹中的文件夹,找到这个init.py
文件,然后执行代码。报告proc
和函数kill()
已注册并可供使用。如果全局对象ext
和扩展对象process
尚未被另一个插件注册,则创建并注册它们,否则使用现有对象。
# Define a show_processes function that generates a MySQL Shell report
def show_processes(session, args, options):
query = "SELECT ID, USER, HOST, COMMAND, INFO FROM INFORMATION_SCHEMA.PROCESSLIST"
if (options.has_key('command')):
query += " WHERE COMMAND = '%s'" % options['command']
result = session.sql(query).execute();
report = []
if (result.has_data()):
report = [result.get_column_names()]
for row in result.fetch_all():
report.append(list(row))
return {"report": report}
# Define a kill_process function that will be exposed by the global object 'ext'
def kill_process(session, id):
result = session.sql("KILL CONNECTION %d" % id).execute()
# Register the show_processes function as a MySQL Shell report
shell.register_report("proc", "list", show_processes, {"brief":"Lists the processes on the target server.",
"options": [{
"name": "command",
"shortcut": "c",
"brief": "Use this option to list processes over specific commands."
}]})
# Register the kill_process function as ext.process.kill()
# Check if global object 'ext' has already been registered
if 'ext' in globals():
global_obj = ext
else:
# Otherwise register new global object named 'ext'
global_obj = shell.create_extension_object()
shell.register_global("ext", global_obj,
{"brief":"MySQL Shell extension plugins."})
# Add the 'process' extension object as a member of the 'ext' global object
try:
plugin_obj = global_obj.process
except IndexError:
# If the 'process' extension object has not been registered yet, do it now
plugin_obj = shell.create_extension_object()
shell.add_extension_object_member(global_obj, "process", plugin_obj,
{"brief": "Utility object for process operations."})
# Add the kill_process function to the 'process' extension object as member 'kill'
try:
shell.add_extension_object_member(plugin_obj, "kill", kill_process, {"brief": "Kills the process with the given ID.",
"parameters": [
{
"name":"session",
"type":"object",
"class":"Session",
"brief": "The session to be used on the operation."
},
{
"name":"id",
"type":"integer",
"brief": "The ID of the process to be killed."
}
]
})
except Exception as e:
shell.log("ERROR", "Failed to register ext.process.kill ({0}).".
format(str(e).rstrip()))
在这里,用户proc
使用 MySQL Shell\show
命令运行报告,然后使用
ext.process.kill()
函数停止列出的进程之一:
mysql-py> \show proc
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+
| ID | USER | HOST | COMMAND | INFO |
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+
| 66 | root | localhost:53998 | Query | PLUGIN: SELECT ID, USER, HOST, COMMAND, INFO FROM INFORMATION_SCHEMA.PROCESSLIST |
| 67 | root | localhost:34022 | Sleep | NULL |
| 4 | event_scheduler | localhost | Daemon | NULL |
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+
mysql-py> ext.process.kill(session, 67)
mysql-py> \show proc
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+
| ID | USER | HOST | COMMAND | INFO |
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+
| 66 | root | localhost:53998 | Query | PLUGIN: SELECT ID, USER, HOST, COMMAND, INFO FROM INFORMATION_SCHEMA.PROCESSLIST |
| 4 | event_scheduler | localhost | Daemon | NULL |
+----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+