Skip to content

Instantly share code, notes, and snippets.

View moofkit's full-sized avatar
💭
(/¯◡ ‿ ◡)/¯ ~ ┻━┻

Dmitriy Ivliev moofkit

💭
(/¯◡ ‿ ◡)/¯ ~ ┻━┻
View GitHub Profile
@moofkit
moofkit / errors.en.yml
Created June 8, 2021 19:29
Error message path for rule with nested params reproducing bug
en:
dry_validation:
errors:
rules:
data:
attributes:
phone_number:
invalid_format: 'is of invalid format'
@moofkit
moofkit / main.rs
Created May 15, 2022 09:59
Rust mutex deadlock example
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
fn main() {
let counter = Arc::new(Mutex::new(0));
let counter2 = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
@moofkit
moofkit / rename.rb
Created February 3, 2023 16:33
Rename Worker Class to Job
#!/usr/bin/env ruby
# frozen_string_literal: true
# https://juanitofatas.com/series/sidekiq/rename
files = Dir.glob("app/workers/**/*.rb")
puts "Found #{files.count} files"
files.each do |file|
content = File.read(file)
if match = content.match(/class (\S+)Worker/)
@moofkit
moofkit / rename.bash
Created February 9, 2023 15:19
Rename files with patterns in bash
$find . -depth -name "*old.txt" -exec sh -c 'f="{}"; mv -- "$f" "${f%old.txt}new.csv"' \;
@moofkit
moofkit / delete_line_from_file.rb
Created March 2, 2023 14:21
Delete matched line from file
#!/usr/bin/env ruby
# frozen_string_literal: true
files = Dir.glob("app/jobs/**/*.rb")
puts "Found #{files.count} files"
files.each do |file|
content = File.read(file).split("\n")
content.each_with_index do |line, index|
if match = line.match(/Worker/)
@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
@moofkit
moofkit / slow_query_logger.rb
Created March 19, 2025 10:32
ActiveRecord slow query log
# frozen_string_literal: true
ActiveSupport::Notifications.monotonic_subscribe("sql.active_record") do |_name, start, finish, _id, payload|
duration = (finish.to_f - start.to_f) * 1000
# NOTE: duration is in milliseconds
if duration >= 500
sql = payload[:sql].to_s.tr("\n", " ").squeeze(" ")
Rails.logger.info("Slow SQL: #{sql} (duration: #{duration.to_i}ms)")
end