Skip to content

Instantly share code, notes, and snippets.

View ssimeonov's full-sized avatar

Simeon Simeonov ssimeonov

View GitHub Profile
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
</head>
<body>
<div id="main_wrapper">
<div id="main_wrapper_top"></div>
function replaceDoc(html) {
var newDoc = document.open("text/html", "replace");
newDoc.write(html);
newDoc.close();
}
@ssimeonov
ssimeonov / new_relic_503.txt
Created January 31, 2014 08:37
New Relic can't handle ingestion again
2014-01-31T08:25:13.684816+00:00 app[analytics.1]: ** [NewRelic][01/31/14 08:25:13 +0000 213b2cc6-fb96-4c1a-b031-9edexxxxxxxx (2)] WARN : Error during check_for_and_handle_agent_commands, will retry later:
2014-01-31T08:25:13.684816+00:00 app[analytics.1]: ** [NewRelic][01/31/14 08:25:13 +0000 213b2cc6-fb96-4c1a-b031-9edexxxxxxxx (2)] WARN : NewRelic::Agent::ServerConnectionException: Service unavailable (503): Service Unavailable
require 'open-uri'
require 'json'
API = 'https://api.github.com/users/swoop-inc/repos'
open(API) do |io|
puts JSON.parse(io.read).
reject { |repo| repo['fork'] }.
map { |repo| repo['full_name'] }.
to_json
@ssimeonov
ssimeonov / choice_seq.rb
Created October 18, 2013 03:32
Choice sequence lexicographic position finder designed to work with any choice set. Optimized to remove tail recursion and eliminate intermediate data structures by using a continually mutating state set. Benchmark shows an operation taking 40 microseconds. The tail recursion version that builds intermediate data structures is 5x slower at 200+ …
class ChoiceSequence
MAX_CHOICES = 26
MAX_SEQUENCE_LENGTH = 25
FACTORIAL = (1..MAX_SEQUENCE_LENGTH).reduce([1]) do |memo, n|
memo << memo.last * n
end.freeze
def self.position(seq)
length = seq.length
@ssimeonov
ssimeonov / array_validator.rb
Last active March 22, 2022 14:48
Enumerable and array validators for ActiveModel::Validations in Rails. Especially useful with document-oriented databases such as MongoDB (accessed via an ODM framework such as Mongoid).
# Syntax sugar
class ArrayValidator < EnumValidator
end
@ssimeonov
ssimeonov / writing-bug-free-chef-recipes.md
Created July 27, 2013 04:21
Some tips for writing bug-free Chef recipes.

Writing Bug-Free Chef Recipes

Chef has unique abstraction and processing models. Until you understand them, it is easy to write buggy recipes.

The most important thing to understand is the following: a Chef recipe is not a collection of scripts.

Chef Processing Model

@ssimeonov
ssimeonov / .gitconfig
Last active December 20, 2015 06:29
~/.gitconfig
# ADD LOCAL CONFIGURATION HERE
[user]
name = Simeon Simeonov
email = sim@fastignite.com
[core]
editor = mate -w
excludesfile = /Users/sim/.gitignore_global
@ssimeonov
ssimeonov / 00_publish.rb
Last active December 20, 2015 05:19
When flow control is active on RabbitMQ (and connections are blocked) processing and exceptions can come out-of-order. The log file below is from Bunny in debug mode. It seems that queue.status is run at the same time as exchange.publish and actually errors out first.
def publish(data)
configure
conn = Bunny.new(connection_uri).start
channel = conn.create_channel
queue = channel.queue(queue_name, durable: true)
exchange = channel.direct(exchange_name, durable: true)
log_data(data)
exchange.publish(data, :routing_key => queue.name)
queue_status(queue.status)
rescue Exception => e
@ssimeonov
ssimeonov / cache_size.rake
Created July 21, 2013 02:01
Estimating the per-entry overhead of ActiveSupport::Cache::MemoryStore.
task :cache_size => :environment do
require 'objspace'
def calc_overhead(run, count)
cache = ActiveSupport::Cache.lookup_store :memory_store, size: 128.gigabytes
# keep all key/value pairs unique
cache.instance_variable_get(:'@data').compare_by_identity
GC.start
before = ObjectSpace.count_objects_size({})[:TOTAL]