Skip to content

Commit 40b9dea

Browse files
authored
Merge pull request #2001 from codidact/art/jobs-again
Fix scheduled jobs
2 parents 84435d4 + 833786a commit 40b9dea

20 files changed

Lines changed: 137 additions & 152 deletions

.github/workflows/ci-cd.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ jobs:
8787
bundler-cache: true
8888
- name: Prepare for testing
8989
run: |
90+
echo "${{ secrets.MASTER_KEY }}" > config/master.key
9091
cp config/database.sample.yml config/database.yml
9192
cp config/storage.sample.yml config/storage.yml
9293
bundle exec rails db:create

app/jobs/application_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class ApplicationJob < ActiveJob::Base
55
# Most jobs are safe to ignore if the underlying records are no longer available
66
discard_on ActiveJob::DeserializationError
77

8-
def initialize
8+
def initialize(*args, **opts)
99
@job_id = SecureRandom.uuid
1010
super
1111
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class MailUncapturedDonationsJob < ApplicationJob
2+
queue_as :default
3+
4+
def perform(*_args)
5+
intents = Stripe::PaymentIntent.list
6+
mailed = 0
7+
errors = 0
8+
intents.auto_paging_each do |pi|
9+
break unless Time.at(pi.created) >= 24.hours.ago
10+
next unless pi.status == 'requires_payment_method'
11+
next unless pi.metadata['user_id'].present?
12+
next if pi.metadata['emailed'].present?
13+
14+
user = User.find(pi.metadata['user_id'])
15+
symbol = { 'GBP' => '£', 'USD' => '$', 'EUR' => '€' }[pi.currency.upcase]
16+
amount = pi.amount / 100
17+
DonationMailer.with(currency: symbol, amount: amount, email: user.email, name: user.username, intent: pi)
18+
.donation_uncaptured.deliver_now
19+
Stripe::PaymentIntent.update(pi.id, { metadata: { emailed: true } })
20+
logger.debug "Mailed ##{user.id} for PaymentIntent #{pi.id}"
21+
mailed += 1
22+
rescue => e
23+
Stripe::PaymentIntent.update(pi.id, { metadata: { email_error: e.message.to_s } })
24+
logger.error "Error sending email for user #{user.id}, PaymentIntent #{pi.id}: #{e.message}"
25+
errors += 1
26+
end
27+
logger.info "Mailed #{mailed} donations, #{errors} errors"
28+
end
29+
end

app/jobs/recalc_abilities_job.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class RecalcAbilitiesJob < ApplicationJob
2+
queue_as :default
3+
4+
##
5+
# Perform ability recalculations.
6+
# @param options [Hash] additional options for the job. Currently supports +verbose+ and +quiet+.
7+
def perform(**options)
8+
resolved = []
9+
destroy = []
10+
all = AbilityQueue.pending.to_a
11+
12+
all.each do |q|
13+
cu = q.community_user
14+
u = cu&.user
15+
16+
if cu.nil? || u.nil?
17+
destroy << q.id
18+
next
19+
end
20+
21+
RequestContext.community = cu.community
22+
23+
if options[:verbose] && !options[:quiet]
24+
logger.debug "Scope: Community : #{cu.community.name} (#{cu.community.host})"
25+
logger.debug " User : #{u.username} (#{cu.user_id})"
26+
logger.debug " CommunityUser : #{cu.id}"
27+
elsif !options[:verbose] && !options[:quiet]
28+
logger.debug "Scope: CommunityUser : #{cu.id}"
29+
end
30+
31+
cu.recalc_abilities!
32+
33+
# Grant mod ability if mod status is given
34+
if cu.at_least_moderator? && !cu.ability?('mod')
35+
cu.grant_ability!('mod')
36+
end
37+
38+
resolved << q.id
39+
rescue => e
40+
logger.error " Failed: #{e.class.name}: #{e.message}"
41+
logger.error e.backtrace
42+
43+
if Rails.env.test?
44+
raise e
45+
end
46+
end
47+
48+
AbilityQueue.where(id: resolved).update(completed: true)
49+
AbilityQueue.where(id: destroy).delete_all
50+
51+
unless options[:quiet]
52+
logger.info "Completed, #{resolved.size}/#{all.size} tasks successful, #{destroy.size} tasks invalid"
53+
end
54+
end
55+
end

config/credentials.yml.enc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
SA7h7Xm7xF3AEvje/xZiPMzem0wh2CysuvvB+iM6R0Epkg+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wP8HGRaips+ZhjBVvSs/8w7yQteBswFrc00+gts5TpqCz3AjNNMsHTP5cr5IhqoNWdpxvFgHHLBAPtn0HWJ5C3OP3X/4GhxG3G9Z99pxbH1ZjWn1vnKadKUfY4vzPYCZsMaokTr2/XuL8wROLohCD53SVATsGsbiL947u+X6WYEZVuOH2IY5xMhTO9JW8DYb0Rm31Te8oOpzoIrz9Ck70YDQcADiNg4SEeLTQlpve40C5+YI5Xr+60GbOHRHSGv31ENnubVTCTv3q6otE0iqCOAQ62RdqhzT23aaz0NuH5DtLIlWn/3ev9Js6mm1rnF7AzVKey01IsS--p3woHV6iIC4KuPxP--fRXQKncEML1whOWmVm2Z8w==

scripts/change_to_wilson_scores.rb

Lines changed: 0 additions & 33 deletions
This file was deleted.

scripts/cleanup_votes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CleanupVotesJob.perform_later
1+
CleanupVotesJob.perform_now

scripts/create_backup_2fa_codes.rb

Lines changed: 0 additions & 4 deletions
This file was deleted.

scripts/create_general_thread_for_old_comments.rb

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1 @@
1-
intents = Stripe::PaymentIntent.list
2-
intents.auto_paging_each do |pi|
3-
begin
4-
break unless Time.at(pi.created) >= 24.hours.ago
5-
next unless pi.status == 'requires_payment_method'
6-
next unless pi.metadata['user_id'].present?
7-
next if pi.metadata['emailed'].present?
8-
user = User.find(pi.metadata['user_id'])
9-
symbol = { 'GBP' => '£', 'USD' => '$', 'EUR' => '€' }[pi.currency.upcase]
10-
amount = pi.amount / 100
11-
DonationMailer.with(currency: symbol, amount: amount, email: user.email, name: user.username, intent: pi)
12-
.donation_uncaptured.deliver_now
13-
Stripe::PaymentIntent.update(pi.id, { metadata: { emailed: true } })
14-
puts "Mailed ##{user.id} for PaymentIntent #{pi.id}"
15-
rescue => ex
16-
Stripe::PaymentIntent.update(pi.id, { metadata: { email_error: "#{ex.message}" } })
17-
end
18-
end
1+
MailUncapturedDonations.perform_now

0 commit comments

Comments
 (0)