Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Begain work. Finished most of the pushing to redis
  • Loading branch information
thesecretmaster committed Dec 6, 2018
1 parent fc346df commit 4e9988e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
1 change: 0 additions & 1 deletion app/controllers/github_controller.rb
Expand Up @@ -207,7 +207,6 @@ def any_status_hook
return if state == 'pending' || (state == 'success' && context == 'github/pages')

if context.start_with? 'ci/circleci'
redis = Redis.new
if state == 'success'
redis.incr "successful_ci_count/#{sha}"
redis.expire "successful_ci_count/#{sha}", 1200
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/posts_controller.rb
Expand Up @@ -134,12 +134,28 @@ def create
# Start autoflagging
Rails.logger.warn "[autoflagging] #{@post.id}: post.save succeeded"

post = {
body: @post.body,
title: @post.title,
reason_weight: @post.reasons.map(&:weight).reduce(:+),
created_at: @post.created_at,
username: @post.username,
stack_exchange_user_id: @post.stack_exchange_user.id
}
redis.hmset("posts/#{@post.id}", *post.to_a)

reason_names = @post.reasons.map(&:reason_name)
redis.lpush "posts/#{@post.id}/reasons", *reason_names

redis.lpush "posts" "#{@post.id}"

Thread.new do
Rails.logger.warn "[autoflagging] #{@post.id}: thread begin"
# Trying to autoflag in a different thread while in test
# can cause race conditions and segfaults. This is bad,
# so we completely suppress the issue and just don't do that.
@post.autoflag unless Rails.env.test?
redis.hset("posts/#{@post.id}", "flagged?", @post.flagged?)
end

format.json { render status: :created, plain: 'OK' }
Expand Down
9 changes: 7 additions & 2 deletions app/models/deletion_log.rb
Expand Up @@ -20,8 +20,13 @@ class DeletionLog < ApplicationRecord
end
end

after_create do
post.update(deleted_at: created_at) if is_deleted && post.deleted_at.nil?
after_create :update_deletion_data

def update_deletion_data
if is_deleted && post.deleted_at.nil?
post.update(deleted_at: created_at)
redis.hset("posts/#{post.id}", "deleted_at", created_at.to_s)
end
end

after_create do
Expand Down
12 changes: 12 additions & 0 deletions app/models/feedback.rb
Expand Up @@ -25,6 +25,18 @@ class Feedback < ApplicationRecord

validates :feedback_type, inclusion: { in: VALID_TYPES }

after_save :populate_redis

def populate_redis
redis.zadd "post/#{post.id}/feedbacks", 0, id.to_s
redis.hmset "feedbacks/#{id}", {
feedback_type: feedback_type,
username: user.try(:username) || user_name,
app_name: api_key.try(:app_name),
invalidated: is_invalidated
}
end

after_save do
if update_post_feedback_cache # if post feedback cache was changed
if post.flagged? && !is_positive?
Expand Down
20 changes: 20 additions & 0 deletions app/models/post.rb
Expand Up @@ -66,6 +66,26 @@ def reject_recent_duplicates
errors.add(:base, "Reported in the last 5 minutes by a different instance: #{conflict.id}")
end

def populate_redis
post = {
body: body,
title: title,
reason_weight: reasons.map(&:weight).reduce(:+),
created_at: created_at,
username: username,
stack_exchange_user_id: stack_exchange_user.id,
flagged: flagged?
}
redis.hmset("posts/#{id}", *post.to_a)

reason_names = reasons.map(&:reason_name)
redis.zadd("posts/#{id}/reasons", *with_no_score(reason_names))

redis.zadd("posts", *with_no_score("#{id}"))
feedbacks.each(&:populate_redis)
deletion_logs.each(&:update_deletion_data)
end

def update_feedback_cache
feedbacks = self.feedbacks.to_a

Expand Down
9 changes: 9 additions & 0 deletions config/initializers/redis.rb
@@ -0,0 +1,9 @@
require 'redis'

def redis
@redis ||= Redis.new
end

def with_no_score(ary)
ary.zip(ary.length.times.map { 0 }).flatten
end

0 comments on commit 4e9988e

Please sign in to comment.