-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscreen_run.py
More file actions
executable file
·138 lines (118 loc) · 4.84 KB
/
Copy pathscreen_run.py
File metadata and controls
executable file
·138 lines (118 loc) · 4.84 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
import sys
import subprocess
import re
import time
import pandas as pd
import numpy as np
from datetime import date, datetime
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from importlib.machinery import SourceFileLoader
from typing import Union
import functools
import logging
logging.basicConfig(filename='/Volumes/NGS_2/Bioinformatics/jarnn/analysis_for_run.log', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# create logger
logger = logging.getLogger('screen_run.py')
logger.setLevel(logging.DEBUG)
def log(_func=None, *, my_logger):
def decorator_log(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
logger = my_logger
args_repr = [repr(a) for a in args]
kwargs_repr = [f"{k}={v!r}" for k, v in kwargs.items()]
signature = ", ".join(args_repr + kwargs_repr)
logger.debug(f"function {func.__name__} called with args {signature}")
try:
result = func(*args, **kwargs)
return result
except Exception as e:
logger.exception(f"Exception raised in {func.__name__}. exception: {str(e)}")
raise e
return wrapper
if _func is None:
return decorator_log
else:
return decorator_log(_func)
@log(my_logger=logger)
def my_subprocess_run(*args, **kwargs):
return subprocess.run(*args, **kwargs)
run_name = str(sys.argv[1])
try:
run_type = str(sys.argv[2])
except:
run_type = None
config = SourceFileLoader("config","/Volumes/NGS_2/Bioinformatics/jarnn/config.py").load_module()
# Function to handle the stdout of the bs CLI tool. Returns a list of list of strings.
@log(my_logger=logger)
def bs_out(bashCommand):
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
tmp=output.decode("utf-8")
tmp=tmp.split('\n')
tmp.pop(0)
tmp.pop(1)
tmp.pop(-1)
tmp.pop(-1)
tmp=[x.replace('|', '') for x in tmp]
tmp=[re.sub(r"\s+", ' ', x) for x in tmp]
return tmp
# Slack_sdk values needed for sending messages to Slack. Uses an API already set up on the Slack website for the UPHL Workspace to post messages on the notifications channel.
# Function makes sending Slack messages as easy as using the print funcition.
client = WebClient(token=config.token)
channel_id = config.channel_id
@log(my_logger=logger)
def slack_message(string):
try:
result = client.chat_postMessage(
channel=channel_id,
text=string
)
except:
logger.info("Slack Error")
# These are the config files that are on the production account
configurations=["bioinfo"]
# This While loops Uses the BaseSpace CLI tool to monitor the progress of the run
t=0
p=0
while t==0:
for i in configurations:
bashCommand='bs list --config=%s runs' % i
tmp=bs_out(bashCommand)
tmp=pd.DataFrame(index=[row.split()[2] for row in tmp[1:]], columns=tmp[0].split()[1:], data=[row.split()[1:4] for row in tmp[1:]])
try:
if tmp.at[run_name,'Status']=='Complete':
user=i
idd=tmp.at[run_name,'Id']
slack_message('%s is "Complete" on BSSH' % run_name)
logger.info('%s is "Complete" on BSSH' % run_name)
t=1
break
except:
print('Not Complete')
try:
if tmp.at[run_name,'Status']=='Failed' or tmp.at[run_name,'Status']=='Stopped' or tmp.at[run_name,'Status']=='Needs Attention' or tmp.at[run_name,'Status']=='Timed Out':
user=i
idd=tmp.at[run_name,'Id']
slack_message('%s has "Failed" or was unable to complete on BSSH; Script is aborting, check BSSH for more information' % run_name)
logger.info('%s has "Failed" or was unable to complete on BSSH; Script is aborting, check BSSH for more information %s' % (run_name,datetime.now()))
sys.exit()
except:
print('Not Failed')
if t==0:
print('Sleep')
time.sleep(1200)
if run_type:
if run_type == 'grandeur':
process = subprocess.Popen(["python","/Volumes/NGS_2/Bioinformatics/jarnn/grandeur_aws_automation.py", "%s" % run_name], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
print("Output:", stdout, stderr)
if run_type == 'mycosnp':
process = subprocess.Popen(["python","/Volumes/NGS_2/Bioinformatics/jarnn/mycosnp_aws_automation.py", "%s" % run_name], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
print("Output:", stdout, stderr)
my_subprocess_run(["screen", "-S", "%s" % run_name, "-X", "quit"])