Skip to content

Instantly share code, notes, and snippets.

View kgantsov's full-sized avatar
🇺🇦

Kostiantyn Hantsov kgantsov

🇺🇦
  • iconik.io
  • Stockholm, Sweden
View GitHub Profile
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
for (( i = 30; i < 38; i++ )); do echo -e "\033[0;"$i"m Normal: (0;$i); \033[1;"$i"m Light: (1;$i)"; done
@kgantsov
kgantsov / postgreslq table dump
Last active March 4, 2019 12:52
Dump postgresql table to a csv file
psql postgresql://user:password@localhost:5432/database -A -F ";" -c "SELECT * FROM users" > users.csv
template1=# Copy (SELECT * FROM users) To '/tmp/users.csv' With CSV DELIMITER ';';
@kgantsov
kgantsov / set_expire_celery_result_in_redis.sh
Created April 17, 2018 06:40
Small script that set an expiration time on each celery result key in redis
for i in $(redis-cli keys celery-task-meta-*); do redis-cli EXPIRE "$i" 3600; done;
import functools
import signal
from contextlib import contextmanager
def signal_handler(_, frame):
raise TimeoutError(
'It took too much time to run code in file <{}> on line <{}>'.format(
frame.f_code.co_filename,
@kgantsov
kgantsov / print_in_colors.py
Created May 9, 2018 10:03
Bunch of functions for colorizing text that needs to be printed to a terminal
from functools import partial
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
def colorize(color, text):
"""
Usage:
>>> print(colorize("BLUE", "This text will be printed in blue color"))
This text will be printed in blue color
@kgantsov
kgantsov / latency.markdown
Created May 15, 2018 11:43 — forked from hellerbarde/latency.markdown
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@kgantsov
kgantsov / rabbitmq add user
Created May 30, 2018 11:36 — forked from tvlooy/rabbitmq.sh
RabbitMQ add user
rabbitmqctl add_user someuser somepass
rabbitmqctl set_user_tags someuser administrator
rabbitmqctl set_permissions -p / someuser ".*" ".*" ".*"
Enable admin:
rabbitmq-plugins enable rabbitmq_management
rabbitmqadmin list queues
@kgantsov
kgantsov / diff_str.sh
Created July 18, 2018 08:38
A bash function that prints difference between two strings
## Install `colordiff` on Ubuntu
# sudo apt-get install colordiff
## Install `colordiff` on MacOS X
# brew install colordiff
# diff-hight is a cli utility from git (since version 2.9).
# It is located in `/usr/share/doc/git/contrib/diff-highlight/diff-highlight` on Ubuntu and
# on `/usr/local/Cellar/git/2.18.0/share/git-core/contrib/diff-highlight/diff-highlight` on MacOS X
@kgantsov
kgantsov / show_long_running_celery_tasks.py
Created November 29, 2018 12:16
Python function that prints currently running celery tasks that are running longer that number of seconds specified in threshold param
from datetime import datetime
import time
import kombu.five
from celery.task.control import inspect
def get_stuck_celery_tasks(threshold=600):
i = inspect()
for worker, tasks in i.active().items():
@kgantsov
kgantsov / revoke_celery_tasks.py
Created January 2, 2019 11:22
Helper functions that revokes celery tasks by name and ID
from celery.task.control import revoke
from celery.task.control import inspect
def revoke_tasks_by_name(task_name, worker_prefix=''):
"""
Revoke all tasks by the name of the celery task
:param task_name: Name of the celery task
:param worker_prefix: Prefix for the worker