REPL (Read-Eval-Print Loop)
Qmonus SDKでは、Python3でプラグインを記述します。プラグインの実行空間では、Qmonus SDKが提供する組込み関数やクラスが利用できます。また、ユーザ自身が開発した様々なプラグインを組み合わせて利用することもできます。
Qmonus SDKで開発したプラグインの動作確認やコードスニペットを試し撃ちする場合、Interactive Shell と呼ばれるWebターミナルが提供するREPL機能を利用することで簡単に実行することができます。
これは通常のPythonインタプリタが動作する名前空間とは異なり、プラグインの実行空間で動作するREPLとして提供されています。
REPLを使ってPythonコードを実行する
SDKポータルでInteractive Shellメニューをオープンしてください。以下のようなプロンプトが表示されます。
...Connecting To : 127.0.0.1:9099(127.0.0.1:9099)
Qmonus-SDK v20.7LTS-patch20201015 [UG30-hash-dev.usen.ad.jp(192.168.2.197)]
2020-10-26T11:08:58.925184+09:00
Type "copyright", "help", "commands"
>>>
では、早速コードをタイプしてみましょう。Python標準REPLと遜色ない使用感で利用できます。
>>> print("Hello, Qmonus!")↵
... ↵
Hello, Qmonus!
↵
>>>
>>> print([i for i in range(10)])↵
... ↵
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
↵
>>>
Qmonusは、非同期IOループ上で動作しているため、以下のようなasyncioを用いたコードはIOループを開始せずにそのまま実行できることに注意してください。
尚、clock.now()はQmonus SDKが提供している組込関数を利用しています。
>>> for i in range(3):↵
... await asyncio.sleep(1)↵
... print(clock.now().isoformat())↵
... ↵
2020-10-26T11:42:30.350619+09:00
2020-10-26T11:42:31.351279+09:00
2020-10-26T11:42:32.351032+09:00
↵
>>>
REPLを使ってSQL文を実行する
REPLでは、セミコロン;で終了する文をSQLと解釈して実行します。DDL/DML文に関わらず実行できるため注意してください。
以下はMySQL環境の実行例です。
>>> show tables;↵
| Tables_in_axis |
collection
collection_pattern
collection_target
ipam
reflection
sent_metrics
transaction_log
unsent_metrics
>>>
>>> desc ipam;↵
| Field | Type | Null | Key | Default | Extra |
id varchar(128) NO PRI NULL
pool varchar(128) NO NULL
cidr tinyblob NO NULL
assignedAt datetime YES NULL
tag varchar(128) YES NULL
>>>
Interactive Shellで利用できる組込みコマンド
commandsとタイプすると利用可能なコマンドがリストアップされます。
>>> commands↵
Type "atoms" : List ATOM classes
Type "instances" : List ATOM instances
Type "models" : List plugin models
Type "modules" : List plugin modules
Type "functions" : List plugin functions
Type "classes" : List built-in classes
Type "transactions" : List transactions
Type "schedules" : List schedules
Type "daemons" : List daemons
Type "runners" : List SDK runners
Type "systems" : Show system information
Type "cluster" : Show cluster status
Type "traceon" : Set ATOM messaging and Scenario trace mode
Type "traceoff" : Unset ATOM messaging and Scenario trace mode
Type "tracedump" : Dump trace results
Type "profon" : Set Hotspot profiling mode
Type "profoff" : UnSet Hotspot profiling mode
Type "profdump" : Dump profiling results
Type "coverage" : Code coverage measurement mode
Type "subscribe" : Subscribe to internal notification channel
Type "debug" : Subscribe debug channel
Type "proxy" : Proxy another Qmonus node interactive shell
Type "memwatch" : Monitor process memory
Type "illusion" : Set the illusion
Type "workspace" : Set workspace mode
Type "shell" : Set shell mode
Type "history" : Script execution history
>>>
atomsコマンド
プラグインとして定義され、ロードされているATOMクラス名のリストを出力します。
>>> atoms↵
Employee
>>>
instancesコマンド
現在のREPLメモリ空間に存在するATOMインスタンスを出力します。
>>> e = atom.Employee(name="Qmonus太郎", age=30, department="ソフトウェア開発部", email="taro@qmonus.com")↵
... ↵
↵
>>> await e.save()↵
... ↵
↵
>>> select * from Employee;↵
| instance | xid | xname | employeeCode | name | age | department | email |
RW1wbG95ZWU6OWZkZGQyY2ExNzYzMTFlYmIxZWNhY2RlNDgwMDExMjI= NULL NULL 9fddd248176311ebb1ecacde48001122 Qmonus太郎 30 ソフトウェア開発部 taro@qmonus.com
>>> instances↵
Employee(instance='RW1wbG95ZWU6OWZkZGQyY2ExNzYzMTFlYmIxZWNhY2RlNDgwMDExMjI=', xid=None, xname=None, employeeCode='9fddd248176311ebb1ecacde48001122', name='Qmonus太郎', age=30, department='ソフトウェア開発部', email='taro@qmonus.com')
>>>
modelsコマンド
ORマップされているモデル(=RDBMS上のテーブル)をリスト表示します。
>>> models↵
AxisAtom Employee
>>>
modulesコマンド
プラグインとして定義され、ロードされているモジュール名のリストを出力します。
>>> modules↵
tenant
>>>
functionsコマンド
組込関数もしくは、プラグインとして定義され、ロードされている関数名のリストを出力します。
>>> functions↵
and_
dict2xml
flattening
join
magic
multi
nodetodict
not_
or_
queryjoin
rowtodict
scp
select
sendmail
step
waitfor_transaction
where_statement
callout
status_poll
pubsub
qprint
deserialize
get_transactions
rendering
drop_class
slack
neo4j
allocate_counter
task
waitfor
>>>
classesコマンド
組込クラス名のリストを出力します。
>>> classes↵
Counter
HotspotException
HTTPError
MU
Frozen
Cache
Scenario
Daemon
Faker
FakeHttpResponse
Geo
Netconf
CLI
CLIProxy
ipam
SQL
Redis
Model
Plugins
Illusion
SNMPv2
SNMPv3
Booking
Websocket
Test
>>>
transactionsコマンド
トランザクションのリストを出力します。
>>> transactions↵
c78d1784176511eba3faacde48001122 c78d1784176511eba3faacde48001122 Complete
f27238e4176511eba3faacde48001122 f27238e4176511eba3faacde48001122 Aborted
>>>
schedulesコマンド
daemonsコマンド
runnersコマンド
traceonコマンド
traceoffコマンド
tracedumpコマンド
profonコマンド
profoffコマンド
profdumpコマンド
coverageコマンド
debugコマンド
illusionコマンド
proxyコマンド
Interactive Shellは通常接続先のQmonusサーバにターミナル入力をwebsocketで送信します。
Qmonusサーバは全てのターミナル入力を解釈して処理を実行してInteractive Shellに結果を返却します。
proxyコマンドを使うと、Interactive Shellが接続しているQmonusサーバはプロキシサーバとして動作し、受信したターミナル入力をプロキシ先のQmonusサーバに転送し、結果を中継します。以下のようにプロキシ先のwebsocketエンドポイントを指定することで動作します。
>>> proxy("172.16.1.101:9000")
subscribeコマンド
Qmonus内部のpubsubチャネルに接続してメッセージをスヌープする機能です。
主にQmonus SDK本体のトラブルシュートに利用するため、プラグインの開発者が利用することはありません。
systemsコマンド
Interactive Shellが接続しているQmonusサーバのシステム情報を出力します。
各種起動パラメータとRDBMS接続プール、Redis接続プールにおけるプール接続数とフリーの接続数を確認できます。
>>> systems↵
config:
...起動パラメータがリストアップされます
pool:
rdbms:
connection: 2
free: 2
redis:
connection: 7
free: 6
>>>
clusterコマンド
Interactive Shellが接続しているQmonusサーバのクラスタ情報を出力します。
Qmonusのクラスタ管理はデータストアであるRedisと同期しています。
>>> cluster↵
{
"master": "127.0.0.1:6379",
"slaves": []
}
>>>
memwatchコマンド
Interactive Shellが接続しているQmonusサーバのメモリ使用量を定期的に監視して変化量を出力します。デフォルトの監視周期は5秒です。
終了する場合は、memwatch(False)とタイプしてください。開始から終了までの期間でメモリを多く獲得した上位10件が表示されます。
尚、監視間隔は、memwatch(interval=15)のようにinterval引数を指定することで変更できます。
>>> memwatch()↵
Memory watch mode has been ENABLED.
2020-10-27T10:15:29.109738+09:00 128.29MB (3.0sec 128.29MB)
2020-10-27T10:15:34.110675+09:00 128.7MB (5.0sec 0.41MB)
2020-10-27T10:15:39.112357+09:00 128.85MB (5.0sec 0.15MB)
>>> memwatch(False)↵
1) /Users/hash/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pyasn1/codec/ber/encoder.py:832: size=6600 B (+6600 B), count=11 (+11), average=600 B
2) /Users/hash/.pyenv/versions/3.7.4/lib/python3.7/site-packages/tornado/platform/asyncio.py:165: size=4304 B (+4304 B), count=34 (+34), average=127 B
3) /Users/hash/.pyenv/versions/3.7.4/lib/python3.7/site-packages/tornado/http1connection.py:178: size=3840 B (+3840 B), count=6 (+6), average=640 B
4) /Users/hash/dev_374/axis-stable/axis/collection/consumer.py:174: size=3220 B (+3220 B), count=49 (+49), average=66 B
5) /Users/hash/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pyasn1/codec/ber/encoder.py:102: size=2960 B (+2960 B), count=5 (+5), average=592 B
6) /Users/hash/dev_374/axis-stable/axis/boot/boot.py:318: size=2617 B (+2617 B), count=31 (+31), average=84 B
7) /Users/hash/dev_374/axis-stable/axis/reflection/consumer.py:208: size=2256 B (+2256 B), count=3 (+3), average=752 B
8) /Users/hash/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pyasn1/type/base.py:373: size=2184 B (+2184 B), count=13 (+13), average=168 B
9) /Users/hash/.pyenv/versions/3.7.4/lib/python3.7/site-packages/pyasn1/type/base.py:606: size=1848 B (+1848 B), count=11 (+11), average=168 B
10) /Users/hash/.pyenv/versions/3.7.4/lib/python3.7/asyncio/tasks.py:608: size=1840 B (+1840 B), count=17 (+17), average=108 B
Memory watch mode has been DISABLED.
>>>
workspaceコマンド
ローカルディスクモードのワークスペースでshellコマンドを実行できるモードです。ワークスペースについてはDocs » リファレンス » ワークスペースを参照してください。
ローカルディスクモードでプラグインを開発するとQmonus SDKが稼動しているホストのローカルディスクにプラグイン定義ファイルが格納されます。これらをgithubなどのリポジトリにプッシュするために利用することができます。引数には、ワークスペース名を与えます。
>>> workspace("handson")↵
'handson' workspace mode '/Users/hash/hands-on_plugins/axis/handson'
>>> pwd↵
/Users/hash/hands-on_plugins/axis/handson
>>> ls -la↵
total 0
drwxr-xr-x 10 hash staff 320 3 19 2020 .
drwxr-xr-x 4 hash staff 128 3 19 2020 ..
-rw-r--r-- 1 hash staff 0 3 19 2020 .gitkeep
drwxr-xr-x 3 hash staff 96 3 23 2020 classes
drwxr-xr-x 8 hash staff 256 3 23 2020 fakers
drwxr-xr-x 3 hash staff 96 3 19 2020 functions
drwxr-xr-x 9 hash staff 288 3 19 2020 illusions
drwxr-xr-x 3 hash staff 96 3 23 2020 scenarios
drwxr-xr-x 14 hash staff 448 3 19 2020 testcases
drwxr-xr-x 3 hash staff 96 3 19 2020 testsuites
>>> git diff↵
>>>
>>> exit↵
workspace mode disabled
shellコマンド
Qmonus SDKの起動ディレクトリでshellコマンドを実行できるモードです。数ギガバイトクラスのバイナリファイルのアップロード機能を提供していますが、アップロードされたファイルは、起動ディレクトリのupload配下に格納されますのでshellコマンドで確認することができます。
>>> shell()↵
shell mode enabled
>>> ls -la upload↵
total 4225280
drwxr-xr-x 3 hash staff 96 1 14 11:38 .
drwxr-xr-x 22 hash staff 704 1 18 10:51 ..
-rw-r--r-- 1 hash staff 2147483648 1 14 11:38 dummy.bin
>>> exit↵
shell mode disabled
>>>
historyコマンド
REPLの実行履歴を出力します。過去30件までの正常実行できたコードブロック履歴について履歴番号とコード先頭行のセットで出力します。
エクスクラメーションマークと履歴番号をタイプすると履歴から再実行することができます。
>>> history↵
1: 'for i in range(3):'
2: print([i for i in range(10)])
3: print("Hello, Qmonus!")
>>> !3↵
... ↵
Hello, Qmonus!
↵
>>>
尚、REPLから24時間以上コードを実行しなかった場合、履歴は消去されることに注意してください。