Skip to content

Commit 3ec7ed2

Browse files
committed
add wait_for_job_completion
1 parent 5f104c8 commit 3ec7ed2

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ create_job_info = client.create_job(job_name='your-job-name', workflow_id='your-
3333
job_id = create_job_info['id']
3434
print('Job Created:', job_id)
3535

36+
# Wait for job to complete
37+
job_info = client.wait_for_job_completion(job_id)
38+
print('Job Status:', job_info['status'])
39+
print('Job Result:', job_info['result'])
40+
3641
# Get job info
3742
job_info = client.get_job(job_id=job_id)
3843
print('Job Status:', job_info['status'])
1.16 KB
Binary file not shown.

musicai_sdk/client.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import time
12
import requests
23
from requests.exceptions import HTTPError
34

5+
46
class MusicAiClient:
5-
def __init__(self, api_key):
7+
def __init__(self, api_key, job_monitor_interval=2):
68
self.api_key = api_key
79
self.base_url = 'https://api.music.ai/api'
10+
self.job_monitor_interval = job_monitor_interval
811

912
def get_headers(self):
1013
return {
@@ -28,6 +31,12 @@ def get_job(self, job_id):
2831
raise HTTPError(f'Error getting job: {response.status_code} {response.text}')
2932
return response.json()
3033

34+
def get_job_status(self, job_id):
35+
response = requests.get(f'{self.base_url}/job/{job_id}/status', headers=self.get_headers())
36+
if response.status_code // 100 != 2:
37+
raise HTTPError(f'Error getting job: {response.status_code} {response.text}')
38+
return response.json()
39+
3140
def get_jobs(self):
3241
response = requests.get(f'{self.base_url}/job', headers=self.get_headers())
3342
if response.status_code // 100 != 2:
@@ -51,8 +60,15 @@ def delete_job(self, job_id):
5160
raise HTTPError(f'Error deleting job: {response.status_code} {response.text}')
5261
return response.json()
5362

63+
def wait_for_job_completion(self, id):
64+
while True:
65+
job = self.get_job_status(id)
66+
if job['status'] in ['SUCCEEDED', 'FAILED']:
67+
return self.get_job(id)
68+
time.sleep(self.job_monitor_interval),
69+
5470
def get_application_info(self):
5571
response = requests.get(f'{self.base_url}/application', headers=self.get_headers())
5672
if response.status_code // 100 != 2:
5773
raise HTTPError(f'Error getting application info: {response.status_code} {response.text}')
58-
return response.json()
74+
return response.json()

0 commit comments

Comments
 (0)