Last active
June 5, 2021 10:01
-
-
Save GuillaumeDerval/ee6d20c94448236dcca2e6bd8d47ca0f to your computer and use it in GitHub Desktop.
A --log-handler-script log script for snakemake that pushes the status to Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# License: MIT | |
import socket | |
from slacker import Slacker | |
import datetime | |
import time | |
import threading | |
SLACK_TOKEN = 'xoxb-00000000000-000000000000-000000000000000000000000' | |
CHANNEL_NAME = "your-channel-on-slack" | |
hostname = socket.gethostname() | |
prefix = "Snakemake is running on {}\n".format(hostname) | |
slack = Slacker(SLACK_TOKEN) | |
last_message = "Snakemake just started" | |
res = slack.chat.post_message(CHANNEL_NAME, last_message, as_user=True) | |
msg_ts = res.body['ts'] | |
channel_id = res.body['channel'] | |
def new_message(msg): | |
global last_message | |
last_message = msg | |
def update_message(): | |
slack.chat.update(channel_id, msg_ts, prefix + last_message + "\nLast update at: {}".format(datetime.datetime.now().strftime("%d/%m %H:%M:%S"))) | |
def get_bar(current, total, size=20): | |
nb_to_display = int(size * current / total) | |
return ("\u26AB" * nb_to_display) + ("\u26AA" * (size - nb_to_display)) | |
def log_handler(msg): | |
if msg.get('level') == 'progress': | |
cur = msg['done'] | |
total = msg['total'] | |
new_message(get_bar(cur, total) + " {}/{}".format(cur, total)) | |
def update_slack(): | |
while True: | |
update_message() | |
time.sleep(2) | |
update_message() | |
d = threading.Thread(name='slack-daemon', target=update_slack) | |
d.setDaemon(True) # the thread will close itself when snakemake shuts down | |
d.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment