Skip to content

Instantly share code, notes, and snippets.

@lloydwatkin
lloydwatkin / action_mailer_job.rb
Created November 7, 2024 11:45
ActionMailer using pure Sidekiq
# i.e. Sidekiq job
class ApplicationMailerJob
include Sideiq::Worker
sidekiq_options queue: "low_priority"
def perform(mailer, action, arguments)
mailer.constantize.send(action, arguments).deliver_now
end
@moofkit
moofkit / application_record.rb
Last active May 28, 2024 08:43
ActiveRecord alter default timeout for PostgreSQL query
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
class << self
# Start a new transaction with a statement timeout.
# The timeout is reset after the block is executed. Works even with pg_bouncer in transaction mode
# This is useful for queries that are expected to be slower than the default timeout
# timeout - the timeout in seconds
# Usage:
# with_statement_timeout(10) do
@synth
synth / flag_registration.rb
Created February 14, 2024 06:33
Feature flag code detection
module Flipper
module FlagRegistration
# These functions are all memoized because they should be static for the
# lifetime of a deployment (albeit they are really static to a Ruby process)
def self.registered_flags
@registered_flags ||= YAML.load_file("config/feature_flags.yml")
end
def self.flags_in_code
@flags_in_code ||= begin
@navidemad
navidemad / benchmark.rb
Last active October 2, 2023 21:57
Benchmark Rails methods
# frozen_string_literal: true
# Usage:
# bundle exec rails runner benchmark.rb
require "benchmark"
require "benchmark/ips"
require "objspace"
require "timecop"
@jjb
jjb / file.md
Last active November 11, 2024 20:06
Using Jemalloc 5 with Ruby.md

For years, people have been using jemalloc with ruby. There were various benchmarks and discussions. Legend had it that Jemalloc 5 didn't work as well as Jemalloc 3.

Then, one day, hope appeared on the horizon. @wjordan offered a config for Jemalloc 5.

Ubuntu/Debian

FROM ruby:3.1.2-bullseye
RUN apt-get update ; \
@natematykiewicz
natematykiewicz / migrate-sidekiq-redis.rb
Last active February 21, 2024 17:18
Migrate Sidekiq Redis
# A script to migrate Sidekiq's redis to a new server.
# This obviously can work for any redis, but I only handled
# data types that Sidekiq uses.
require 'redis'
old_redis = Redis.new url: 'redis://old-redis:6379'
new_redis = Redis.new url: 'redis://new-redis:6379'
unknowns = []
@bf4
bf4 / README.md
Created June 7, 2022 17:51
warnings and Ruby 3.0

on CI and local

export RUBYOPT="-W:deprecated" # show warnings, deprecations only
@natematykiewicz
natematykiewicz / unroutable_routes.rake
Last active June 9, 2022 19:47
Find routes that will raise a routing error when requested
desc 'Find routes that will raise a routing error when requested'
task unroutable_routes: :environment do
# A lot of this code was taken from how `rake routes` works
# https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/railties/lib/rails/commands/routes/routes_command.rb
require 'action_dispatch/routing/inspector'
unroutables = Rails.application.routes.routes.
map { |r| ActionDispatch::Routing::RouteWrapper.new(r) }.
reject { |r| r.internal? || r.engine? || r.path.starts_with?('/rails/') || !r.controller }.
@palkan
palkan / README.md
Created January 24, 2022 13:29
Rails boot time profiling

Add the following to application.rb:

$icallbacks = []
$icallbacks.define_singleton_method(:print) do
  puts sort_by { |(a, b)| -b }.map { |(a, b)| "#{b}\t\t#{a}" }.join("\n")
end

ActiveSupport::Notifications.subscribe("load_config_initializer.railties") do |event|
 $icallbacks &lt;&lt; [event.payload[:initializer], event.duration]
@DmitryTsepelev
DmitryTsepelev / change_column_type.rb
Created August 27, 2020 08:46
How to change column type (e.g., int -> bigint) without downtime
ActiveRecord::Migration.remove_foreign_key(:current_table, :foreign_table) # no lock
ActiveRecord::Migration.add_column(:current_table, :column_bigint, :bigint) # no lock
copy_data = lambda do
CurrentTable.where(column_bigint: nil).where.not(column: nil).in_batches do |batch|
batch.update_all("column_bigint = column")
end
end