Skip to content

Commit

Permalink
Fix auth issue with function scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
evangriffiths committed Feb 8, 2024
1 parent c737ded commit 756a796
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
10 changes: 8 additions & 2 deletions prediction_market_agent/deploy/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from prediction_market_agent.data_models.market_data_models import AgentMarket
from prediction_market_agent.deploy.utils import (
export_requirements_from_toml,
gcloud_create_topic_cmd,
gcloud_delete_function_cmd,
gcloud_delete_topic_cmd,
gcloud_deploy_cmd,
gcloud_schedule_cmd,
get_gcloud_function_uri,
Expand Down Expand Up @@ -113,6 +115,10 @@ def deploy_to_gcp(
else:
shutil.copy(requirements_file, f"{tempdir}/requirements.txt")

# Create the topic used to trigger the function. Note we use the
# convention that the topic name is the same as the function name
subprocess.run(gcloud_create_topic_cmd(gcp_fname), shell=True)

# Deploy the function
cmd = gcloud_deploy_cmd(
gcp_function_name=gcp_fname,
Expand Down Expand Up @@ -143,5 +149,5 @@ def run_deployed_gcp_function(function_name: str) -> requests.Response:


def remove_deployed_gcp_function(function_name: str) -> None:
cmd = gcloud_delete_function_cmd(function_name)
subprocess.run(cmd, shell=True)
subprocess.run(gcloud_delete_function_cmd(function_name), shell=True)
subprocess.run(gcloud_delete_topic_cmd(function_name), shell=True)
18 changes: 13 additions & 5 deletions prediction_market_agent/deploy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def gcloud_deploy_cmd(
cmd = (
f"gcloud functions deploy {gcp_function_name} "
f"--runtime {get_gcloud_python_runtime_str()} "
f"--trigger-http "
f"--trigger-topic {gcp_function_name} "
f"--gen2 "
f"--region {get_gcloud_region()} "
f"--source {source} "
Expand All @@ -49,18 +49,26 @@ def gcloud_deploy_cmd(

def gcloud_schedule_cmd(function_name: str, cron_schedule: str) -> str:
return (
f"gcloud scheduler jobs create http {function_name} "
f"gcloud scheduler jobs create pubsub {function_name} "
f"--schedule '{cron_schedule}' "
f"--uri {get_gcloud_function_uri(function_name)} "
f"--http-method POST "
f"--location {get_gcloud_region()}"
f"--topic {function_name} "
f"--location {get_gcloud_region()} "
"--message-body '{}' "
)


def gcloud_delete_function_cmd(fname: str) -> str:
return f"gcloud functions delete {fname} --region={get_gcloud_region()} --quiet"


def gcloud_create_topic_cmd(topic_name: str) -> str:
return f"gcloud pubsub topics create {topic_name}"


def gcloud_delete_topic_cmd(topic_name: str) -> str:
return f"gcloud pubsub topics delete {topic_name}"


def get_gcloud_project_id() -> str:
return (
subprocess.run(
Expand Down

0 comments on commit 756a796

Please sign in to comment.