Skip to content

Instantly share code, notes, and snippets.

View rda1902's full-sized avatar
๐Ÿ˜€

Dmitriy rda1902

๐Ÿ˜€
View GitHub Profile
@rda1902
rda1902 / gist:7504e0244d528a798591c56ed1be4633
Created August 18, 2023 11:02
load to file all keys without expires
redis-cli keys "*" | while read LINE ; do TTL=`redis-cli ttl "$LINE"`; if [ $TTL -eq -1 ]; then echo "$LINE" >> 123.txt; fi; done;
@rda1902
rda1902 / async.rb
Created March 21, 2023 16:53 — forked from bruno-/async.rb
Threads vs Async Ruby
require "async"
CONCURRENCY = 1000
ITERATIONS = 100
def work
Async do |task|
CONCURRENCY.times do
task.async do
sleep 1

Pry Cheat Sheet

Command Line

  • pry -r ./config/app_init_file.rb - load your app into a pry session (look at the file loaded by config.ru)
  • pry -r ./config/environment.rb - load your rails into a pry session

Debugger

batches = Queue.new
Order.select(:id).find_in_batches do |batch|
batches << batch.map(&:id)
end
cpu_available.times do
Thread.new do
while(ids = batches.pop)
pid = Process.fork do
Sunspot.index! Order.includes(:product).find(ids)
end

My largest Sidekiq application had a memory leak and I was able to find and fix it in just few hours spent on analyzing Ruby's heap. In this post I'll show my profiling setup.

As you might know Ruby 2.1 introduced a few great changes to ObjectSpace, so now it's much easier to find a line of code that is allocating too many objects. Here is great post explaining how it's working.

I was too lazy to set up some seeding and run it locally, so I checked that test suite passes when profiling is enabled and pushed debugging to production. Production environment also suited me better since my jobs data can't be fully random generated.

So, in order to profile your worker, add this to your Sidekiq configuration:

if ENV["PROFILE"]
@rda1902
rda1902 / README.md
Created November 12, 2021 16:35 — forked from wvengen/README.md
Ruby memory analysis over time

Finding a Ruby memory leak using a time analysis

When developing a program in Ruby, you may sometimes encounter a memory leak. For a while now, Ruby has a facility to gather information about what objects are laying around: ObjectSpace.

There are several approaches one can take to debug a leak. This discusses a time-based approach, where a full memory dump is generated every, say, 5 minutes, during a time that the memory leak is showing up. Afterwards, one can look at all the objects, and find out which ones are staying around, causing the

@rda1902
rda1902 / yardoc_cheatsheet.md
Created August 12, 2021 14:17 — forked from chetan/yardoc_cheatsheet.md
YARD cheatsheet
class ParseEntries
def process(message)
entry = JSON.parse(message.value)
[entry]
end
end
class CombineEntries
def initialize
@open_transactions = {}
@rda1902
rda1902 / yardoc_cheatsheet.md
Created December 23, 2020 15:39 — forked from phansch/yardoc_cheatsheet.md
Improved YARD cheatsheet
@rda1902
rda1902 / docker-pry-rails.md
Created November 19, 2020 20:45 — forked from briankung/docker-pry-rails.md
Using pry-rails with Docker

First, add pry-rails to your Gemfile:
https://github.com/rweng/pry-rails

gem 'pry-rails', group: :development

Then you'll want to rebuild your Docker container to install the gems