Skip to content

Instantly share code, notes, and snippets.

@romiras
romiras / queue_dumper.cr
Created April 29, 2024 14:13
Drain Sidekiq' queue in Redis with matching args to stdout, otherwise push to another queue (Crystal lang)
# Run:
# crystal src/main.cr \
# -redis-url='redis://localhost:6379' \
# -namespace=sidekiq \
# -queue-name=foo \
# -class-name=Foo::BarProcessor \
# -arg1=123 \
# > filtered_jobs.jsonl
require "json"
@romiras
romiras / fix_raw_object_rails.rb
Created April 29, 2024 09:17
A hack to fix class dynamically in YaML-serialized attribute, Ruby on Rails
def raw_object
self[:raw_object]
rescue ArgumentError => e
raw = raw_object_before_type_cast
raw.gsub!(%r{!ruby/object:MyNamespace::Fixme}, '!ruby/object:MyNamespace::Corrected')
Psych.load(raw, symbolize_names: true)
end
@romiras
romiras / fix_yaml_column.rb
Created April 29, 2024 09:07
Fixing memoization issues in Ruby 2.4.x
# FILE: config/initializers/fix_yaml_column.rb
# serializes the columns that break because of memoized cache
#
# alternatively, we could have just removed the string for the memoized cache, but that sounded too dangerous
# hard-require to load the class
require 'active_record/coders/yaml_column'
class ActiveRecord::Coders::YAMLColumn
@romiras
romiras / loop_breaker.rb
Created April 27, 2024 21:28
Break a loop gracefully on Ctrl-C
Signal.trap("INT") { throw :exit_loop }
def aaa
catch :exit_loop do
loop do
puts 'foo'
p Benchmark.realtime { sleep 1 }
puts 'bar'
end
end
Signal.trap('INT', 'DEFAULT')
@romiras
romiras / routes.rb
Created April 21, 2024 10:08
Debugging Datadog agent in Ruby on Rails app
Rails.application.routes.draw do
get '/health', to: proc { [200, {}, ['ok']] }, as: :app_health_check
get '/dd', to: proc {
port_is_open = Socket.tcp('localhost', 8126, connect_timeout: 5) { true } rescue false
res = "Datadog port is open: #{port_is_open}"
[200, {}, [res]]
}, as: :dd_health_check
end
@romiras
romiras / langchain_sql1.py
Created April 7, 2024 21:17
Querying SQLite using LangChain
"""
This demo program outputs raw SQL query by analyzing table, which is run by agent executer, via SQL adapter.
"""
import os
import sqlite3
from langchain.agents import *
from langchain.sql_database import SQLDatabase
from langchain.chains import create_sql_query_chain
from langchain_community.llms import OpenAI
@romiras
romiras / ru-talk-c-family.txt
Created December 26, 2023 22:17
Разговор о C, C++ и C#
Я:
расскажи про проблемы языков программирования C, C++, C# в стиле хайку
ИИ:
Конечно, в стиле хайку:
C и C++: Память - дикая, Утечки, ошибки, страх. Указатели власть.
C#: Графика, сети, Управляй, безопасность. .NET, удобство.
@romiras
romiras / docker-compose.yml
Created August 22, 2023 21:06
Docker Compose for OpenSearch 2.x single node
version: "3"
services:
opensearch:
image: opensearchproject/opensearch:${OPENSEARCH_VERSION}
restart: "${OPENSEARCH_RESTART:-unless-stopped}"
environment:
- cluster.name=opensearch-cluster
- node.name=opensearch-node1
- discovery.type=single-node
@romiras
romiras / gist:77eaad109fed0555339fdaaff08677ba
Created February 26, 2023 14:56
Replace shebang line in place in all files in Ruby `bin`directory from version 2.7.6 to 2.7.7 on MacOS
# using ripgrep on macos
for f in $(rg --files-with-matches -F '2.7.6/'); do sed -i '' 's/\/2\.7\.6\//\/2\.7\.7\//' $f; done
@romiras
romiras / !resque_async_drainer.rb
Last active May 28, 2022 09:11
Asynchronously drain Resque queue with concurrent workers in Ruby
require_relative './logging'
require_relative './async_consumer'
def drain(data_store, queue_name, output_filename)
json_store = File.open(output_filename, 'a')
executor = lambda do |class_name, args|
Logging.logger.info "class_name: #{class_name}, args: #{args}"
json_store.write("#{ { class: class_name, args: args }.to_json }\n")