# Path: ob_pipelines/batch.py
# class BatchClient(object):
# 
#     def __init__(self):
#         self._client = boto3.client('batch')
#         self._queue = self.get_active_queue()
# 
#     def get_active_queue(self):
#         """Get name of first active job queue"""
#         # Get dict of active queues keyed by name
#         queues = {q['jobQueueName']: q for q in self._client.describe_job_queues()['jobQueues']
#                   if q['state'] == 'ENABLED' and q['status'] == 'VALID'}
#         if not queues:
#             raise Exception('No job queues with state=ENABLED and status=VALID')
# 
#         # Pick the first queue as default
#         return list(queues.keys())[0]
# 
#     def get_job_id_from_name(self, job_name):
#         # Job name is unique. If the job exists, use its id
#         jobs = self._client.list_jobs(jobQueue=self._queue, jobStatus='RUNNING')['jobSummaryList']
#         matching_jobs = [job for job in jobs if job['jobName'] == job_name]
#         if matching_jobs:
#             return matching_jobs[0]['jobId']
# 
#     def get_job_status(self, job_id):
#         """
#         Retrieve task statuses from ECS API
# 
#         Returns list of {SUBMITTED|PENDING|RUNNABLE|STARTING|RUNNING|SUCCEEDED|FAILED} for each id in job_ids
#         """
#         response = self._client.describe_jobs(jobs=[job_id])
# 
#         # Error checking
#         status_code = response['ResponseMetadata']['HTTPStatusCode']
#         if status_code != 200:
#             msg = 'Job status request received status code {0}:\n{1}'
#             raise Exception(msg.format(status_code, response))
# 
#         return response['jobs'][0]['status']
# 
#     def submit_job(self, job_definition, parameters, job_name=None, queue=None):
#         # Use boto3 client directly since it's easy
#         if job_name is None:
#             job_name = _random_id()
#         response = self._client.submit_job(
#             jobName=job_name,
#             jobQueue=queue or self.get_active_queue(),
#             jobDefinition=job_definition,
#             parameters=parameters
#         )
#         return response['jobId']
# 
#     def wait_on_job(self, job_id):
#         """Poll task status until STOPPED"""
# 
#         while True:
#             status = self.get_job_status(job_id)
#             if status == 'SUCCEEDED':
#                 logger.info('Batch job {} SUCCEEDED'.format(job_id))
#                 return True
#             elif status == 'FAILED':
#                 # Raise and notify if job failed
#                 data = self._client.describe_jobs(jobs=[job_id])['jobs']
#                 raise BatchJobException('Job {} failed: {}'.format(job_id, json.dumps(data, indent=4)))
# 
#             time.sleep(POLL_TIME)
#             logger.debug('Batch job status for job {0}: {1}'.format(
#                 job_id, status))
# 
#     def register_job_definition(self, json_fpath):
#         """Register a job definition with AWS Batch, using a JSON"""
#         with open(json_fpath) as f:
#             job_def = json.load(f)
#         response = self._client.register_job_definition(**job_def)
#         status_code = response['ResponseMetadata']['HTTPStatusCode']
#         if status_code != 200:
#             msg = 'Register job definition request received status code {0}:\n{1}'
#             raise Exception(msg.format(status_code, response))
#         return response


import os.path as op
import ob_pipelines
from ob_pipelines.batch import BatchClient


module_dir = op.dirname(ob_pipelines.__file__)  # __file__ is __init__.py, have to get parent folder

bc = BatchClient()  # thin wrapper for boto3.client('batch')

# assuming job definition is already registered

parameters = {
    'analysis_dir': 's3://com-dnli-ngs/test/1e4/'
}


# Path: ob_pipelines/batch.py
# class BatchClient(object):
# 
#     def __init__(self):
#         self._client = boto3.client('batch')
#         self._queue = self.get_active_queue()
# 
#     def get_active_queue(self):
#         """Get name of first active job queue"""
#         # Get dict of active queues keyed by name
#         queues = {q['jobQueueName']: q for q in self._client.describe_job_queues()['jobQueues']
#                   if q['state'] == 'ENABLED' and q['status'] == 'VALID'}
#         if not queues:
#             raise Exception('No job queues with state=ENABLED and status=VALID')
# 
#         # Pick the first queue as default
#         return list(queues.keys())[0]
# 
#     def get_job_id_from_name(self, job_name):
#         # Job name is unique. If the job exists, use its id
#         jobs = self._client.list_jobs(jobQueue=self._queue, jobStatus='RUNNING')['jobSummaryList']
#         matching_jobs = [job for job in jobs if job['jobName'] == job_name]
#         if matching_jobs:
#             return matching_jobs[0]['jobId']
# 
#     def get_job_status(self, job_id):
#         """
#         Retrieve task statuses from ECS API
# 
#         Returns list of {SUBMITTED|PENDING|RUNNABLE|STARTING|RUNNING|SUCCEEDED|FAILED} for each id in job_ids
#         """
#         response = self._client.describe_jobs(jobs=[job_id])
# 
#         # Error checking
#         status_code = response['ResponseMetadata']['HTTPStatusCode']
#         if status_code != 200:
#             msg = 'Job status request received status code {0}:\n{1}'
#             raise Exception(msg.format(status_code, response))
# 
#         return response['jobs'][0]['status']
# 
#     def submit_job(self, job_definition, parameters, job_name=None, queue=None):
#         # Use boto3 client directly since it's easy
#         if job_name is None:
#             job_name = _random_id()
#         response = self._client.submit_job(
#             jobName=job_name,
#             jobQueue=queue or self.get_active_queue(),
#             jobDefinition=job_definition,
#             parameters=parameters
#         )
#         return response['jobId']
# 
#     def wait_on_job(self, job_id):
#         """Poll task status until STOPPED"""
# 
#         while True:
#             status = self.get_job_status(job_id)
#             if status == 'SUCCEEDED':
#                 logger.info('Batch job {} SUCCEEDED'.format(job_id))
#                 return True
#             elif status == 'FAILED':
#                 # Raise and notify if job failed
#                 data = self._client.describe_jobs(jobs=[job_id])['jobs']
#                 raise BatchJobException('Job {} failed: {}'.format(job_id, json.dumps(data, indent=4)))
# 
#             time.sleep(POLL_TIME)
#             logger.debug('Batch job status for job {0}: {1}'.format(
#                 job_id, status))
# 
#     def register_job_definition(self, json_fpath):
#         """Register a job definition with AWS Batch, using a JSON"""
#         with open(json_fpath) as f:
#             job_def = json.load(f)
#         response = self._client.register_job_definition(**job_def)
#         status_code = response['ResponseMetadata']['HTTPStatusCode']
#         if status_code != 200:
#             msg = 'Register job definition request received status code {0}:\n{1}'
#             raise Exception(msg.format(status_code, response))
#         return response


# Path: ob_pipelines/apps/multiqc/test_aws_batch.py
import os.path as op
import ob_pipelines
from ob_pipelines.batch import BatchClient



module_dir = op.dirname(ob_pipelines.__file__)  # __file__ is __init__.py, have to get parent folder

bc = BatchClient()  # thin wrapper for boto3.client('batch')

# assuming job definition is already registered

parameters = {
    'analysis_dir': 's3://com-dnli-ngs/test/1e4/'
}


job_id = bc.submit_job('multiqc', parameters)