Skip to content

Instantly share code, notes, and snippets.

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

Dmitriy Ivliev moofkit

💭
(/¯◡ ‿ ◡)/¯ ~ ┻━┻
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / Makefile
Created January 12, 2021 16:26
sentry-havy-serialize-reproduce
start:
rackup config.ru -p 9292
test:
time curl 'http://localhost:9292/exception'

After a bit of digging, I believe the issue was that the 9.6 install setup a cluster for the new instance which was interrupting the pg_upgrade. I ended up getting it working by dropping the new cluster and upgrading the 9.3 cluster using pg_upgradecluster from the following gist: https://gist.github.com/delameko/bd3aa2a54a15c50c723f0eef8f583a44

Install packages

sudo apt-get install postgresql-9.6 postgresql-server-dev-9.6 postgresql-contrib-9.6 -y

List clusters. Should show existing (9.3) and newly installed (9.6)

pg_lsclusters

@moofkit
moofkit / gist:2146f18953be9053ae372c063d28912b
Created September 30, 2016 12:21
lock monitor. postgresql
CREATE VIEW lock_monitor AS
SELECT COALESCE(((blockingl.relation)::regclass)::text, blockingl.locktype) AS locked_item,
(now() - blockeda.query_start) AS waiting_duration,
blockeda.pid AS blocked_pid,
blockeda.query AS blocked_query,
blockedl.mode AS blocked_mode,
blockinga.pid AS blocking_pid,
blockinga.query AS blocking_query,
blockingl.mode AS blocking_mode
FROM (((pg_locks blockedl
module ApplicationType
extend ActiveSupport::Concern
module ClassMethods
def model_name
superclass.model_name
end
def name
superclass.name