Skip to content

Instantly share code, notes, and snippets.

@ericchen
ericchen / pg_advisory_lock.rb
Created April 19, 2024 06:06
pg advisory locks
class LockManager
def self.with_transaction_lock(lock_name)
lock_id = Zlib.crc32(lock_name.to_s)
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute("SELECT pg_advisory_xact_lock(#{lock_id})")
yield
end
end
def self.with_session_lock(lock_name)
@ericchen
ericchen / js.md
Last active April 18, 2024 05:04
前端面试题目
  1. JS作用域有哪些,分别有什么区别?
  2. 说一说cookie sessionStorage localStorage 区别
  3. 怎么添加事件监听,有哪些方式,请分别写出
  4. const arr = [1, [2, [3, 4]]], 请写一个函数实现将数组arr扁平化。
  5. 实现一个串行的后台接口加载函数, 要求上一个接口加载完毕,再加载下一个接口
  6. HTTP 常用的状态码及使用场景
  7. HTTP 常用的请求方式,区别和用途
  8. 跨域有哪些解决方案?各有什么区别。
@ericchen
ericchen / pinterest_spammer.rb
Created May 28, 2016 09:37 — forked from v-pukman/pinterest_spammer.rb
Post to Pinterest using Ruby and Mechanize
# Usage example:
#
# s = PinterestSpammer.new
# s.sign_in 'your_account_email', 'your_account_password'
# s.get_boards # get boards list, that contains board_id (for example - 455708124733779520)
# s.create_pin(455708124733779520, 'https://xyz.xyz/', 'http://rubyonrails.org/images/rails.png', 'Spammer!')
#
#
require 'mechanize'
class PinterestSpammer
@ericchen
ericchen / directory_mv.md
Last active May 8, 2016 14:44
将一个目录下一定时间内的目录移到另外一个目录下

将当前目录下过去30天的目录移动指定目录下

find . -mindepth 1 -type d -mtime +30 | xargs -i mv {} /home/bu/date_30/

1.当前时间24小时—当前时间(昨天-今天)

find . -mtime 0

2.当前时间48小时前–当前时间24小时前(前天-昨天)

@ericchen
ericchen / redis_hack.md
Last active May 1, 2016 15:14
redis漏洞导致服务器被黑

今天早上起来发现登录不进去vps了,然后通过vps web端将root密码重新设置, 检查/etc/passwd 文件被加入下面user REDIS0006?, Google了一下,原来是利用了redis的漏洞,由于偷懒将redis没有设置密码且以root权限运行,也没有设置只允许本地访问 (bind 127.0.0.1未打开)导致黑客可以修改/etc/passwd文件

相关文章:

@ericchen
ericchen / postgresql.md
Created February 27, 2016 15:09
在debian 7 wheezy上安装postgresql-9.4

添加源并安装

  1. vi /etc/apt/sources.list.d/pgdg.list
  2. 添加下面内容到文件:deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main
  3. 添加签名key: wget http://www.postgresql.org/media/keys/ACCC4CF8.asc, apt-key add ACCC4CF8.asc
  4. 更新源: apt-get update
  5. 安装: sudo apt-get install postgresql-9.4
  6. 配置验证方式: sudo vi /etc/postgresql/9.4/main/pg_hba.conf 将peer改成md5, 然后重启: sudo service postgresql restart

配置数据库

  1. 登录数据库: sudo -u postgres psql template1
  2. 创建用户名: CREATE USER dbuser WITH PASSWORD 'password';
@ericchen
ericchen / sidekiq.md
Last active March 15, 2016 10:20
sidekiq使用
启动sidekiq

bundle exec sidekiq -c 10 -e production -L log/sidekiq.log -d

队列查询

# See workers
Sidekiq::Client.registered_workers

# See queues
Sidekiq::Client.registered_queues
@ericchen
ericchen / mysql_outfile.md
Created December 9, 2015 03:14
导出mysql表中部分字段迁移到其它数据库

导出: select tag,counter into OUTFILE '/var/lib/mysql/tags.csv' FIELDS TERMINATED BY ',' FROM tag_counters ORDER BY counter desc;

导入到其它数据库: LOAD DATA INFILE '/Users/eric/Downloads/tags.csv' INTO TABLE tags FIELDS TERMINATED BY ',' (tag, counter);

@ericchen
ericchen / newline_textarea.rb
Created November 29, 2015 14:28
在textarea里面正确显示换行
#need replace with double quote instead of single quote
test_string <<EOF
Specifications:\n Brand: Brand new and high quality\n Style: Sexy black lace mask\n\nItem Description\n 1. Suit for all kinds of occasion like party and Halloween.\n 2. Matching your clothes well.\n\nPackage Includes\n1 x Masquerade Mask
EOF
test_string.gsub(/(\\r\\n)|\\n/, "\n")
@ericchen
ericchen / https.md
Created November 25, 2015 01:57
namecheap获取ssl cert并配置https

#####首先要激活在namecheap上购买的ssl服务

  1. 生成SSL证书获取CSR bash openssl req -new -key yourdomain.pem -out yourdomain.csr , 然后按照提示输入信息即可
  2. 执行cat yourdomain.csr 内容大概如下 ``` -----BEGIN CERTIFICATE REQUEST----- MIICxDCCAawCAQAwfzELMAkGA1UEBhMCQ04xEDAOBgNVBAgMB0JlaWppbmcxEDAO BgNVBAcMB0JlaWppbmcxDjAMBgNVBAoMBUJpZ2ZhMRcwFQYDVQQDDA55b3VyZG9t YWluLmNvbTEjMCEGCSqGSIb3DQEJARYUYWRtaW5AeW91cmRvbWFpbi5jb20wggEi MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC9fsxThwLzCd54s2GMUcjlleCj LTf9bYGQyIjn+6z7kJbDbcrkbZxMysfRnFLa6u5oK7S9PQfGHK/gZeHfSLXbD/GL wi171tZpnQHjfLjMbHEUEUCFQD7ueek/v3/Tr+T0+em1gQt/93K2dqv7Cx+bupwc zNkQmqNgqslC4sdKQZjLUIHYLr/j8lQQLOOn9/PiuNOGCTaK5g9TA20oVCjpJZuf