Skip to content

Commit

Permalink
Fix retries on GCP deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii committed Feb 19, 2024
1 parent b04007c commit d0615e2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
25 changes: 20 additions & 5 deletions examples/cloud_deployment/gcp/deploy.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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=<your-name> <secret-name>` command.
memory=256,
cron_schedule="0 */2 * * *",
cron_schedule=cron_schedule,
)


if __name__ == "__main__":
typer.run(main)
9 changes: 6 additions & 3 deletions prediction_market_agent_tooling/deploy/agent_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
9 changes: 9 additions & 0 deletions prediction_market_agent_tooling/deploy/gcp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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} "
Expand All @@ -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} "
Expand Down

0 comments on commit d0615e2

Please sign in to comment.