diff options
author | Stan Hu <stanhu@gmail.com> | 2016-05-09 00:21:10 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2016-05-12 07:00:19 -0500 |
commit | 7f7359b1381d376b81fd9b81120d8cfa0231a526 (patch) | |
tree | bec8110ed8c14fb0dc38ced1ebf21177dbc2acc5 /lib | |
parent | f0f1bbec8ad5d5fade7c0efeee22ba9b9bc44f07 (diff) | |
download | gitlab-shell-7f7359b1381d376b81fd9b81120d8cfa0231a526.tar.gz |
Use Redis Ruby client instead of shelling out to redis-cli
Closes gitlab-org/gitlab-ce#17329
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab_config.rb | 22 | ||||
-rw-r--r-- | lib/gitlab_net.rb | 19 | ||||
-rw-r--r-- | lib/gitlab_post_receive.rb | 10 |
3 files changed, 25 insertions, 26 deletions
diff --git a/lib/gitlab_config.rb b/lib/gitlab_config.rb index 831f0e3..ebf72d6 100644 --- a/lib/gitlab_config.rb +++ b/lib/gitlab_config.rb @@ -54,26 +54,4 @@ class GitlabConfig def git_annex_enabled? @config['git_annex_enabled'] ||= false end - - # Build redis command to write update event in gitlab queue - def redis_command - if redis.empty? - # Default to old method of connecting to redis - # for users that haven't updated their configuration - %W(env -i redis-cli) - else - redis['database'] ||= 0 - redis['host'] ||= '127.0.0.1' - redis['port'] ||= '6379' - if redis.has_key?("socket") - %W(#{redis['bin']} -s #{redis['socket']} -n #{redis['database']}) - else - if redis.has_key?("pass") - %W(#{redis['bin']} -h #{redis['host']} -p #{redis['port']} -n #{redis['database']} -a #{redis['pass']}) - else - %W(#{redis['bin']} -h #{redis['host']} -p #{redis['port']} -n #{redis['database']}) - end - end - end - end end diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb index 8b6d33b..8e1fe39 100644 --- a/lib/gitlab_net.rb +++ b/lib/gitlab_net.rb @@ -1,6 +1,7 @@ require 'net/http' require 'openssl' require 'json' +require 'redis' require_relative 'gitlab_config' require_relative 'gitlab_logger' @@ -63,6 +64,24 @@ class GitlabNet nil end + def redis_client + redis_config = config.redis + database = redis_config['database'] || 0 + params = { + host: redis_config['host'] || '127.0.0.1', + port: redis_config['port'] || 6379, + db: database + } + + if redis_config.has_key?("socket") + params = { path: redis_config['socket'], db: database } + elsif redis_config.has_key?("pass") + params[:password] = redis_config['pass'] + end + + Redis.new(params) + end + protected def config diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb index 0fff479..8632432 100644 --- a/lib/gitlab_post_receive.rb +++ b/lib/gitlab_post_receive.rb @@ -2,6 +2,7 @@ require_relative 'gitlab_init' require_relative 'gitlab_net' require 'json' require 'base64' +require 'redis' require 'securerandom' class GitlabPostReceive @@ -74,11 +75,12 @@ class GitlabPostReceive queue = "#{config.redis_namespace}:queue:post_receive" msg = JSON.dump({ 'class' => 'PostReceive', 'args' => [@repo_path, @actor, changes], 'jid' => @jid }) - if system(*config.redis_command, 'rpush', queue, msg, - err: '/dev/null', out: '/dev/null') + + begin + GitlabNet.new.redis_client.rpush(queue, msg) return true - else - puts "GitLab: An unexpected error occurred (redis-cli returned #{$?.exitstatus})." + rescue => e + puts "GitLab: An unexpected error occurred in writing to Redis: #{e}" return false end end |