diff --git a/examples/cloud_deployment/gcp/deploy.py b/examples/cloud_deployment/gcp/deploy.py index eca66f74..39dba896 100644 --- a/examples/cloud_deployment/gcp/deploy.py +++ b/examples/cloud_deployment/gcp/deploy.py @@ -1,13 +1,24 @@ import getpass +import typer -from prediction_market_agent_tooling.deploy.agent_example import DeployableCoinFlipAgent +from prediction_market_agent_tooling.deploy.agent_example import ( + DeployableAgent, + DeployableCoinFlipAgent, + DeployableAlwaysRaiseAgent, +) from prediction_market_agent_tooling.markets.markets import MarketType -if __name__ == "__main__": - agent = DeployableCoinFlipAgent() + +def main( + agent_name: str, cron_schedule: str = "0 */2 * * *", branch: str = "main" +) -> None: + agent: DeployableAgent = { + "coin_flip": DeployableCoinFlipAgent, + "always_raise": DeployableAlwaysRaiseAgent, + }[agent_name]() agent.deploy_gcp( # TODO: Switch to main. - repository="git+https://github.com/gnosis/prediction-market-agent-tooling.git@peter/refactor-deployment", + repository="git+https://github.com/gnosis/prediction-market-agent-tooling.git@{branch}", market_type=MarketType.MANIFOLD, labels={ "owner": getpass.getuser() @@ -18,5 +29,9 @@ "MANIFOLD_API_KEY": f"JUNG_PERSONAL_GMAIL_MANIFOLD_API_KEY:latest" }, # Must be in the format "env_var_in_container => secret_name:version", you can create secrets using `gcloud secrets create --labels owner= ` command. memory=256, - cron_schedule="0 */2 * * *", + cron_schedule=cron_schedule, ) + + +if __name__ == "__main__": + typer.run(main) diff --git a/prediction_market_agent_tooling/deploy/agent_example.py b/prediction_market_agent_tooling/deploy/agent_example.py index 8d2e822d..393c309e 100644 --- a/prediction_market_agent_tooling/deploy/agent_example.py +++ b/prediction_market_agent_tooling/deploy/agent_example.py @@ -6,9 +6,12 @@ class DeployableCoinFlipAgent(DeployableAgent): def pick_markets(self, markets: list[AgentMarket]) -> list[AgentMarket]: - if len(markets) > 1: - return random.sample(markets, 1) - return markets + return random.sample(markets, 1) def answer_binary_market(self, market: AgentMarket) -> bool: return random.choice([True, False]) + + +class DeployableAlwaysRaiseAgent(DeployableAgent): + def answer_binary_market(self, market: AgentMarket) -> bool: + raise RuntimeError("I always raise!") diff --git a/prediction_market_agent_tooling/deploy/gcp/utils.py b/prediction_market_agent_tooling/deploy/gcp/utils.py index 9b331fa8..0f557982 100644 --- a/prediction_market_agent_tooling/deploy/gcp/utils.py +++ b/prediction_market_agent_tooling/deploy/gcp/utils.py @@ -15,6 +15,8 @@ def gcloud_deploy_cmd( env_vars: dict[str, str] | None, secrets: dict[str, str] | None, memory: int, # in MB + timeout: int = 180, + retry_on_failure: bool = False, ) -> str: cmd = ( f"gcloud functions deploy {gcp_function_name} " @@ -26,7 +28,14 @@ def gcloud_deploy_cmd( f"--entry-point {entry_point} " f"--memory {memory}MB " f"--no-allow-unauthenticated " + f"--timeout {timeout}s " + # Explicitly set no concurrency, min instances to 0 (agent is executed only once in a while) and max instances to 1 (parallel agents aren't allowed). + "--concurrency 1 " + "--min-instances 0 " + "--max-instances 1 " ) + if retry_on_failure: + cmd += "--retry " if labels: for k, v in labels.items(): cmd += f"--update-labels {k}={v} "