Skip to content

Instantly share code, notes, and snippets.

View marcosgz's full-sized avatar

Marcos G. Zimmermann marcosgz

View GitHub Profile
@marcosgz
marcosgz / encryption-decryption.rb
Created June 19, 2021 14:13 — forked from gevans/encryption-decryption.rb
A couple examples of using asymmetric RSA signing and encryption using Ruby's OpenSSL libraries.
require 'openssl'
key = OpenSSL::PKey::RSA.new(2048)
p encrypted_string = key.public_encrypt('my plaintext string', OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
p decrypted_string = key.private_decrypt(encrypted_string, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
@marcosgz
marcosgz / postgres_queries_and_commands.sql
Created April 1, 2020 19:27 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@marcosgz
marcosgz / boolean_value.rb
Created August 23, 2018 21:04 — forked from attenzione/boolean_value.rb
Ruby: casting to boolean value
class BooleanValue
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/column.rb#L8
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF']
def self.from_string(value)
Rails.present? ? with_rails(value) : without_rails(value)
end
private
@marcosgz
marcosgz / puma.monitrc
Created December 13, 2017 15:56 — forked from sudara/puma.monitrc
Example config needed to use monit with puma, monitoring workers for mem.
# this monit config goes in /etc/monit/conf.d
check process puma_master
with pidfile /data/myapp/current/tmp/puma.pid
start program = "/etc/monit/scripts/puma start"
stop program = "/etc/monit/scripts/puma stop"
group myapp
check process puma_worker_0
with pidfile /data/myapp/current/tmp/puma_worker_0.pid
@marcosgz
marcosgz / gist:9f3c7a8116d687f06cd1c22e651fb25d
Created October 4, 2017 13:27 — forked from stetic/gist:2929166
Delete local memcached keys with specific prefix using Ruby
#!/usr/bin/env ruby
require 'net/telnet'
key_prefix = 'www.your:prefix'
cache_dump_limit = 1000
localhost = Net::Telnet::new("Host" => "localhost", "Port" => 11211, "Timeout" => 3)
slab_ids = []
localhost.cmd("String" => "stats items", "Match" => /^END/) do |c|
matches = c.scan(/STAT items:(\d+):/)
@marcosgz
marcosgz / README.rdoc
Created August 18, 2017 20:00 — forked from ktheory/README.rdoc
An example for logging Fog requests in a Rails app

Instrument Fog Requests in Rails

This code adds debug statements to the rails log for each fog reqests (similar to how active record shows SQL queries):

Fog AWS Compute Request (803.0ms)  [ {"Action"=>"DescribeInstances", ...} ]

The end of the request includes cumulative Fog info:

Completed 200 OK in 6043ms (Views: 2955.0ms | ActiveRecord: 319.9ms | Fog: 1642.6ms, 4 reqs)

My largest Sidekiq application had a memory leak and I was able to find and fix it in just few hours spent on analyzing Ruby's heap. In this post I'll show my profiling setup.

As you might know Ruby 2.1 introduced a few great changes to ObjectSpace, so now it's much easier to find a line of code that is allocating too many objects. Here is great post explaining how it's working.

I was too lazy to set up some seeding and run it locally, so I checked that test suite passes when profiling is enabled and pushed debugging to production. Production environment also suited me better since my jobs data can't be fully random generated.

So, in order to profile your worker, add this to your Sidekiq configuration:

if ENV["PROFILE"]
MAX_DISTANCE_AWAY_IN_KM = 100.0
RAD_PER_DEG = 0.017453293
Rmiles = 3956 # radius of the great circle in miles
Rkm = 6371 # radius in kilometers, some algorithms use 6367
Rfeet = Rmiles * 5282 # radius in feet
Rmeters = Rkm * 1000 # radius in meters
def haversine_distance( lat1, lon1, lat2, lon2 )
dlon = lon2 - lon1