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
@kgantsov
kgantsov / README.md
Last active November 12, 2025 07:23
Leader election with Consul

Leader election with Consul

Install the Python Consul client:

pip install python-consul

Run consul

@kgantsov
kgantsov / peewee_rqlite.py
Created October 21, 2025 18:06
Minimalistic Peewee adapter for rqlite
import requests
import json
import time
from peewee import *
class RqliteCursor:
"""Minimal cursor-like wrapper for rqlite query responses."""
def __init__(self, rows=None, columns=None, lastrowid=None, rowcount=None):
self._rows = rows or []
self._columns = columns or []
@kgantsov
kgantsov / jwt_decode.py
Last active October 7, 2025 18:30
Script for printing JWT token's payload
import argparse
import base64
import json
import typing
from datetime import datetime
def parse_jwt_token(token_part: str) -> dict[str, typing.Any]:
token_part_with_padding = f"{token_part}{'=' * (len(token_part) % 4)}"
@kgantsov
kgantsov / kubectl_top_pods_container_stats.py
Last active March 25, 2025 08:06
Memory usage statistics (min, max, mean, 95th percentile) for Kubernetes pods grouped by container name
import argparse
import subprocess
import statistics
from collections import defaultdict
from typing import Union
def run_kubectl_top(
all_namespaces: bool = False,
@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
@kgantsov
kgantsov / large_file_server.py
Created April 14, 2017 11:18
Example of serving large files using aiohttp server without reading entire file into a memory
import asyncio
import os
from aiohttp import web
from aiohttp import streamer
@streamer
async def file_sender(writer, file_path=None):
"""
@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;
@kgantsov
kgantsov / time_from_uuid1.py
Created April 11, 2022 07:23
time_from_uuid1.py
from datetime import datetime
from datetime import timedelta
from uuid import UUID
def time_from_uuid1(u: str) -> datetime:
return datetime(1582, 10, 15) + timedelta(microseconds=UUID(u).time // 10)
@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():
fastapi==0.70.0
gino==1.0.1
pytest==6.2.5
pytest-asyncio==0.16.0
requests==2.26.0