Comments

Rails调用oracle的存储过程

调用方式如下:

cursor = ActiveRecord::Base.connection.raw_connection.parse("BEGIN MYPROC(:age, :name, :fathers_age, :capital_name); END;")
cursor.bind_param(:name, 'akshay')
cursor.bind_param(:age, '20')
cursor.bind_param(:fathers_age, nil, Fixnum)
cursor.bind_param(:capital_name, nil, String, 20)
cursor.exec()
p cursor[:fathers_age], cursor[:capital_name]


require 'sequel'
db = Sequel.connect(:adapter=>'oracle', :host=>'localhost', :database=>'db_name', :user=>'test', :password=>'test')
db.execute("begin MYPROC(:age, :name, :fathers_age, :capital_name); end;", {:arguments => [[20, "integer"], ["akshay", "string"], [nil, "integer"], [nil, "string"]]}) { |cursor| [cursor[3], cursor[4]] }
Comments

Ruby根据不同的异常处理不同事件

在一个class里面你想根据不同的异常回调不同事件

class NoMoneyException < StandardError
end

class ValidNumberException < StandardError
end

class Sale

    def self.create(options)
        obj = new(options)
        obj.validation
    end

    def validation

        raise NoMoneyException if money > current_user.money
        raise ValidNumberException if number > warehouse.nventory
    end
end


begin
    Sale.create({...})
rescue NoMoneyException => e
    callback ..
rescue ValidNumberException => e
    callback ..
end
Comments

两个常用的shell脚本

  1. 日志切割

     #!/bin/sh
     log_path="/home/littlefire/var/www/apps/littlefire/current/log"
     to_day=`date -d "yesterday" +"%Y%m%d"`
     cp ${log_path}/production.log ${log_path}/production_${to_day}.log
     echo "" > ${log_path}/production.log
     chown littlefire:littlefire ${log_path}/production.log
     bzip2 ${log_path}/production_${to_day}.log
    
  2. mysql数据库定时备份

     #!/bin/sh
     #
     #发送Email的话要安装 mutt与msmtp才可以
    
     today=`date -d "today" +"%Y%m%d"`
     path=/home/kaka/data_backup/
     filename=backup_${today}.sql
     file_path=${path}${filename}
     tar_name=${path}backup_${today}.tar.gz
     /usr/bin/mysqldump -uroot -p** database_name > ${file_path}
    
     tar -zcvf $tar_name -C $path $filename
     rm $file_path
     find $path -mtime +6 -type f -name backup_*.tar.gz | xargs rm -rf 
     #echo "littefire data dump ${today}" | mutt -s "${today} dump data" ***@qq.com -a $tar_name
    
Comments

异常平台 Exceptional

visit: 官网

一个不错的统计项目的出错请求,而且支持多种语言,但是不是免费($9/month)

Rails 使用方式

  1. 在Gemfile配置

     gem exceptional
    
  2. 打包

     bundle install
    
  3. 生成exceptional的配置文件

     #YOUR-API-KEY 你申请的key
     bundle exec exceptional install YOUR-API-KEY
    
Comments

Ngrok反向代理工具

ngrok是个款不错的工具,在我们开发中经常要用到。

visit: 官网

使用方式

  1. 简单的运行

     // 代理你电脑3001端口, 运行后通过http://127.0.0.1:4040查看请求状态    
     ngrok 3001
    
  2. 验证运行

     // authtoken你在官方注册,就会生成一个, authtoken只要在电脑上运行一次就可以了会
     // 记录的, 如果你验证了的话ngrok 生成的前缀域名不会被占用和变化
     ngrok -authtoken U54UzZiDLFQxyZ-Ow*** 3001
    
  3. 需要别人验证才可以浏览

     // helmet用户名, 12345密码
     ngrok -httpauth="helmet:12345" 3001
    
  4. 设置前缀域名

     ngrok -subdomain=example 3001
    
  5. tcp代理

     ngrok -proto=tcp 22
    
  6. 指定其它服务器代理

     //为192.168.1.16服务器的3001端口代理
     ngrok 192.168.1.16:3001
    
  7. 运行多个类型服务

     // ngrok.yml配置类型如下
    
     tunnels:
       client:
         proto:
           https: "3000"
         auth: user:password
       c99bba1.ngrok.com:
         proto:
           http: "3000"
       ssh:
         proto:
           tcp: "22"
    
    
     // config: 配置文件路径
     // start: 运行哪几种类型服务(client, ssh c99bba1.ngrok.com)
     ngrok -log=stdout -authtoken=U54UzZiDLFQxyZ-Ow*** -config=ngrok.yml start client ssh c99bba1.ngrok.com
    
git
Comments

Git 错误 Object Is Empy / Corrupt

问题

早上上班开始工作是准备git push到远程服务器时,出现下面错误!我估计是之前的merge导致的

1
2
  error: object file .git/objects/xx/xxx is empty
  fatal: loose object xxx (stored in .git/objects/xx/xxx is corrupt

解决方案

参考链接, 其实我就是删除空的commit就可以了

find .git/objects/ -size 0 -exec rm -f {} \; 
Comments

Elasticsearch 动态加载属性的mapping

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  curl -XPUT 'localhost:9200/products' -d '{
    "settings": {
      "analysis": {
        "analyzer": {
          "pinyin_analyzer": {
            "tokenizer": "my_pinyin",
            "filter": ["standard", "nGram"]
          }
        },
        "tokenizer": {
          "my_pinyin": {
            "type": "pinyin",
            "first_letter": "prefix",
            "padding_char": ""
          }
        }
      }
    },
    "mappings": {
      "product": {
        "dynamic_templates": [
          {
            "property_template": {
              "path_match": "properties.*",
              "mapping": {
                "type": "multi_field",
                "fields": {
                  "{name}": {
                    "type": "string",
                    "analyzer" : "pinyin_analyzer",
                    "boost": 10,
                    "store": "no",
                    "term_vector": "with_positions_offsets"
                  },
                  "{name}_untouched": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              }
            }
          }
        ]
      }
    }
  }'

  curl -XPOST 'localhost:9200/products/product' -d '{name: "加多宝", properties: {ping: 1} }'
  curl -XPOST 'localhost:9200/products/product' -d '{name: "水杯", properties: {ge: 1} }'

  curl -XGET 'localhost:9200/products/product/_mapping?pretty'
Comments

Elasticsearch脚本排序

由于自己要自定义score, 所以才会使用script来写复杂的sort

javascript script 排序

1
bin/plugin -install elasticsearch/elasticsearch-lang-javascript/1.4.0
  • 写js脚本

在你的配置目录下新建文件scripts/products/sort.js, 我的配置目录是/etc/elasticsearch,简单实例:

1
2
3
(function(){
  doc._score.value + doc.likes.value
})()
  • 查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
curl -XGET 'http://localhost:9200/products/_search?pretty' -d '{
  "query": {
    "custom_score": {
      "script": "global_product",
      "query": {
        "filtered": {
          "filter": {
            "and": [
              {
                "term": {
                  "_type": "product"
                }
              }
            ]
          }
        }
      }
    }
  },
  "sort": [
    {
      "_score": "desc"
    }
  ]
}'

products_sort: products是目录sort脚本文件名

native script 排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
curl -X GET 'http://localhost:9200/products/_search?pretty' -d '{
  "query": {
    "custom_score": {
      "script": "productSort",
      "lang": "native",
      "query": {
        "filtered": {
          "filter": {
            "and": [
              {
                "term": {
                  "_type": "product"
                }
              }
            ]
          }
        }
      }
    }
  },
  "sort": [
    {
      "_score": "desc"
    }
  ]
}'

单机做测试,js与native排序性能方面有差距同样的5W数据,比较一下查询速度: js:70ms, native: 20ms

Comments

Elasticsearch 多个索引查询,根据不同类型条件判定

CURL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
curl -XGET 'http://localhost:9200/indexA,indexB/_search?pretty=1' -d '{
"query" : {
    "bool": {
      "should": [{
        "filtered": {
          "filter":{
            "and": [
              {
                "range":{
                  "start_time":{
                    "lte": "2013-09-24T00:00:00+08:00"
                  }
                }
              },{
                "range": {
                  "end_time": {
                    "gt": "2013-09-24T00:00:00+08:00"
                  }
                }
              },
              {
                "term": {
                  "_type": "TypeA"
                }
              }]
          }
        }
      },{
        "filtered": {
          "filter": {
            "term": {
              "_type": "TypeB"
            }
          }
        }
      }]
    }
  },
  "sort": [
    {
      "_script": {
        "script": "doc[\u0027score\u0027].value/((time()-doc[\u0027start_time_ms\u0027].value) / 3600)",
        "type": "number",
        "order": "desc"
      },
      "_score": {

      }
    }
  ]
}'

indexA 与 indexB 索引的 TypeA与TypeB

ruby gem Tire

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Tire.search ['indexA', 'indexB'] do
  from _from
  size _size

  query do
    boolean do
      should do
        filtered do
          filter :range, :end_time => {gt: toDay}
          filter :range, :start_time => {lte: toDay}
          filter :term, :_type => "TypeA"
          filter :term, :status => 1
          filter :terms, {"category.id" => category_ids} if q[:catalog_id].present?
        end
      end
      should do
        filtered do
          filter :term, {:_type => "TypeB"}
        end
      end

    end
  end

  sort("_script" => {
    :script => "doc['score'].value/((time()-doc['start_time_ms'].value) / 3600)",
    :type   => "number",
    :order  => "desc"
  }, "_score" => {})

end
Copyright © 2014 kaka