Skip to content

Commit

Permalink
Merge pull request #49 from RubyOnWorld/jobs-checkout-schedule
Browse files Browse the repository at this point in the history
jobs-checkout-schedule auto expire user api key
  • Loading branch information
xmas7 authored Nov 4, 2022
2 parents e02f665 + 2b6ec6e commit 9dd4a3e
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/jobs/scheduled/about_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Jobs
class AboutStats < ::Jobs::Scheduled
every 30.minutes

def execute(args)
About.refresh_stats
end
end
end
26 changes: 26 additions & 0 deletions app/jobs/scheduled/activation_reminder_emails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Jobs
class ActivationReminderEmails < ::Jobs::Scheduled
every 2.hours

def execute(args)
User.joins("LEFT JOIN user_custom_fields ON users.id = user_id AND user_custom_fields.name = 'activation_reminder'")
.where(active: false, staged: false, user_custom_fields: { value: nil })
.where('users.created_at BETWEEN ? AND ?', 3.days.ago, 2.days.ago)
.find_each do |user|

user.custom_fields['activation_reminder'] = true
user.save_custom_fields

email_token = user.email_tokens.create!(email: user.email, scope: EmailToken.scopes[:signup])
::Jobs.enqueue(
:user_email,
type: "activation_reminder",
user_id: user.id,
email_token: email_token.token
)
end
end
end
end
17 changes: 17 additions & 0 deletions app/jobs/scheduled/auto_expire_user_api_keys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Jobs

class AutoExpireUserApiKeys < ::Jobs::Scheduled
every 1.day

def execute(args)
if SiteSetting.expire_user_api_keys_days > 0
expire_user_api_keys_days = SiteSetting.expire_user_api_keys_days.days.ago

UserApiKey.where("last_used_at < ?", expire_user_api_keys_days).update_all(revoked_at: Time.zone.now)
end
end
end

end
28 changes: 28 additions & 0 deletions app/jobs/scheduled/auto_queue_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

# This job will automatically act on records that have gone unhandled on a
# queue for a long time.
module Jobs
class AutoQueueHandler < ::Jobs::Scheduled

every 1.day

def execute(args)
return unless SiteSetting.auto_handle_queued_age.to_i > 0

Reviewable
.pending
.where('created_at < ?', SiteSetting.auto_handle_queued_age.to_i.days.ago)
.each do |reviewable|

if reviewable.is_a?(ReviewableFlaggedPost)
reviewable.perform(Discourse.system_user, :ignore, expired: true)
elsif reviewable.is_a?(ReviewableQueuedPost)
reviewable.perform(Discourse.system_user, :reject_post)
elsif reviewable.is_a?(ReviewableUser)
reviewable.perform(Discourse.system_user, :delete_user)
end
end
end
end
end
31 changes: 31 additions & 0 deletions app/jobs/scheduled/badge_grant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Jobs

class BadgeGrant < ::Jobs::Scheduled
def self.run
self.new.execute(nil)
end

every 1.day

def execute(args)
return unless SiteSetting.enable_badges

Badge.enabled.find_each do |b|
begin
BadgeGranter.backfill(b)
rescue => ex
# TODO - expose errors in UI
Discourse.handle_job_exception(ex, error_context({}, code_desc: 'Exception granting badges', extra: { badge_id: b.id }))
end
end

BadgeGranter.revoke_ungranted_titles!
UserBadge.ensure_consistency! # Badge granter sometimes uses raw SQL, so hooks do not run. Clean up data
UserStat.update_distinct_badge_count
end

end

end
27 changes: 27 additions & 0 deletions app/jobs/scheduled/bookmark_reminder_notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Jobs

# Runs periodically to send out bookmark reminders, capped at 300 at a time.
# Any leftovers will be caught in the next run, because the reminder_at column
# is set to NULL once a reminder has been sent.
class BookmarkReminderNotifications < ::Jobs::Scheduled
every 5.minutes

def self.max_reminder_notifications_per_run
@@max_reminder_notifications_per_run ||= 300
@@max_reminder_notifications_per_run
end

def self.max_reminder_notifications_per_run=(max)
@@max_reminder_notifications_per_run = max
end

def execute(args = nil)
bookmarks = Bookmark.pending_reminders.includes(:user).order('reminder_at ASC')
bookmarks.limit(BookmarkReminderNotifications.max_reminder_notifications_per_run).each do |bookmark|
BookmarkReminderNotificationHandler.new(bookmark).send_notification
end
end
end
end
14 changes: 14 additions & 0 deletions app/jobs/scheduled/category_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Jobs

class CategoryStats < ::Jobs::Scheduled
every 24.hours

def execute(args)
Category.update_stats
end

end

end

0 comments on commit 9dd4a3e

Please sign in to comment.