Skip to content

Instantly share code, notes, and snippets.

View marcosgz's full-sized avatar

Marcos G. Zimmermann marcosgz

View GitHub Profile
@marcosgz
marcosgz / git-ubuntu-11.04
Created September 5, 2011 20:36
git ubuntu 11.04
# Install Git
$ sudo apt-get update
$ sudo apt-get install git-core git-doc git-svn git-gui gitk
$ git --version
git version 1.7.1
# Abbreviations
$ git config --global alias.st status
$ git config --global alias.co checkout
$ git config --global alias.ci commit
@marcosgz
marcosgz / mysql-ubunty-11.04
Created September 6, 2011 13:24
Instalação do Mysql no ubuntu 11.04
# Install mysql
root@host:~# apt-get install mysql-server mysql-client libmysql-ruby libmysqlclient-dev mysql-admin mysql-query-browser
# Add "marcos" user with all privileges.
root@host:~# mysql -u root -p
mysql> CREATE USER 'marcos'@'localhost' IDENTIFIED BY 'secret';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'marcos'@'localhost' WITH GRANT OPTION;
mysql> quit
# Mysql config
@marcosgz
marcosgz / ruby1.8-rvm-ubuntu-11.04
Created September 6, 2011 13:33
Instalando ruby & rvm no ubuntu 11.04
root@host:~# apt-get install build-essential
root@host:~# apt-get install zlib1g-dev libreadline5-dev libssl-dev libxml2-dev
root@host:~# apt-get install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby openssl sqlite3 libsqlite3-dev libsqlite3-ruby1.8 rubygems1.8
root@host:~# ln -s /usr/bin/ruby1.8 /usr/bin/ruby
root@host:~# ln -s /usr/bin/ri1.8 /usr/bin/ri
root@host:~# ln -s /usr/bin/rdoc1.8 /usr/bin/rdoc
root@host:~# ln -s /usr/bin/irb1.8 /usr/bin/irb
# Instalar rvm (Single-User installation)
marcos@host:~$ sudo apt-get install curl
@marcosgz
marcosgz / gist:2662433
Created May 11, 2012 21:10
SQL => /browse/concert/dates:this-weekend
Event Load (21.7ms)
SELECT `events`.* FROM `events`
INNER JOIN `schedules` ON `schedules`.`event_id` = `events`.`id`
WHERE `events`.`deleted` = 0 AND ((ends_at >= '2012-05-13 23:59:59' OR ends_at IS NULL) AND ((frequency IS NULL AND DATE(starts_at) IN ('2012-05-11','2012-05-12','2012-05-13')) OR (starts_at <= '2012-05-11 00:00:00'
AND (
frequency = 'daily'
OR (
frequency = 'weekly'
AND dayname = DATE_FORMAT('2012-05-11', '%W')
)
<!DOCTYPE html>
<html>
<head>
<!-- (...) -->
<%= csrf_meta_tag unless response.cache_control[:public] %>
<!-- (...) -->
</head>
<body>
<!-- (...) -->
<% unless response.cache_control[:public] %>
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

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"]
@marcosgz
marcosgz / aws-ec2-public-hostname.txt
Created February 22, 2017 20:06
/opt/aws/bin/ec2-metadata -p will print the public hostname of that instance.
$ /opt/aws/bin/ec2-metadata -p
public-hostname: ec2-***-***-***.compute-1.amazonaws.com
@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)
@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+):/)