How it Works#

Overview#

run_soap_command() 是 SDK 的核心函数, 它可以将 GM 命令从任何地方, 例如 EC2, ECS, Lambda, 或是本地电脑, 发送到游戏服务器上运行, 并获得返回的结果. 这个函数的底层实现是 build 好 acoresoapagent gm … 所需要的 CLI 命令, 然后使用 aws_ssm_run_command 这个库远程执行这个 CLI. 根据你的参数, 你可以选 SOAP response 打印到 stdout 或是写入到 S3. 注意, 这个函数仅仅是开始一个异步执行, 它不会自动等待执行完成, 而是会返回一个 SoapResponseAsyncGetter 对象. 这个对象有一个 get() 方法, 你可以调用这个方法来等待执行完成并获得对应的 SOAPResponse 的列表.

Usage Example#

test_request 这个例子展示了如何使用 run_soap_command 函数.

test_request.py
 1# -*- coding: utf-8 -*-
 2
 3import boto3
 4from acore_server_metadata.api import Server
 5from acore_soap_remote.request import run_soap_command
 6
 7
 8def test():
 9    boto_ses = boto3.session.Session(profile_name="bmt_app_dev_us_east_1")
10    ec2_client = boto_ses.client("ec2")
11    rds_client = boto_ses.client("rds")
12    s3_client = boto_ses.client("s3")
13    ssm_client = boto_ses.client("ssm")
14
15    server_id = "sbx-blue"
16    server = Server(id=server_id)
17    server.refresh(ec2_client=ec2_client, rds_client=rds_client)
18    if server.is_running() is False:
19        raise SystemError(f"EC2 {server_id!r} is not running")
20    ec2_instance_id = server.ec2_inst.id
21
22    # Run GM commands in sequence, and read the response data from stdout
23    soap_response_async_getter = run_soap_command(
24        gm_commands=[
25            ".account delete test1",
26            ".account create test1 1234",
27            ".account set gmlevel test1 3 -1",
28            ".account set password test1 123456 123456",
29            ".account delete test1",
30        ],
31        ec2_instance_id=ec2_instance_id,
32        ssm_client=ssm_client,
33        s3_client=s3_client,
34    )
35    soap_response_list = soap_response_async_getter.get()
36    for soap_response in soap_response_list:
37        print("=" * 80)
38        print(f"{soap_response.succeeded = }, {soap_response.message = }")
39
40    # Run GM commands in sequence, and read the response data from S3
41    soap_response_async_getter = run_soap_command(
42        gm_commands=[
43            ".account delete test1",
44            ".account create test1 1234",
45            ".account set gmlevel test1 3 -1",
46            ".account set password test1 123456 123456",
47            ".account delete test1",
48        ],
49        ec2_instance_id=ec2_instance_id,
50        ssm_client=ssm_client,
51        input_s3uri="s3://bmt-app-dev-us-east-1-data/projects/acore_soap_remote/int_test/input.json",
52        output_s3uri="s3://bmt-app-dev-us-east-1-data/projects/acore_soap_remote/int_test/output.json",
53        s3_client=s3_client,
54    )
55    soap_response_list = soap_response_async_getter.get()
56    for soap_response in soap_response_list:
57        print("=" * 80)
58        print(f"{soap_response.succeeded = }, {soap_response.message = }")
59
60
61if __name__ == "__main__":
62    from acore_soap_remote.tests import run_cov_test
63
64    run_cov_test(__file__, "acore_soap_remote.request", preview=False)