-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.py
51 lines (40 loc) · 1.66 KB
/
poll.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import time
import os
import subprocess
import argparse
from google.cloud import aiplatform
def get_job_state(job_name):
job = aiplatform.CustomJob.get(resource_name=job_name)
return job.state
def play_sound():
sound_file = '/usr/share/sounds/ubuntu/stereo/system-ready.ogg'
if os.path.exists(sound_file):
try:
subprocess.run(['paplay', sound_file], check=True)
except subprocess.CalledProcessError:
print("Error playing sound. Make sure 'paplay' is installed.")
else:
print("✅ Job is live. see - 🚀 -- https://console.cloud.google.com/vertex-ai/training/custom-jobs")
def poll_job_state(job_name):
print(f"Starting to poll job: {job_name}")
while True:
current_state = get_job_state(job_name)
print(f"Current job state: {current_state}")
if current_state == 3:
print(f"Job state changed to: {current_state}")
play_sound()
break
time.sleep(60) # Wait for 60 seconds before polling again
def main():
parser = argparse.ArgumentParser(description="Poll Vertex AI Custom Job State")
parser.add_argument("job_name", help="Full resource name of the Vertex AI Custom Job")
args = parser.parse_args()
# Get the project ID from environment variable
project_id = os.environ.get('GCP_PROJECT')
if not project_id:
raise ValueError("GCP_PROJECT environment variable is not set. Please set it to your Google Cloud project ID.")
# Initialize the AI Platform client
aiplatform.init(project=project_id, location='us-central1')
poll_job_state(args.job_name)
if __name__ == "__main__":
main()