Skip to content

Instantly share code, notes, and snippets.

@evd0kim
Forked from GuillaumeDerval/log_handler_slack.py
Created April 9, 2020 15:29
Show Gist options
  • Save evd0kim/be2eac55fb60332a0548c856095d6744 to your computer and use it in GitHub Desktop.
Save evd0kim/be2eac55fb60332a0548c856095d6744 to your computer and use it in GitHub Desktop.
A --log-handler-script log script for snakemake that pushes the status to Slack
# 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