Skip to content

Restoring previously scheduled tasks. #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions app/clock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,44 @@

include Clockwork

# On the first of every month send the popular protips from the previous month.
every(1.day, 'protip_mailer:popular_protips', if: ->(t){ t.day == 1 }) do
if ENV['PROTIP_MAILER_POPULAR_PROTIPS']
last_month = 1.month.ago
ProtipMailerPopularProtipsWorker.perform_async(last_month.beginning_of_month, last_month.end_of_month)
else
Rails.logger.warn('PROTIP_MAILER_POPULAR_PROTIPS is disabled. Set `heroku config:set PROTIP_MAILER_POPULAR_PROTIPS=true` to allow sending scheduled emails.')
end
end

# Runs as 1:01 AM Pacific
every(1.day, 'award:activate:active', at: '01:01') do
ActivatePendingUsersWorker.perform_async
every(1.day, 'teams:refresh', at: '22:00') do
TeamsRefreshJob.perform_async
end

every(1.day, 'award:refresh:stale', at: '00:00') do
RefreshStaleUsersWorker.perform_async
end

# On the first of every month send the popular protips from the previous month.
every(1.day, 'protip_mailer:popular_protips', if: ->(t){ t.day == 1 }) do
last_month = 1.month.ago
ProtipMailerPopularProtipsWorker.perform_async(last_month.beginning_of_month, last_month.end_of_month)
# Runs as 1:00 AM Pacific
every(1.day, 'award:activate:active', at: '01:00') do
ActivatePendingUsersWorker.perform_async
end

every(1.day, 'cleanup:protips:associate_zombie_upvotes', at: '02:00') do
CleanupProtipsAssociateZombieUpvotesJob.perform_async
end

every(1.day, 'search:sync', at: '03:00') do
SearchSyncJob.perform_async
end

every(1.day, 'protips:recalculate_scores', at: '04:00') do
ProtipsRecalculateScoresJob.perform_async
end

every(1.day, 'clear_expired_sessions', at: '05:00') do
ClearExpiredSessionsJob.perform_async
end

every(1.day, 'cleanup:protips:associate_zombie_upvotes', at: '00:00') {}
every(1.day, 'clear_expired_sessions', at: '00:00') {}
every(1.day, 'facts:system', at: '00:00') {}
every(1.day, 'protips:recalculate_scores', at: '00:00') {}
every(1.day, 'search:sync', at: '00:00') {}
every(1.day, 'teams:refresh', at: '00:00') {}
# This is tied with broken code. Probably should delete
# every(1.day, 'facts:system', at: '00:00') {}
4 changes: 2 additions & 2 deletions app/jobs/award_user_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AwardUserJob
sidekiq_options queue: :low

def perform(username, badges)
user = User.find_by_username(username)
user = User.with_username(username)

if badges.first.is_a?(String)
badges.map!(&:constantize)
Expand All @@ -13,4 +13,4 @@ def perform(username, badges)
user.check_achievements!(badges)
end

end
end
4 changes: 2 additions & 2 deletions app/jobs/build_activity_stream_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class BuildActivityStreamJob
sidekiq_options queue: :medium

def perform(username)
user = User.find_by_username(username)
user = User.with_username(username)
user.build_repo_followed_activity!
end
end
end
14 changes: 14 additions & 0 deletions app/jobs/cleanup_protips_associate_zombie_upvotes_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CleanupProtipsAssociateZombieUpvotesJob
include Sidekiq::Worker

sidekiq_options queue: :low

def perform
Like.joins('inner join users on users.tracking_code = likes.tracking_code').
where('likes.tracking_code is not null').
where(user_id: nil).
find_each(batch_size: 100) do |like|
ProcessLikeJob.perform_async(:associate_to_user, like.id)
end
end
end
9 changes: 9 additions & 0 deletions app/jobs/clear_expired_sessions_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ClearExpiredSessionsJob
include Sidekiq::Worker

sidekiq_options queue: :low

def perform
ActiveRecord::SessionStore::Session.delete_all(["updated_at < ?", 7.days.ago])
end
end
1 change: 0 additions & 1 deletion app/jobs/deactivate_team_jobs_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ def perform(id)
job.deactivate!
end
end

end
2 changes: 2 additions & 0 deletions app/jobs/generate_top_users_composite_job.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODO: Broken, generates errors due to changes in `convert` (ImageMagick)

class GenerateTopUsersCompositeJob
include Sidekiq::Worker

Expand Down
4 changes: 2 additions & 2 deletions app/jobs/github_badge_org_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class GithubBadgeOrgJob
sidekiq_options queue: :medium

def perform(username, action)
user = User.find_by_username(username)
user = User.with_username(username)
unless user.nil? or user.github.nil?
if action.to_sym == :add
GithubBadge.new.add_all(user.badges, user.github)
Expand All @@ -13,4 +13,4 @@ def perform(username, action)
end
end
end
end
end
7 changes: 4 additions & 3 deletions app/jobs/process_like_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ def perform(process_type, like_id)
when 'associate_to_user'
begin
like.user_id = User.find_by_tracking_code(like.tracking_code)
like.save
rescue ActiveRecord::RecordNotUnique
like.save!
rescue ActiveRecord::RecordNotUnique => ex
ap ex
like.destroy
end
end
end
end
end
11 changes: 11 additions & 0 deletions app/jobs/protips_recalculate_scores_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ProtipsRecalculateScoresJob
include Sidekiq::Worker

sidekiq_options queue: :low

def perform
Protip.where('created_at > ?', 25.hours.ago).where(upvotes_value_cache: nil).each do |protip|
ProcessProtipJob.perform_async(:recalculate_score, protip.id)
end
end
end
33 changes: 33 additions & 0 deletions app/jobs/search_sync_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class SearchSyncJob
include Sidekiq::Worker

sidekiq_options queue: :medium

def perform
number_of_protips_in_index = Protip.tire.search { query { all } }.total
number_of_protips_in_database = Protip.count

if number_of_protips_in_index != number_of_protips_in_database
protips_in_index = Protip.tire.search do
size number_of_protips_in_index
query { all }
end.map { |protip| protip.id.to_i }

protips_in_database = Protip.select(:id).map(&:id)

#now that we know the sets in db and index, calculate the missing records
nonexistent_protips = (protips_in_index - protips_in_database)
unindexed_protips = (protips_in_database - protips_in_index)

nonexistent_protips.each do |nonexistent_protip_id|
Protip.index.remove({'_id' => nonexistent_protip_id, '_type' => 'protip'})
end

unindexed_protips.each do |unindexed_protip_id|
IndexProtip.perform_async(unindexed_protip_id)
end

puts "removed #{nonexistent_protips.count} protips and added #{unindexed_protips.count} protips"
end
end
end
2 changes: 1 addition & 1 deletion app/jobs/seed_github_protips_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SeedGithubProtipsJob
sidekiq_options queue: :low

def perform(username)
user = User.find_by_username(username)
user = User.with_username(username)
user.build_github_proptips_fast
end
end
11 changes: 11 additions & 0 deletions app/jobs/teams_refresh_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class TeamsRefreshJob
include Sidekiq::Worker

sidekiq_options queue: :low

def perform
Team.all.each do |team|
ProcessTeamJob.perform_async('recalculate', team.id)
end
end
end
41 changes: 20 additions & 21 deletions app/models/badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ class Badge < ActiveRecord::Base

scope :of_type, ->(badge) { where(badge_class_name: badge.class.name) }

class << self
def rename(old_class_name, new_class_name)
Badge.where(badge_class_name: old_class_name).map { |badge| badge.update_attribute(:badge_class_name, new_class_name) }
Fact.where('metadata LIKE ?', "%#{old_class_name}%").each do |fact|
if fact.metadata[:award] == old_class_name
fact.metadata[:award] = new_class_name
end
fact.save
def self.rename(old_class_name, new_class_name)
Badge.where(badge_class_name: old_class_name).map { |badge| badge.update_attribute(:badge_class_name, new_class_name) }

Fact.where('metadata LIKE ?', "%#{old_class_name}%").each do |fact|
if fact.metadata[:award] == old_class_name
fact.metadata[:award] = new_class_name
end
ApiAccess.where('awards LIKE ?', "%#{old_class_name}%").each do |api_access|
if api_access.awards.delete(old_class_name)
api_access.awards << new_class_name
end
api_access.save
fact.save
end

ApiAccess.where('awards LIKE ?', "%#{old_class_name}%").each do |api_access|
if api_access.awards.delete(old_class_name)
api_access.awards << new_class_name
end
api_access.save
end
end

Expand All @@ -41,12 +41,12 @@ def visible?

def tokenized_skill_name
@tokenized_skill_name ||= begin
if badge_class.respond_to?(:skill)
Skill.tokenize(badge_class.skill)
else
''
end
end
if badge_class.respond_to?(:skill)
Skill.tokenize(badge_class.skill)
else
''
end
end
end

def next
Expand Down Expand Up @@ -89,13 +89,12 @@ def generate_event
def to_event_hash
{ achievement: { name: self.display_name, description: (self.try(:for) || self.try(:description)), percentage_of_achievers: self.percent_earned,
achiever: { first_name: self.user.short_name }, image_path: self.image_path },
user: { username: self.user.username } }
user: { username: self.user.username } }
end

def event_type
:unlocked_achievement
end

end

# == Schema Information
Expand Down
1 change: 1 addition & 0 deletions app/models/badges/changelogd.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# TODO: broken
!class Changelogd < BadgeBase
describe "Changelog'd",
skill: 'Open Source',
Expand Down
4 changes: 3 additions & 1 deletion app/models/protip.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

require 'net_validators'
require 'open-uri'
require 'cfm'
Expand Down Expand Up @@ -785,7 +787,7 @@ def images
end

def retrieve_title_from_html(html)
Nokogiri::XML.fragment(html.xpath("//title").map(&:text).join).text.force_encoding('ASCII-8BIT').gsub(/\P{ASCII}/, '')
Nokogiri::XML.fragment(html.xpath("//title").map(&:text).join).text.gsub(/\P{ASCII}/, '')
end

def upvote_ancestor(link_identifier, link)
Expand Down
16 changes: 7 additions & 9 deletions app/models/skill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ class Skill < ActiveRecord::Base

default_scope where(deleted: false)

class << self
def tokenize(value)
v = value.to_s.gsub('&', 'and').downcase.gsub(/\s|\./, BLANK)
v = 'nodejs' if v == 'node'
Sanitize.clean(v)
end
def self.tokenize(value)
v = value.to_s.gsub('&', 'and').downcase.gsub(/\s|\./, BLANK)
v = 'nodejs' if v == 'node'
Sanitize.clean(v)
end

def deleted?(user_id, skill_name)
Skill.with_deleted.where(user_id: user_id, name: skill_name, deleted: true).any?
end
def self.deleted?(user_id, skill_name)
Skill.with_deleted.where(user_id: user_id, name: skill_name, deleted: true).any?
end

def merge_with(another_skill)
Expand Down
2 changes: 1 addition & 1 deletion app/views/protip_mailer/popular_protips.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

%tr.tip{style: nopad}
%td.avatar{style: "#{nopad} padding-left: 30px; width: 36px; padding-bottom: 20px;"}
%img{alt: "Avatar", height: 36, src: image_url(users_image_path(protip.user)), style: "#{nopad} border: solid 2px #fff; -webkit-box-shadow: 0px 1px 1px 1px rgba(0, 0, 0, 0.1); box-shadow: 0px 1px 1px 1px rgba(0, 0, 0, 0.1);", width: 36}
%img{alt: "Avatar", height: 36, src: image_url(protip.user.avatar.url), style: "#{nopad} border: solid 2px #fff; -webkit-box-shadow: 0px 1px 1px 1px rgba(0, 0, 0, 0.1); box-shadow: 0px 1px 1px 1px rgba(0, 0, 0, 0.1);", width: 36}
%td.link{style: "#{nopad} padding-right: 20px; padding-left: 10px; width: 270px; padding-bottom: 20px;"}
%a{href: protip_url(protip.public_id, @issue), style: "#{nopad} #{sans_serif} font-size: 14px; line-height: 22px; color: #48494E; text-decoration: none; display: block;"}
= protip.title
Expand Down
1 change: 0 additions & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
config.assets.digest = true
config.static_cache_control = 'public, max-age=31536000'
config.host = ENV['HOST_DOMAIN']
config.logger.level = Logger.const_get(ENV['LOG_LEVEL'] ? ENV['LOG_LEVEL'].upcase : 'INFO')
end
4 changes: 1 addition & 3 deletions lib/awards.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

module Awards

def award_from_file(filename)
text = File.read(filename)

unless text.nil?
csv = CSV.parse(text, headers: false)
csv.each do |row|
Expand Down
Loading