Skip to content

Instantly share code, notes, and snippets.

View YanhaoYang's full-sized avatar

Yanhao Yang YanhaoYang

  • Berlin, Germany
View GitHub Profile
@YanhaoYang
YanhaoYang / all-in-one-file.rb
Created May 14, 2014 00:17
code, tests and command line, all in one file
# main logic
def hi
'hi'
end
# rspec tests
require 'rspec'
describe "hi" do
it "says 'hi'" do
@YanhaoYang
YanhaoYang / begin_and_end_transactions_by_requests.rb
Last active August 29, 2015 14:01
If a Rails server is running in single threading mode, you can begin a transaction by a request / action, then end a transaction with another request / action. All requests between these two requests will be wrapped in one transaction. How can this be useful? Sometimes.
module Concerns
module Transactions
extend ActiveSupport::Concern
def transaction_begin
ActiveRecord::Base.connection.begin_db_transaction
ActiveRecord::Base.connection.increment_open_transactions
render nothing: true
end
@YanhaoYang
YanhaoYang / Tagged logs written by sidekiq workers in Rails log.rb
Created June 6, 2014 09:23
Rails itself writes tagged logs in Rails log files. Sidekiq workers can also write logs into Rails log files. But these logs are not tagged by default, therefore, it is hard to identify which logs are generated by a specific worker. With Sidekiq's middleware, it is quit easy to convert those logs into tagged logs.
module Sidekiq
module Middleware
module Server
class TaggedLogger
def call(worker, item, queue)
tag = "#{worker.class.to_s} #{SecureRandom.hex(12)}"
::Rails.logger.tagged(tag) do
job_info = "Start at #{Time.now.to_default_s}: #{item.inspect}"
::Rails.logger.info(job_info)
@YanhaoYang
YanhaoYang / vagrant-scp
Created July 7, 2014 14:44
A script to scp file to a Vagrant box on OS X
#!/usr/bin/env bash
if [ -n "$1" ];
then
ssh_config=$(mktemp -t ssh_config)
vagrant ssh-config > "$ssh_config"
scp -F "$ssh_config" $@
else
@YanhaoYang
YanhaoYang / nginx.conf
Created October 29, 2014 13:40
nginx configuration for Rails page caching
server {
listen 80;
server_name [domain name];
root [Rails root]/public;
location @passenger {
passenger_enabled on;
passenger_app_env production;
}
@YanhaoYang
YanhaoYang / controller.rb
Created October 29, 2014 13:43
Get template digest in controllers
def template_digest
tmpl = lookup_context.find(action_name, _prefixes)
ActionView::Digestor.digest(name: tmpl.virtual_path, finder: lookup_context)
end
@YanhaoYang
YanhaoYang / load-redis-session-in-console.rb
Created December 9, 2014 14:03
Load Rails' session data from redis in console
YourApp::Application.config.session_store.new(app).get_session({}, "session_store:fe95d1b2921ebf2eb7f18ece4f588520")
@YanhaoYang
YanhaoYang / cache-get-requests-of-activeresource.rb
Created January 10, 2015 15:06
Cache GET requests of ActiveResource
module ArCache
extend ActiveSupport::Concern
def get(path, headers = {})
cache_key = "ar_cache_#{path}"
Rails.cache.fetch(cache_key, expires_in: 60.seconds) do
super
end
end
@YanhaoYang
YanhaoYang / parse-rails-log.rb
Last active August 29, 2015 14:16
Parse rails log and generate structured data
require "json"
requests = {}
File.open("production.log").each_line do |ln|
if ln =~ /^\[([0-9a-f]+)\] (.+)/
id = $1
msg = $2
requests[id] ||= {lines: []}
requests[id][:lines] << msg
if msg =~ /Processing by (.+) as/
@YanhaoYang
YanhaoYang / rdoc-example.rb
Created November 17, 2015 14:41 — forked from nicholasjhenry/rdoc-example.rb
RDoc Example
# * Style guide based on Rails documention
module Namespace #:nodoc: don't document this
# Generic Namespace exception class
class NamespaceError < StandardError
end
# Raised when...
class SpecificError < NamespaceError
end