Skip to content

Instantly share code, notes, and snippets.

@afa
afa / curl.md
Created November 18, 2020 13:45 — forked from subfuzion/curl.md
curl POST examples

Common Options

-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.

-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).

-c, --cookie-jar <file name> File to save response cookies to.

@afa
afa / slugify.sql
Created August 29, 2019 12:58 — forked from ianks/slugify.sql
Generating Slugs in Postgres
CREATE EXTENSION IF NOT EXISTS "unaccent"
CREATE OR REPLACE FUNCTION slugify("value" TEXT)
RETURNS TEXT AS $$
-- removes accents (diacritic signs) from a given string --
WITH "unaccented" AS (
SELECT unaccent("value") AS "value"
),
-- lowercases the string
"lowercase" AS (
@afa
afa / tmux-cheatsheet.markdown
Created August 19, 2019 09:21 — forked from MohamedAlaa/tmux-cheatsheet.markdown
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@afa
afa / representations_file_storage_base_model.rb
Created February 4, 2019 18:11
примеры работы с внешним апи
module FileStorage
class BaseModel < Hashie::Trash
include Hashie::Extensions::Coercion
include Hashie::Extensions::MergeInitializer
include Hashie::Extensions::IndifferentAccess
include Hashie::Extensions::IgnoreUndeclared
end
end
@afa
afa / interactors_aggregated_process_org_result.rb
Last active February 4, 2019 16:47
small aggregation builder. ohm_ -- redis model, elastic_ -- elastic search index model.
# frozen_string_literal: true
class Aggregated::ProcessOrgResult
include Interactor
delegate :object, :answer_value, :answer_max_value, :depths, to: :context
def call
load_params
param = setup_param(@obj)
sub = setup_sub(@obj)

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 the sidekiq_profiler.rb (below) to your project

Adjust number of jobs you want your worker to process before you have heap dumped.
Run a sample worker: PROFILE=1 sidekiq -C config/sidekiq.yml and wait for jobs to be processed.

@afa
afa / 1-activerecord.rb
Created January 25, 2018 14:37 — forked from janko/1-activerecord.rb
INSERTing 50,000 records into a database in ActiveRecord, Arel, SQL, activerecord-import and Sequel.
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Migration.class_eval do
create_table(:records) do |t|
t.string :column
end
end
data = 50_000.times.map { |i| Hash[column: "Column #{i}"] }
@afa
afa / coffee-to-es6.md
Created November 15, 2017 23:23 — forked from kvz/coffee-to-es6.md
Going from CoffeeScript to ES6

These are the steps I took to change a modest project from CoffeeScript to ES6.

My ~2000LoC project took me around 6 hours to port, but I sunk 3 hours into a stupid mistake, and 2 into figuring out these steps, so with these steps & warnings at your disposal already, you should be able to do bigger projects in considerably less time.

Automatic tools only take you so far, there will be some manual fixing. Sometimes the generated code clearly doesn't look like a human wrote it, and sometimes there are bugs.

In this last case, when porting software trips over a particular bit of CoffeeScript, comment this bit out, and try transpiling the file again. If this is successfull, you'll see the commented CoffeeScript inside the JS, and you can port that bit yourself.

Okay let's dive right in!

@afa
afa / Gemfile
Created August 8, 2017 15:40 — forked from palkan/Gemfile
FactoryProf: profiler for your FactoryGirl
# if you want to render flamegraphs
gem "stackprof", require: false # required by flamegraph
gem "flamegraph", require: false
@afa
afa / factory_doctor.rb
Created August 8, 2017 15:39 — forked from palkan/factory_doctor.rb
FactoryDoc: detect useless data generation in tests
module FactoryGirl
module Doctor
module FloatDuration
refine Float do
def duration
t = self
format("%02d:%02d.%03d", t / 60, t % 60, t.modulo(1) * 1000)
end
end
end