Skip to content

Instantly share code, notes, and snippets.

module UpdateIgnore
def update_ignore(updates, conditions)
set = updates.map{|k,v| "#{connection.quote_column_name(k)} = #{connection.quote v}" }.join(', ')
connection.update "UPDATE IGNORE #{quoted_table_name} SET #{set} WHERE #{self.where(conditions).where_clauses.join('AND')}"
end
end
@skojin
skojin / multi_hosts_reverse_proxy
Last active August 29, 2015 14:08
Nginx reverse proxy
server {
listen 80 default_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
location / {
proxy_pass http://desthost.com;
}
}
SELECT CONCAT(table_schema, '.', table_name), CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows, CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA, CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx, CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size, ROUND(index_length / data_length, 2) idxfrac FROM information_schema.TABLES ORDER BY data_length + index_length DESC LIMIT 20;
@skojin
skojin / deep_freeze.rb
Created August 19, 2016 13:19
ruby deep freeze without monkey patch
# recursive freeze of hash, array
module DeepFreeze
extend self
def freeze(obj_or_enumerable)
if obj_or_enumerable.respond_to?(:each)
obj_or_enumerable.each { |v| DeepFreeze.freeze(v) }
end
obj_or_enumerable.freeze
obj_or_enumerable
@skojin
skojin / backup_bookmarks.rb
Last active April 10, 2017 07:58
backup imhonet.com
COOKIE = ARGV[0]
TO = ARGV[1] || 'json/bookmarks'
`mkdir -p #{TO}`
%w{films serials books games person}.each do |type|
print "#{type} "
(1..99).each do |page|
json = `curl 'http://#{type}.imhonet.ru/web.php?path=favorites/all/&domain=#{type}&page=#{page}' --silent -H '#{COOKIE}' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' -H 'Accept: application/json' -H 'Referer: http://films.imhonet.ru/favorites/all/' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' --compressed`
break unless json[0] == '{' # if content is not json, then pager ended
print '.'
@skojin
skojin / crontab
Last active January 18, 2017 21:23
aws cloudwatch custom rails metrics
*/5 * * * * aws cloudwatch put-metric-data --metric-name MyPassengerCount --namespace "System/Linux" --dimensions "InstanceId=$(ec2metadata --instance-id)" --unit Count --value $(ps ax | grep "[P]assenger RackApp" | wc -l)
*/5 * * * * aws cloudwatch put-metric-data --metric-name MyDJWorkersCount --namespace "System/Linux" --dimensions "InstanceId=$(ec2metadata --instance-id)" --unit Count --value $(ps ax | grep "[d]elayed_job" | wc -l)
*/5 * * * * aws cloudwatch put-metric-data --metric-name MyCometServerCount --namespace "System/Linux" --dimensions "InstanceId=$(ec2metadata --instance-id)" --unit Count --value $(ps ax | grep "[b]randrep_comet_server" | wc -l)
# N of Passenger processed that older then 1 hour
*/30 * * * * aws cloudwatch put-metric-data --metric-name MyOldPassengerCount --namespace "System/Linux" --dimensions "InstanceId=$(ec2metadata --instance-id)" --unit Count --value $(ps axo etimes,args | grep "RackApp" | awk '{if ($1 >= (1 * 3600)) print $0}' | wc -l)
# Each record of this class store one extra attribute for each model
# Used to store not important data need for reports, data sync, and etc
class ExtraValue < ActiveRecord::Base
belongs_to :record, polymorphic: true
scope :type, ->(klass){ where(record_type: klass) }
scope :record, ->(r){ where(record_id: r.id, record_type: r.class) }
class << self
@skojin
skojin / keybase.md
Created July 6, 2017 15:01
keybase.md

Keybase proof

I hereby claim:

  • I am skojin on github.
  • I am skojin (https://keybase.io/skojin) on keybase.
  • I have a public key ASBIneplQGFfIiZhlB6VN72mbLa_bwt0kBG4NLr-tcJzAwo

To claim this, I am signing this object:

@skojin
skojin / sql_dump.rb
Last active July 23, 2017 12:28
export AR model to sql
# export AR model to sql
# example:
# SqlDump.new( KeyValue.where(..) ).export{ |sql| puts sql }
# SqlDump.new( KeyValue.where(..) ).export(file: "key_values_dump.sql")
class SqlDump
def initialize(scope)
@scope = scope
@count = 0
end
@skojin
skojin / sql_tz.rb
Created July 23, 2017 19:14
util to map datetime column to date in current timezone
# SQL timezone time/date related utils
module SqlTz
extend self
# convert time to date with respect to timezone
# example: where("#{SqlTz.time_to_date(:created_at)} = ?", Date.today)
def date(column)
"DATE(DATE_ADD(#{column}, INTERVAL #{Time.current.utc_offset} SECOND))"
end