Skip to content

Instantly share code, notes, and snippets.

Avatar

Joey Aghion joeyAghion

View GitHub Profile
@joeyAghion
joeyAghion / patch_datadog_sidekiq_traces.rb
Created Sep 6, 2019
This patch is based on https://github.com/DataDog/dd-trace-rb/pull/798 and can be added (e.g., as a rails initializer) to provide more meaningful trace labels rather than `Sidekiq::Extensions::DelayedClass`.
View patch_datadog_sidekiq_traces.rb
module Datadog
module Contrib
module Sidekiq
module Tracing
def job_resource(job)
if job['wrapped']
job['wrapped']
elsif job['class'] == 'Sidekiq::Extensions::DelayedClass'
YAML.load(job['args'].first)[0..1].join('.') rescue job['class'] # rubocop:disable Security/YAMLLoad
else
@joeyAghion
joeyAghion / quicktime_steps_to_record_screen_and_face.md
Created Sep 8, 2017
Free and easy way to record screen (including slide presentations) as well as your own face (via front-facing camera) using Quicktime.
View quicktime_steps_to_record_screen_and_face.md
  • Open Quicktime
  • File > New Movie Recording
  • View > Float on Top
  • Resize and reposition camera view over slide as desired
  • File > New Screen Recording
  • From "record" button drop-down, ensure microphone is enabled
  • Press record button
  • Drag
  • Start recording
  • Advance slides as necessary
View example.out
# In most cases, script proceeds as expected:
$ ruby script.rb
................................................................................ done.
$ ruby script.rb
................................................................................ done.
# Occasionally, fails with empty results:
$ ruby script.rb
........script.rb:59:in `block (3 levels) in <main>': got a nil widget (RuntimeError)
View utf-8-mac_demo.rb
> "Báe".length
=> 4
> "Báe".encoding
=> #<Encoding:UTF-8>
> "Báe".encode('utf-8', 'utf-8-mac').encoding
=> #<Encoding:UTF-8>
> "Báe".encode('utf-8', 'utf-8-mac').length
=> 3
View html_encoding_and_safe_strings_in_rails.rb
# In gravity Rails console, for example.
url = app.reset_password_url(a: 'b', c: 'd')
# => "http://www.example.com/reset_password?a=b&c=d"
url.html_safe?
# => false
ERB::Util.h(url) # explicitly call the h() helper that's implicitly called by <%= ... %>
# => "http://www.example.com/reset_password?a=b&amp;c=d"
view = ActionView::Base.new('app/views', {}, ActionController::Base.new)
# => #<ActionView::Base:0x000000110c5a60 ...>
view.render(inline: "<html><body><%= url %></body></html>", locals: {url: url}) # encode implicitly
View keybase.md

Keybase proof

I hereby claim:

  • I am joeyaghion on github.
  • I am joeya (https://keybase.io/joeya) on keybase.
  • I have a public key whose fingerprint is 7388 6671 6CF0 51A2 97BE 5D80 E61F 207F EE3F 0C11

To claim this, I am signing this object:

@joeyAghion
joeyAghion / heroku_apps_with_domains.sh
Created Dec 5, 2014
List heroku apps that I own with any associated domains.
View heroku_apps_with_domains.sh
# apps in the *first* section of heroku apps output (i.e., apps that I own)
apps=( `heroku apps | sed -e '/^$/,$d' | grep -v '^=='` )
# for each app that I own, show associated domains
for (( i = 0 ; i < ${#apps[@]} ; i++ ))
do
echo `heroku domains --app ${apps[$i]}`
done
View pre-commit_rubocop.rb
#!/usr/bin/env ruby
# Adapted from https://gist.github.com/hanloong/9849098
require 'english'
require 'rubocop'
ADDED_OR_MODIFIED = /A|AM|^M/.freeze
changed_files = `git status --porcelain`.split(/\n/)
View db_splitting_example.rb
class ImportBase
include Mongoid::Document
include Mongoid::Timestamps
field :import_version, type: String, default: 'v1' # legacy imports use 'v1' connection
before_create do
self.import_version = 'v2' # new imports use 'v2' connection
end
@joeyAghion
joeyAghion / delayed_jobs_by_queue_and_priority.rb
Last active Apr 8, 2018
delayed jobs by queue and priority
View delayed_jobs_by_queue_and_priority.rb
# Number of queued jobs per queue and priority
pp Delayed::Job.distinct(:queue).sort.map{|q| Delayed::Job.where(queue: q).distinct(:priority).sort.map{|p| [q, p, Delayed::Job.where(queue: q, priority: p).count] } }.sort; nil
# [[["any", 2, 3], ["any", 3, 3323], ["any", 4, 5542], ["any", 5, 2]],
# [["bidding", 3, 15]],
# [["default", 3, 10]],
# [["fair", 3, 246]]]
# First 20 jobs, in order that workers dequeue them
pp Delayed::Job.where(:run_at.lte => Time.now.utc, failed_at: nil).any_of({locked_by: /worker/}, {locked_at: nil}, {locked_at: {'$lt' => (Time.now.utc - 14400)}}).any_in(queue: %w{any imgs}).desc(:locked_by).asc(:priority).asc(:run_at).limit(20).map{|j| [j.id, j.handler.inspect[0..250], j.locked_by, j.run_at, (Time.now.utc - j.locked_at if j.locked_at)] }; nil
# [[<BSON::ObjectId:0x135554220 data=5629067d7261696db2000040>,