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 / 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 / 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 / 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 / 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 / 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 / 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 / cron-job-for-low-battery
Created May 10, 2014 14:46
A cron job warning when battery is low
*/2 * * * * if [[ `pmset -g batt | awk -F'[^0-9]*' '{ print $3 }'` -lt 20 ]];then say "Battery low";fi
@YanhaoYang
YanhaoYang / postgresql-connections.md
Created December 18, 2013 09:36
PostgreSLQ connections
  • Show active connections

    • SELECT * FROM pg_stat_activity;
    • ps aux | grep postgres | wc -l (roughly, when you cannot connect to it)
  • Show @max_connections@

    • In psql: SHOW max_connections; (SHOW ALL;)
  • SQL: SELECT * FROM pg_settings WHERE name = 'max_connections';

@YanhaoYang
YanhaoYang / shared-memory.txt
Last active December 31, 2015 17:19
Increase shared memory in Linux
# 16 GB shared memory
$ sysctl -w kernel.shmmax=17179869184
# A page is almost always 4096 bytes except in unusual kernel configurations with "huge pages" (use getconf PAGE_SIZE to verify).
$ sysctl -w kernel.shmall=4194304
To preserve the settings between reboots, in /etc/sysctl.conf:
kernel.shmmax=17179869184
kernel.shmall=4194304
@YanhaoYang
YanhaoYang / Differences between Ruby 1.8 and 1.9.md
Created October 15, 2013 02:43
Differences between Ruby 1.8 and 1.9

Date.parse

1.8.7: parse(str='-4712-01-01', comp=false, sg=ITALY)
1.9.3: parse(string='-4712-01-01T00:00:00+00:00'[, comp=true[, start=ITALY]])

Note: the default values of comp are different.

Enumerator#next