Skip to content

Instantly share code, notes, and snippets.

View adamkuipers's full-sized avatar

Adam Kuipers adamkuipers

View GitHub Profile
@adamkuipers
adamkuipers / gist:49d76c6494eb50d49821
Created November 24, 2014 20:31
Creating rabbitmq connection on after_fork callback.
Resque.after_fork do |job|
$rabbitmq_connection = Bunny.new(AMQP_URL)
$rabbitmq_connection.start
$rabbitmq_channel = $rabbitmq_connection.create_channel
$rabbitmq_exchange = $rabbitmq_channel.topic('stream', durable: true)
end
@adamkuipers
adamkuipers / gist:12578343d31a651bee4a
Last active August 29, 2015 14:06
ActiveRecord failing at batch-insert
irb(main):083:0> order.save.explain
(0.4ms) BEGIN
SQL (15.1ms) INSERT INTO "orders" ("address_id", "created_at", "customer_id", "order_request_id", "price_cents", "shipping_price_cents", "source", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["address_id", 291085], ["created_at", Wed, 10 Sep 2014 19:07:34 UTC +00:00], ["customer_id", 63601], ["order_request_id", "2687c3b4-c0b3-44a1-887d-04073ae62d1b"], ["price_cents", 8900], ["shipping_price_cents", 1200], ["source", "web-cart"], ["updated_at", Wed, 10 Sep 2014 19:07:34 UTC +00:00]]
SQL (4.1ms) UPDATE "customers" SET "orders_count" = COALESCE("orders_count", 0) + 1 WHERE "customers"."id" = 63601
SQL (7.9ms) INSERT INTO "order_items" ("created_at", "order_id", "photo_count", "printer", "product", "source", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["created_at", Wed, 10 Sep 2014 19:07:34 UTC +00:00], ["order_id", 80566], ["photo_count", 3], ["printer", 1], ["product", "calendardateless"], ["so
; Find Nth algorithm
;
; This algorithm finds the nth largest element in a list. First, it sorts
; the list in descending order and then places the value at index n in
; location 127.
;
; Dynamic Instruction Count for 19.b. at location 30
; 18,240 instructions
; 30th largest element in given array is 32
;