-
-
Notifications
You must be signed in to change notification settings - Fork 71
Fix scheduled jobs #2001
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
Fix scheduled jobs #2001
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0446e51
rm old jobs and update existing to perform_now
ArtOfCode- 53ec03b
Move recalc abilities to a job
ArtOfCode- c8b27ed
Move uncaptured donations to a job
ArtOfCode- 4f45298
Rubocop
ArtOfCode- 1d97814
Add argument to job
ArtOfCode- c6281e2
Parameterise initialize
ArtOfCode- 5f81af3
Use a hash instead
ArtOfCode- 9393ecf
Copy master key for credentials
ArtOfCode- a135a4a
Allow net connections for Stripe test
ArtOfCode- b02ccef
Skip test if no API key
ArtOfCode- 833786a
fixed missing EoF newlines
Oaphi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| class MailUncapturedDonationsJob < ApplicationJob | ||
| queue_as :default | ||
|
|
||
| def perform(*_args) | ||
| intents = Stripe::PaymentIntent.list | ||
| mailed = 0 | ||
| errors = 0 | ||
| intents.auto_paging_each do |pi| | ||
| break unless Time.at(pi.created) >= 24.hours.ago | ||
| next unless pi.status == 'requires_payment_method' | ||
| next unless pi.metadata['user_id'].present? | ||
| next if pi.metadata['emailed'].present? | ||
|
|
||
| user = User.find(pi.metadata['user_id']) | ||
| symbol = { 'GBP' => '£', 'USD' => '$', 'EUR' => '€' }[pi.currency.upcase] | ||
| amount = pi.amount / 100 | ||
| DonationMailer.with(currency: symbol, amount: amount, email: user.email, name: user.username, intent: pi) | ||
| .donation_uncaptured.deliver_now | ||
| Stripe::PaymentIntent.update(pi.id, { metadata: { emailed: true } }) | ||
| logger.debug "Mailed ##{user.id} for PaymentIntent #{pi.id}" | ||
| mailed += 1 | ||
| rescue => e | ||
| Stripe::PaymentIntent.update(pi.id, { metadata: { email_error: e.message.to_s } }) | ||
| logger.error "Error sending email for user #{user.id}, PaymentIntent #{pi.id}: #{e.message}" | ||
| errors += 1 | ||
| end | ||
| logger.info "Mailed #{mailed} donations, #{errors} errors" | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| class RecalcAbilitiesJob < ApplicationJob | ||
| queue_as :default | ||
|
|
||
| ## | ||
| # Perform ability recalculations. | ||
| # @param options [Hash] additional options for the job. Currently supports +verbose+ and +quiet+. | ||
| def perform(**options) | ||
| resolved = [] | ||
| destroy = [] | ||
| all = AbilityQueue.pending.to_a | ||
|
|
||
| all.each do |q| | ||
| cu = q.community_user | ||
| u = cu&.user | ||
|
|
||
| if cu.nil? || u.nil? | ||
| destroy << q.id | ||
| next | ||
| end | ||
|
|
||
| RequestContext.community = cu.community | ||
|
|
||
| if options[:verbose] && !options[:quiet] | ||
| logger.debug "Scope: Community : #{cu.community.name} (#{cu.community.host})" | ||
| logger.debug " User : #{u.username} (#{cu.user_id})" | ||
| logger.debug " CommunityUser : #{cu.id}" | ||
| elsif !options[:verbose] && !options[:quiet] | ||
| logger.debug "Scope: CommunityUser : #{cu.id}" | ||
| end | ||
|
|
||
| cu.recalc_abilities! | ||
|
|
||
| # Grant mod ability if mod status is given | ||
| if cu.at_least_moderator? && !cu.ability?('mod') | ||
| cu.grant_ability!('mod') | ||
| end | ||
|
|
||
| resolved << q.id | ||
| rescue => e | ||
| logger.error " Failed: #{e.class.name}: #{e.message}" | ||
| logger.error e.backtrace | ||
|
|
||
| if Rails.env.test? | ||
| raise e | ||
| end | ||
| end | ||
|
|
||
| AbilityQueue.where(id: resolved).update(completed: true) | ||
| AbilityQueue.where(id: destroy).delete_all | ||
|
|
||
| unless options[:quiet] | ||
| logger.info "Completed, #{resolved.size}/#{all.size} tasks successful, #{destroy.size} tasks invalid" | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| SA7h7Xm7xF3AEvje/xZiPMzem0wh2CysuvvB+iM6R0Epkg+z+vZG2oxI/9tVzDGELWtwoNDPeqNa0bUbGjo6gt7xyEpmQbivEVKnwcbVK+1bxPHKke+hveVEUhmY/axFIIpty+VTnZVnrJkZ35/rW9egcRr5O2HikuAuBz8KrYLvB2w3h8EdmcIbbfjMvKWlUVvc8D9t3v+k56B1CbTghTmGeG0NLijWAA+U+pDmErPOR9JUAVY/nbF2/T8aQ5Szameuoj88Pk5P6alFDHwlWUSHhuEY5Z7A5LKM1NtzlTGc+CAcTVzKvx6DCg8WUJugy3uS7Bpp6uNj9TvJoZzrb0i5G1nDmIl7tK2GCNclp3JFEklrUJIrF4asRuDQUzdvuPeTEXNyi2cc5d6hSBG+MwEvG90P6n0td5+gv+rr1FCKuzP5ftyxqPsnvZuBKRxNSw0+VTTXhTO73jcwttHYrD9Jwc9tVbCVqegwsEO0dB0sNL6M89rudDMbpPnz7rJXr6lXD4Az3qJ/0N3ubYDwiuGOk7UHDntm4Id4BFDm5qeLeOUg1n2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wqPy/oW/jMDP8OvDZuWtNwpxIFJYT7sKdvIvEvjrPR+0pyTVsVMZomTiUIcFC/IA9KV+DVjtj+Tfy2UZH4W8AU6i8hObiSs4CbT081L2AHc0ZcLKMI/bh8+vBay4J2UB6vcBbpNbAaNPyj7bXsrM0+YxpPJidgrEMyoVt+fIiA5H/kXhp8WV3Jkr2eEriq5z9+m6IZFSFs58i06hN0ZH5WHUdhJcVIgMBiLOex4SVd9zL6NW2y+ZK9Fs27z8u22zn+51oQHcpz2wbmVV3sGBYaFLFinqm3/HoW60eAcvM4SfXErz0fwIzq7yN7XIY9q5/6E0hhAdHZQqVlEJWOhUAIH9ik+Ga90gWLfkkoym/G1PIUYnmeJtLUKYyj94b+YdxQNkgzGtJA7FCGVmNUuy8wCw0OIG1hg3z9TSg5mbN7ZNhBRVDvKQENJqg9tFXM0MgXn+lQ0v+io4ds6Ist2BGr4gJIz5R7bRFOIG6G60/+ZEfPBjNcj5rrGYDBATmqKo9myaSLQ68WyJB37Fs3CchM4lAdoVbXnEyJwP8HGRaips+ZhjBVvSs/8w7yQteBswFrc00+gts5TpqCz3AjNNMsHTP5cr5IhqoNWdpxvFgHHLBAPtn0HWJ5C3OP3X/4GhxG3G9Z99pxbH1ZjWn1vnKadKUfY4vzPYCZsMaokTr2/XuL8wROLohCD53SVATsGsbiL947u+X6WYEZVuOH2IY5xMhTO9JW8DYb0Rm31Te8oOpzoIrz9Ck70YDQcADiNg4SEeLTQlpve40C5+YI5Xr+60GbOHRHSGv31ENnubVTCTv3q6otE0iqCOAQ62RdqhzT23aaz0NuH5DtLIlWn/3ev9Js6mm1rnF7AzVKey01IsS--p3woHV6iIC4KuPxP--fRXQKncEML1whOWmVm2Z8w== |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| CleanupVotesJob.perform_later | ||
| CleanupVotesJob.perform_now |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1 @@ | ||
| intents = Stripe::PaymentIntent.list | ||
| intents.auto_paging_each do |pi| | ||
| begin | ||
| break unless Time.at(pi.created) >= 24.hours.ago | ||
| next unless pi.status == 'requires_payment_method' | ||
| next unless pi.metadata['user_id'].present? | ||
| next if pi.metadata['emailed'].present? | ||
| user = User.find(pi.metadata['user_id']) | ||
| symbol = { 'GBP' => '£', 'USD' => '$', 'EUR' => '€' }[pi.currency.upcase] | ||
| amount = pi.amount / 100 | ||
| DonationMailer.with(currency: symbol, amount: amount, email: user.email, name: user.username, intent: pi) | ||
| .donation_uncaptured.deliver_now | ||
| Stripe::PaymentIntent.update(pi.id, { metadata: { emailed: true } }) | ||
| puts "Mailed ##{user.id} for PaymentIntent #{pi.id}" | ||
| rescue => ex | ||
| Stripe::PaymentIntent.update(pi.id, { metadata: { email_error: "#{ex.message}" } }) | ||
| end | ||
| end | ||
| MailUncapturedDonations.perform_now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,58 +1,15 @@ | ||
| options = OpenStruct.new | ||
| options = {} | ||
|
|
||
| OptionParser.new do |opts| | ||
| opts.banner = "Usage: rails r scripts/recalc_abilities.rb [options]" | ||
|
|
||
| opts.on('-v', '--verbose', 'Run with additional logging') do | ||
| options.verbose = true | ||
| options[:verbose] = true | ||
| end | ||
|
|
||
| opts.on('-q', '--quiet', 'Run with minimal logging') do | ||
| options.quiet = true | ||
| options[:quiet] = true | ||
| end | ||
| end.parse! | ||
|
|
||
| resolved = [] | ||
| destroy = [] | ||
| all = AbilityQueue.pending.to_a | ||
|
|
||
| all.each do |q| | ||
| begin | ||
| cu = q.community_user | ||
| u = cu&.user | ||
|
|
||
| if cu.nil? || u.nil? | ||
| destroy << q.id | ||
| next | ||
| end | ||
|
|
||
| RequestContext.community = cu.community | ||
|
|
||
| if options.verbose && !options.quiet | ||
| puts "Scope: Community : #{cu.community.name} (#{cu.community.host})" | ||
| puts " User : #{u.username} (#{cu.user_id})" | ||
| puts " CommunityUser : #{cu.id}" | ||
| elsif !options.verbose && !options.quiet | ||
| puts "Scope: CommunityUser : #{cu.id}" | ||
| end | ||
|
|
||
| cu.recalc_abilities | ||
|
|
||
| # Grant mod ability if mod status is given | ||
| if cu.at_least_moderator? && !cu.ability?('mod') | ||
| cu.grant_ability!('mod') | ||
| end | ||
|
|
||
| resolved << q.id | ||
| rescue => e | ||
| $stderr.puts " Failed: #{e.class.name}: #{e.message}" | ||
| $stderr.puts e.backtrace | ||
| end | ||
| end | ||
|
|
||
| AbilityQueue.where(id: resolved).update(completed: true) | ||
| AbilityQueue.where(id: destroy).delete_all | ||
|
|
||
| unless options.quiet | ||
| puts "Completed, #{resolved.size}/#{all.size} tasks successful, #{destroy.size} tasks invalid" | ||
| end | ||
| RecalcAbilitiesJob.perform_now(options) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| AutoCloseComplaintsJob.perform_later | ||
| AutoCloseComplaintsJob.perform_now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| CleanUpNewThreadFollowersJob.perform_later | ||
| CleanUpNewThreadFollowersJob.perform_now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| CleanUpSpammyUsersJob.perform_later | ||
| PotentialSpamProfilesJob.perform_later | ||
| CleanUpSpammyUsersJob.perform_now | ||
| PotentialSpamProfilesJob.perform_now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| SendSummaryEmailsJob.perform_later | ||
| SendSummaryEmailsJob.perform_now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| CleanUpThreadFollowersJob.perform_later | ||
| CleanUpThreadFollowersJob.perform_now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| require 'test_helper' | ||
|
|
||
| class MailUncapturedDonationsJobTest < ActiveJob::TestCase | ||
| setup do | ||
| WebMock.allow_net_connect! | ||
| end | ||
|
|
||
| teardown do | ||
| WebMock.disable_net_connect! | ||
| end | ||
|
|
||
| test 'should run job successfully' do | ||
| skip unless Stripe.api_key | ||
| WebMock.allow_net_connect! | ||
| perform_enqueued_jobs do | ||
| MailUncapturedDonationsJob.perform_later | ||
| end | ||
| assert_performed_jobs 1 | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| require 'test_helper' | ||
|
|
||
| class RecalcAbilitiesJobTest < ActiveJob::TestCase | ||
| test 'should run job successfully' do | ||
| perform_enqueued_jobs do | ||
| RecalcAbilitiesJob.perform_later(verbose: false, quiet: true) | ||
| end | ||
| assert_performed_jobs 1 | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: I don't think we need to parametrize it all, do we? We don't use the param anyway, so let's avoid creating mental overhead for those less familiar with the syntax.