Skip to content
This repository
Browse code

Remove common logging boilerplate, and ensure every job doesn't forge…

…t to reconnect to the db.
  • Loading branch information...
commit 1ac7266b7f111cd9b358d174b1cb0d60f17c377d 1 parent 65f2192
Phil Crosby philc authored
10 lib/resque_job_helper.rb
@@ -4,6 +4,16 @@ module ResqueJobHelper
4 4 def self.included(klass) klass.extend(ClassMethods) end
5 5
6 6 module ClassMethods
  7 + attr_reader :logger
  8 +
  9 + # Called by most jobs to automate boilerplate and ensure we don't forget something important
  10 + # (like reconnecting to the DB).
  11 + def setup(log_file_name = nil)
  12 + log_file_name ||= "#{@queue}.log" # @queue will be something like :db_commit_ingest.
  13 + @logger = MetaRepo.logger = Logging.logger = Logging.create_logger(log_file_name)
  14 + reconnect_to_database
  15 + end
  16 +
7 17 # In any Resque worker, our SQL connection will be invalid because of the fork or if the connection
8 18 # has timed out.
9 19 def reconnect_to_database() DB[:users].select(1).first rescue nil end
5 resque_jobs/batch_comment_emails.rb
@@ -17,10 +17,7 @@ class BatchCommentEmails
17 17
18 18 # - filter_by_user_id: an optional parameter which is supplied by the integration tests only.
19 19 def self.perform(filter_by_user_id = nil)
20   - logger = Logging.logger = Logging.create_logger("batch_comment_emails.log")
21   - MetaRepo.logger = logger
22   -
23   - reconnect_to_database
  20 + setup
24 21
25 22 comments_dataset = Comment.filter(:has_been_emailed => false)
26 23 comments_dataset = comments_dataset.filter(:user_id => filter_by_user_id) if filter_by_user_id
6 resque_jobs/clone_new_repo.rb
... ... @@ -1,8 +1,10 @@
1 1 require "bundler/setup"
2 2 require "pathological"
3 3 require "lib/script_environment"
  4 +require "lib/resque_job_helper"
4 5
5 6 class CloneNewRepo
  7 + include ResqueJobHelper
6 8 @queue = :clone_new_repo
7 9
8 10 # We're willing to spend up to 5 minutes to clone a repo. This can be necessary for giant repos or
@@ -10,9 +12,7 @@ class CloneNewRepo
10 12 TIMEOUT = 5 * 60
11 13
12 14 def self.perform(repo_name, repo_url)
13   - logger = Logging.logger = Logging.create_logger("clone_new_repo.log")
14   - MetaRepo.logger = logger
15   -
  15 + setup
16 16 # This can take awhile if the repo is very large.
17 17 repo_path = File.join(REPOS_ROOT, repo_name)
18 18 if File.exists?(repo_name)
7 resque_jobs/db_commit_ingest.rb
@@ -15,12 +15,7 @@ class DbCommitIngest
15 15
16 16 # Called by Resque.
17 17 def self.perform(repo_name, remote_name)
18   - logger = Logging.logger = Logging.create_logger("db_commit_ingest.log")
19   - logger.info "Importing new commits from #{repo_name}:#{remote_name} into the database."
20   - MetaRepo.logger = logger
21   -
22   - reconnect_to_database
23   -
  18 + setup
24 19 MetaRepo.instance.scan_for_new_repos
25 20
26 21 # A value of 200 is not so useful when we're importing single new commits, but really useful when we're
5 resque_jobs/delete_old_comments_by_demo_users.rb
@@ -12,10 +12,7 @@ class DeleteOldCommentsByDemoUsers
12 12 @queue = :delete_old_comments_by_demo_users
13 13
14 14 def self.perform
15   - logger = Logging.logger = Logging.create_logger("delete_old_comments_by_demo_users.log")
16   -
17   - reconnect_to_database
18   -
  15 + setup
19 16 old_comments = Time.now - COMMENT_EXPIRATION
20 17 demo_users = User.filter(:permission => "demo").select(:id).all # There should only be one demo user.
21 18 return if demo_users.empty?
6 resque_jobs/delete_repo.rb
@@ -11,11 +11,7 @@ class DeleteRepo
11 11 TIMEOUT = 5 * 60
12 12
13 13 def self.perform(repo_name)
14   - logger = Logging.logger = Logging.create_logger("delete_repo.log")
15   - MetaRepo.logger = logger
16   -
17   - reconnect_to_database
18   -
  14 + setup
19 15 repo = GitRepo.first(:name => repo_name)
20 16 raise message "Error deleting repo: #{repo_name} does not exist in the database." if repo.nil?
21 17
5 resque_jobs/deliver_comment_emails.rb
@@ -11,10 +11,7 @@ class DeliverCommentEmails
11 11 @queue = :deliver_comment_emails
12 12
13 13 def self.perform(comment_ids)
14   - logger = Logging.logger = Logging.create_logger("deliver_comment_emails.log")
15   - MetaRepo.logger = logger
16   - reconnect_to_database
17   -
  14 + setup
18 15 MetaRepo.instance.scan_for_new_repos
19 16
20 17 comments = Comment.filter(:id => comment_ids).all
5 resque_jobs/deliver_commit_emails.rb
@@ -11,10 +11,7 @@ class DeliverCommitEmails
11 11 @queue = :deliver_commit_emails
12 12
13 13 def self.perform(repo_name, commit_sha)
14   - logger = Logging.logger = Logging.create_logger("deliver_commit_emails.log")
15   - MetaRepo.logger = logger
16   -
17   - reconnect_to_database
  14 + setup
18 15 MetaRepo.instance.scan_for_new_repos
19 16
20 17 commit = MetaRepo.instance.db_commit(repo_name, commit_sha)
5 resque_jobs/deliver_review_request_emails.rb
@@ -9,10 +9,7 @@ class DeliverReviewRequestEmails
9 9 @queue = :deliver_review_request_emails
10 10
11 11 def self.perform(repo_name, commit_sha, requester_email, emails)
12   - logger = Logging.logger = Logging.create_logger("deliver_review_request_emails.log")
13   - MetaRepo.logger = logger
14   -
15   - reconnect_to_database
  12 + setup
16 13 MetaRepo.instance.scan_for_new_repos
17 14
18 15 commit = MetaRepo.instance.db_commit(repo_name, commit_sha)
6 resque_jobs/fetch_commits.rb
@@ -15,12 +15,8 @@ class FetchCommits
15 15 FETCH_TIMEOUT = 10 # The per repo timeout, in seconds.
16 16
17 17 def self.perform
18   - logger = Logging.logger = Logging.create_logger("fetch_commits.log")
19   - MetaRepo.logger = logger
20   -
21   - reconnect_to_database
  18 + setup
22 19 MetaRepo.instance.scan_for_new_repos
23   -
24 20 fetch_commits(MetaRepo.instance.repos)
25 21 end
26 22
4 resque_jobs/generate_tagged_diffs.rb
@@ -12,12 +12,12 @@ class GenerateTaggedDiffs
12 12 @queue = :generate_tagged_diffs
13 13
14 14 def self.perform(repo_name, commit_sha)
15   - reconnect_to_database
  15 + setup
16 16 GitDiffUtils.setup(RedisManager.redis_instance)
17 17 MetaRepo.instance.scan_for_new_repos
18 18 grit_commit = MetaRepo.instance.get_grit_repo(repo_name).commits(commit_sha, 1).first
19 19 unless grit_commit
20   - puts "Error: this commit is not found: #{repo_name} #{commit_sha}"
  20 + logger.error "Error: this commit is not found: #{repo_name} #{commit_sha}"
21 21 return
22 22 end
23 23 GitDiffUtils::get_tagged_commit_diffs(repo_name, grit_commit, :warm_the_cache => true)

0 comments on commit 1ac7266

Please sign in to comment.
Something went wrong with that request. Please try again.