Skip to content

Instantly share code, notes, and snippets.

View benoittgt's full-sized avatar
🏴

Benoit Tigeot benoittgt

🏴
View GitHub Profile
# start FactoryBot id sequence at a different number. Naive approach.
# other idea https://github.com/thoughtbot/factory_bot/issues/350#issuecomment-14475441
FactoryBot.define do
factory :app do
sequence(:id) { |n| 1.to_s.ljust(self.instance.class.name.size, "0").to_i + n }
end
end
@benoittgt
benoittgt / totp_collision.rb
Created April 1, 2021 11:49
Test TOTP collision
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rotp"
# 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.