Skip to content

Instantly share code, notes, and snippets.

View benoittgt's full-sized avatar
🏴

Benoit Tigeot benoittgt

🏴
View GitHub Profile
# frozen_string_literal: true
# ruby ruby_trap.rb
begin
require "bundler/inline"
end
gemfile(true) do
source "https://rubygems.org"
gem "rspec", "~> 3"
@benoittgt
benoittgt / sidekiq_job_display_class_run_at.rb
Created March 8, 2021 17:35
Get first job status from Sidekiq that match a class
process, thread, msg = workers.select { |process, thread, msg| Sidekiq::Job.new(msg['payload']).display_class == 'GenerateInvoices
Job' }.first
job = Sidekiq::Job.new(msg['payload'])
puts "job class: #{job.display_class}, time: #{Time.at(message['run_at'])}"
@benoittgt
benoittgt / memory_sampling_middleware.rb
Created January 19, 2021 10:11
Memory middleware for Sidekiq
# lib/sidekiq/memory_sampling_middleware.rb
# frozen_string_literal: true
class Sidekiq::MemorySamplingMiddleware
attr_accessor :logger
def initialize(options = { logger: Sidekiq::Logging.logger })
self.logger = options.fetch(:logger) { raise 'Missing logger parameter in options' }
end
CLASS_KEY = 'class'
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
# With get process mem Gem from Richard
class MemoryDiff
class << self
def print_usage_before_and_after(description)
mem = GetProcessMem.new.mb
puts "MEM:BEFORE:#{description.inspect} - MEMORY USAGE(MB): #{mem.round(2)}mb"
yield
after_mem = GetProcessMem.new.mb
puts "MEM:AFTER:#{description.inspect} - MEMORY USAGE(MB): #{after_mem.round(2)}mb. Diff: #{display_diff(after_mem - mem)}"
@benoittgt
benoittgt / testing_child_processes.rb
Last active August 28, 2020 15:56
Read an print zombie child process info in Ruby
class ChildProcess
class Ps
attr_accessor :pid, :ppid, :state, :command
def to_s
"pid: '#{pid}', ppid: '#{ppid}', state: '#{state}', command: '#{command}'"
end
end
attr_reader :list
@benoittgt
benoittgt / childprocess.rb
Last active August 28, 2020 08:48
Playing with child process in Ruby
pid = Process.pid
puts "Current process pid : #{pid}"
child_pids = []
6.times.each do |i|
child_pids << Process.fork do
Process.setproctitle("ruby childp n #{i} with #{i * 3}s")
puts "sleeping #{i * 3}s"; sleep i * 3
end
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
@benoittgt
benoittgt / custom_rspec_matcher_with_block.rb
Created July 14, 2020 20:42
RSpec matcher with block execution
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
@benoittgt
benoittgt / eq_and_yield_matcher.rb
Created July 14, 2020 19:50
RSpec EQ and Yield
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"