add sns topic publishing of successful jobs
This commit is contained in:
parent
0e9c341741
commit
2b312547a0
1 changed files with 77 additions and 12 deletions
64
handler.py
64
handler.py
|
|
@ -8,6 +8,27 @@ import botocore
|
|||
CRAN_SRC_R3_URL = 'https://cran.rstudio.com/src/base/R-3/'
|
||||
CRAN_SRC_R4_URL = 'https://cran.rstudio.com/src/base/R-4/'
|
||||
batch_client = boto3.client('batch', region_name='us-east-1')
|
||||
sns_client = boto3.client('sns', region_name='us-east-1')
|
||||
|
||||
|
||||
class JobDetails:
|
||||
def __init__(self, version, platform):
|
||||
self.version = version
|
||||
self.platform = platform
|
||||
|
||||
@classmethod
|
||||
def from_job_name(cls, job_name):
|
||||
_R, version, platform = job_name.split('-', 2)
|
||||
return cls(version.replace('_', '.'), platform)
|
||||
|
||||
def job_name(self):
|
||||
return f"R-{self.version.replace('.', '_')}-{self.platform}"
|
||||
|
||||
def job_definition_arn(self):
|
||||
return os.environ[f"JOB_DEFINITION_ARN_{self.platform.replace('-','_')}"]
|
||||
|
||||
def to_json(self):
|
||||
return {'version': self.version, 'platform': self.platform}
|
||||
|
||||
|
||||
def _to_list(input):
|
||||
|
|
@ -78,21 +99,20 @@ def _container_overrides(version):
|
|||
|
||||
def _submit_job(version, platform):
|
||||
"""Submit an R build job to AWS Batch."""
|
||||
job_name = '-'.join(['R', version, platform])
|
||||
job_name = job_name.replace('.', '_')
|
||||
job_definition_arn = 'JOB_DEFINITION_ARN_{}'.format(platform.replace('-','_'))
|
||||
job_details = JobDetails(version, platform)
|
||||
|
||||
args = {
|
||||
'jobName': job_name,
|
||||
'jobName': job_details.job_name(),
|
||||
'jobQueue': os.environ['JOB_QUEUE_ARN'],
|
||||
'jobDefinition': os.environ[job_definition_arn],
|
||||
'containerOverrides': _container_overrides(version)
|
||||
'jobDefinition': job_details.job_definition_arn(),
|
||||
'containerOverrides': _container_overrides(job_details.version)
|
||||
}
|
||||
if os.environ.get('DRYRUN'):
|
||||
print('DRYRUN: would have queued {}'.format(job_name))
|
||||
return 'dryrun-no-job-{}'.format(job_name)
|
||||
print('DRYRUN: would have queued {}'.format(job_details.job_name))
|
||||
return 'dryrun-no-job-{}'.format(job_details.job_name)
|
||||
else:
|
||||
response = batch_client.submit_job(**args)
|
||||
print("Started job for R:{},Platform:{},id:{}".format(version, platform, response['jobId']))
|
||||
print("Started job with details:{},id:{}".format(job_details, response['jobId']))
|
||||
return response['jobId']
|
||||
|
||||
|
||||
|
|
@ -145,3 +165,29 @@ def poll_running_jobs(event, context):
|
|||
event['finishedJobCount'] = len(event['failedJobIds']) + len(event['succeededJobIds'])
|
||||
event['unfinishedJobCount'] = len(event['jobIds']) - event['finishedJobCount']
|
||||
return event
|
||||
|
||||
|
||||
def finished(event, _context):
|
||||
"""Publish details about successfully finished jobs."""
|
||||
|
||||
# first, if there were no succeeded jobs, return instead of publishing details about builds
|
||||
if len(event['succeededJobIds']) < 1:
|
||||
return event
|
||||
|
||||
# fetch all jobs, removing those which are not in our succeeded id list
|
||||
r = batch_client.list_jobs(jobQueue=os.environ['JOB_QUEUE_ARN'], jobStatus='SUCCEEDED')
|
||||
print(f'r: {r}')
|
||||
succeeded_jobs = [i for i in r['jobSummaryList'] if i['jobId'] in event['succeededJobIds']]
|
||||
|
||||
message = {'versions': []}
|
||||
|
||||
for job in succeeded_jobs:
|
||||
details = JobDetails.from_job_name(job['jobName'])
|
||||
message['versions'].append(vars(details))
|
||||
|
||||
response = sns_client.publish(
|
||||
TargetArn=os.environ['SNS_TOPIC_ARN'],
|
||||
Message=json.dumps(message),
|
||||
)
|
||||
print(f'Published to topic, response:{response}')
|
||||
return event
|
||||
|
|
|
|||
Loading…
Reference in a new issue