このページは機械翻訳したものです。
例 7.3 レポートおよび拡張オブジェクトを含む MySQL Shell プラグイン
この例では、現在実行中のプロセスを表示する関数 show_processes()
と、指定された ID を持つプロセスを強制終了する関数 kill_process()
を定義します。show_processes()
は MySQL Shell レポートになり、kill_process()
は拡張オブジェクトによって提供される関数になります。
このコードは、shell.register_report()
メソッドを使用して、show_processes()
を MySQL Shell レポート proc
として登録します。 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
は、別のプラグインによってまだ登録されていない場合は作成および登録され、それ以外の場合は既存のオブジェクトが使用されます。
Press CTRL+C to copy# 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()))
ここで、ユーザーは MySQL Shell \show
コマンドを使用してレポート proc
を実行し、ext.process.kill()
関数を使用してリストされているプロセスのいずれかを停止します:
Press CTRL+C to copymysql-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 | +----+-----------------+-----------------+---------+----------------------------------------------------------------------------------+