Module: Jobs

Defined in:
app/jobs/scheduled/heartbeat.rb,
app/jobs/base.rb,
app/jobs/scheduled/weekly.rb,
app/jobs/regular/bump_topic.rb,
app/jobs/regular/merge_user.rb,
app/jobs/regular/open_topic.rb,
app/jobs/regular/post_alert.rb,
app/jobs/regular/user_email.rb,
app/jobs/onceoff/grant_emoji.rb,
app/jobs/regular/bulk_invite.rb,
app/jobs/regular/close_topic.rb,
app/jobs/regular/unpin_topic.rb,
app/jobs/onceoff/fix_s3_etags.rb,
app/jobs/onceoff/grant_onebox.rb,
app/jobs/regular/delete_topic.rb,
app/jobs/regular/invite_email.rb,
app/jobs/regular/process_post.rb,
app/jobs/regular/create_backup.rb,
app/jobs/regular/process_email.rb,
app/jobs/regular/remove_banner.rb,
app/jobs/regular/run_heartbeat.rb,
app/jobs/scheduled/about_stats.rb,
app/jobs/scheduled/badge_grant.rb,
app/jobs/onceoff/fix_posts_read.rb,
app/jobs/regular/anonymize_user.rb,
app/jobs/regular/delete_replies.rb,
app/jobs/regular/group_pm_alert.rb,
app/jobs/regular/retrieve_topic.rb,
app/jobs/scheduled/poll_mailbox.rb,
app/jobs/regular/clear_slow_mode.rb,
app/jobs/regular/export_csv_file.rb,
app/jobs/regular/update_gravatar.rb,
app/jobs/regular/update_username.rb,
app/jobs/scheduled/clean_up_tags.rb,
app/jobs/regular/crawl_topic_link.rb,
app/jobs/regular/group_smtp_email.rb,
app/jobs/regular/mass_award_badge.rb,
app/jobs/regular/suspicious_login.rb,
app/jobs/regular/topic_timer_base.rb,
app/jobs/scheduled/admin_problems.rb,
app/jobs/scheduled/category_stats.rb,
app/jobs/scheduled/reindex_search.rb,
app/jobs/scheduled/tl3_promotions.rb,
app/jobs/regular/notify_tag_change.rb,
app/jobs/regular/push_notification.rb,
app/jobs/regular/run_problem_check.rb,
app/jobs/scheduled/schedule_backup.rb,
app/jobs/scheduled/unsilence_users.rb,
app/jobs/regular/notify_moved_posts.rb,
app/jobs/scheduled/clean_up_exports.rb,
app/jobs/scheduled/clean_up_uploads.rb,
app/jobs/scheduled/enqueue_onceoffs.rb,
app/jobs/regular/change_display_name.rb,
app/jobs/regular/create_linked_topic.rb,
app/jobs/regular/critical_user_email.rb,
app/jobs/regular/emit_web_hook_event.rb,
app/jobs/regular/export_user_archive.rb,
app/jobs/regular/feature_topic_users.rb,
app/jobs/regular/send_system_message.rb,
app/jobs/regular/toggle_topic_closed.rb,
app/jobs/scheduled/old_keys_reminder.rb,
app/jobs/scheduled/purge_unactivated.rb,
app/jobs/scheduled/top_refresh_older.rb,
app/jobs/scheduled/top_refresh_today.rb,
app/jobs/regular/backup_chunks_merger.rb,
app/jobs/regular/notify_post_revision.rb,
app/jobs/regular/update_hotlinked_raw.rb,
app/jobs/scheduled/auto_queue_handler.rb,
app/jobs/scheduled/call_discourse_hub.rb,
app/jobs/scheduled/check_new_features.rb,
app/jobs/scheduled/drop_backup_schema.rb,
app/jobs/scheduled/periodical_updates.rb,
app/jobs/scheduled/run_problem_checks.rb,
app/jobs/onceoff/clean_up_post_timings.rb,
app/jobs/onceoff/fix_retro_anniversary.rb,
app/jobs/onceoff/migrate_custom_emojis.rb,
app/jobs/onceoff/post_uploads_recovery.rb,
app/jobs/regular/download_backup_email.rb,
app/jobs/regular/enable_bootstrap_mode.rb,
app/jobs/regular/pull_hotlinked_images.rb,
app/jobs/regular/sync_acls_for_uploads.rb,
app/jobs/regular/update_group_mentions.rb,
app/jobs/scheduled/clean_up_email_logs.rb,
app/jobs/scheduled/regenerate_sitemaps.rb,
app/jobs/onceoff/clear_width_and_height.rb,
app/jobs/onceoff/migrate_censored_words.rb,
app/jobs/onceoff/migrate_featured_links.rb,
app/jobs/onceoff/migrate_tagging_plugin.rb,
app/jobs/onceoff/retro_recent_time_read.rb,
app/jobs/regular/bulk_grant_trust_level.rb,
app/jobs/regular/bulk_user_title_update.rb,
app/jobs/regular/notify_category_change.rb,
app/jobs/regular/send_push_notification.rb,
app/jobs/regular/update_top_redirection.rb,
app/jobs/scheduled/clean_up_search_logs.rb,
app/jobs/scheduled/topic_timer_enqueuer.rb,
app/jobs/scheduled/update_heat_settings.rb,
app/jobs/onceoff/fix_encoded_topic_slugs.rb,
app/jobs/onceoff/init_category_tag_stats.rb,
app/jobs/onceoff/retro_grant_anniversary.rb,
app/jobs/regular/group_pm_update_summary.rb,
app/jobs/regular/rebake_posts_for_upload.rb,
app/jobs/scheduled/clean_up_email_tokens.rb,
app/jobs/scheduled/cleanup_imap_sync_log.rb,
app/jobs/scheduled/enqueue_digest_emails.rb,
app/jobs/scheduled/enqueue_suspect_users.rb,
app/jobs/scheduled/ensure_db_consistency.rb,
app/jobs/scheduled/ignored_users_summary.rb,
app/jobs/scheduled/migrate_upload_scheme.rb,
app/jobs/scheduled/process_badge_backlog.rb,
app/jobs/scheduled/purge_deleted_uploads.rb,
app/jobs/onceoff/create_tags_search_index.rb,
app/jobs/regular/admin_confirmation_email.rb,
app/jobs/regular/confirm_sns_subscription.rb,
app/jobs/regular/create_avatar_thumbnails.rb,
app/jobs/regular/download_avatar_from_url.rb,
app/jobs/regular/process_sns_notification.rb,
app/jobs/scheduled/clean_up_crawler_stats.rb,
app/jobs/scheduled/clean_up_unmatched_ips.rb,
app/jobs/scheduled/create_missing_avatars.rb,
app/jobs/scheduled/disable_bootstrap_mode.rb,
app/jobs/scheduled/pending_users_reminder.rb,
app/jobs/onceoff/migrate_upload_extensions.rb,
app/jobs/regular/generate_topic_thumbnails.rb,
app/jobs/regular/publish_topic_to_category.rb,
app/jobs/regular/rebake_custom_emoji_posts.rb,
app/jobs/scheduled/clean_up_inactive_users.rb,
app/jobs/scheduled/directory_refresh_daily.rb,
app/jobs/scheduled/directory_refresh_older.rb,
app/jobs/scheduled/update_animated_uploads.rb,
app/jobs/scheduled/update_topic_hot_scores.rb,
app/jobs/onceoff/clean_up_sidekiq_statistic.rb,
app/jobs/onceoff/fix_encoded_category_slugs.rb,
app/jobs/onceoff/grant_first_reply_by_email.rb,
app/jobs/onceoff/remove_old_auto_close_jobs.rb,
app/jobs/regular/automatic_group_membership.rb,
app/jobs/regular/process_bulk_invite_emails.rb,
app/jobs/regular/sync_topic_user_bookmarked.rb,
app/jobs/scheduled/backfill_dominant_colors.rb,
app/jobs/scheduled/check_out_of_date_themes.rb,
app/jobs/scheduled/clean_up_post_reply_keys.rb,
app/jobs/scheduled/clean_up_unused_api_keys.rb,
app/jobs/scheduled/destroy_old_hidden_posts.rb,
app/jobs/scheduled/grant_anniversary_badges.rb,
app/jobs/onceoff/clean_up_user_export_topics.rb,
app/jobs/regular/make_embedded_topic_visible.rb,
app/jobs/scheduled/clean_up_stylesheet_cache.rb,
app/jobs/scheduled/clean_up_unmatched_emails.rb,
app/jobs/scheduled/clean_up_unsubscribe_keys.rb,
app/jobs/scheduled/purge_old_web_hook_events.rb,
app/jobs/scheduled/redeliver_web_hook_events.rb,
app/jobs/onceoff/fix_featured_link_for_topics.rb,
app/jobs/onceoff/fix_invalid_gravatar_uploads.rb,
app/jobs/regular/update_topic_upload_security.rb,
app/jobs/scheduled/activation_reminder_emails.rb,
app/jobs/scheduled/aggregate_web_hooks_events.rb,
app/jobs/scheduled/clean_up_api_keys_max_life.rb,
app/jobs/scheduled/clean_up_associated_groups.rb,
app/jobs/scheduled/destroy_old_deletion_stubs.rb,
app/jobs/scheduled/invalidate_inactive_admins.rb,
app/jobs/onceoff/fix_invalid_upload_extensions.rb,
app/jobs/scheduled/check_translation_overrides.rb,
app/jobs/scheduled/clean_dismissed_topic_users.rb,
app/jobs/scheduled/ensure_s3_uploads_existence.rb,
app/jobs/scheduled/presence_channel_auto_leave.rb,
app/jobs/scheduled/purge_expired_ignored_users.rb,
app/jobs/onceoff/correct_missing_dualstack_urls.rb,
app/jobs/scheduled/clean_up_associated_accounts.rb,
app/jobs/scheduled/clean_up_unused_staged_users.rb,
app/jobs/scheduled/pending_reviewables_reminder.rb,
app/jobs/regular/notify_mailing_list_subscribers.rb,
app/jobs/scheduled/clean_up_unused_user_api_keys.rb,
app/jobs/scheduled/pending_queued_posts_reminder.rb,
app/jobs/scheduled/process_shelved_notifications.rb,
app/jobs/regular/post_update_topic_tracking_state.rb,
app/jobs/regular/publish_group_membership_updates.rb,
app/jobs/scheduled/clean_up_email_change_requests.rb,
app/jobs/regular/delete_inaccessible_notifications.rb,
app/jobs/regular/update_post_uploads_secure_status.rb,
app/jobs/scheduled/bookmark_reminder_notifications.rb,
app/jobs/scheduled/clean_up_user_api_keys_max_life.rb,
app/jobs/regular/invite_password_instructions_email.rb,
app/jobs/regular/pull_user_profile_hotlinked_images.rb,
app/jobs/onceoff/fix_primary_emails_for_staged_users.rb,
app/jobs/scheduled/create_recent_post_search_indexes.rb,
app/jobs/onceoff/fix_out_of_sync_user_uploaded_avatar.rb,
app/jobs/regular/download_profile_background_from_url.rb,
app/jobs/scheduled/grant_new_user_of_the_month_badges.rb,
app/jobs/scheduled/process_user_notification_schedules.rb,
app/jobs/scheduled/cleanup_redelivering_web_hook_events.rb,
app/jobs/scheduled/fix_user_usernames_and_groups_names_clash.rb,
app/jobs/scheduled/purge_old_web_hook_events_daily_aggregate.rb,
app/jobs/onceoff/fix_post_search_data_after_default_locale_rename.rb,
app/jobs/scheduled/clean_up_unused_registered_user_api_key_clients.rb

Overview

This job will automatically act on records that have gone unhandled on a queue for a long time.

Defined Under Namespace

Classes: AboutStats, ActivationReminderEmails, AdminConfirmationEmail, AdminProblems, AggregateWebHooksEvents, AnonymizeUser, AutoQueueHandler, AutomaticGroupMembership, BackfillDominantColors, BackfillSidebarSiteSettings, BackupChunksMerger, BadgeGrant, Base, BookmarkReminderNotifications, BulkGrantTrustLevel, BulkInvite, BulkUserTitleUpdate, BumpTopic, CallDiscourseHub, CategoryStats, ChangeDisplayName, CheckNewFeatures, CheckOutOfDateThemes, CheckTranslationOverrides, CleanDismissedTopicUsers, CleanUpApiKeysMaxLife, CleanUpAssociatedAccounts, CleanUpAssociatedGroups, CleanUpCrawlerStats, CleanUpEmailChangeRequests, CleanUpEmailLogs, CleanUpEmailTokens, CleanUpExports, CleanUpInactiveUsers, CleanUpPostReplyKeys, CleanUpPostTimings, CleanUpSearchLogs, CleanUpSidekiqStatistic, CleanUpStylesheetCache, CleanUpTags, CleanUpUnmatchedEmails, CleanUpUnmatchedIPs, CleanUpUnsubscribeKeys, CleanUpUnusedApiKeys, CleanUpUnusedRegisteredUserApiKeyClients, CleanUpUnusedStagedUsers, CleanUpUnusedUserApiKeys, CleanUpUploads, CleanUpUserApiKeysMaxLife, CleanUpUserExportTopics, CleanupImapSyncLog, CleanupRedeliveringWebHookEvents, ClearSlowMode, ClearWidthAndHeight, CloseTopic, ConfirmSnsSubscription, CorrectMissingDualstackUrls, CrawlTopicLink, CreateAvatarThumbnails, CreateBackup, CreateLinkedTopic, CreateMissingAvatars, CreateRecentPostSearchIndexes, CreateTagsSearchIndex, CreateUserReviewable, CriticalUserEmail, DeleteInaccessibleNotifications, DeleteReplies, DeleteTopic, DestroyOldDeletionStubs, DestroyOldHiddenPosts, DirectoryRefreshDaily, DirectoryRefreshOlder, DisableBootstrapMode, DownloadAvatarFromUrl, DownloadBackupEmail, DownloadProfileBackgroundFromUrl, DropBackupSchema, EmitWebHookEvent, EnableBootstrapMode, EnqueueDigestEmails, EnqueueOnceoffs, EnqueueSuspectUsers, EnsureDbConsistency, EnsureS3UploadsExistence, ExportCsvFile, ExportUserArchive, FeatureTopicUsers, FixEncodedCategorySlugs, FixEncodedTopicSlugs, FixFeaturedLinkForTopics, FixInvalidGravatarUploads, FixInvalidUploadExtensions, FixOutOfSyncUserUploadedAvatar, FixPostSearchDataAfterDefaultLocaleRename, FixPostsRead, FixPrimaryEmailsForStagedUsers, FixRetroAnniversary, FixS3Etags, FixUserUsernamesAndGroupsNamesClash, GenerateTopicThumbnails, GrantAnniversaryBadges, GrantEmoji, GrantFirstReplyByEmail, GrantNewUserOfTheMonthBadges, GrantOnebox, GroupPmAlert, GroupPmUpdateSummary, GroupSmtpEmail, HandledExceptionWrapper, Heartbeat, IgnoredUsersSummary, IndexCategoryForSearch, IndexUserFieldsForSearch, InitCategoryTagStats, InvalidateInactiveAdmins, InviteEmail, InvitePasswordInstructionsEmail, MakeEmbeddedTopicVisible, MassAwardBadge, MergeUser, MigrateCensoredWords, MigrateCustomEmojis, MigrateFeaturedLinks, MigrateTaggingPlugin, MigrateUploadExtensions, MigrateUploadScheme, NotifyCategoryChange, NotifyMailingListSubscribers, NotifyMovedPosts, NotifyPostRevision, NotifyReviewable, NotifyTagChange, OldKeysReminder, Onceoff, OpenTopic, PendingQueuedPostsReminder, PendingReviewablesReminder, PendingUsersReminder, PeriodicalUpdates, PollMailbox, PostAlert, PostUpdateTopicTrackingState, PostUploadsRecovery, PresenceChannelAutoLeave, ProcessBadgeBacklog, ProcessBulkInviteEmails, ProcessEmail, ProcessPost, ProcessShelvedNotifications, ProcessSnsNotification, ProcessUserNotificationSchedules, PublishGroupMembershipUpdates, PublishTopicToCategory, PullHotlinkedImages, PullUserProfileHotlinkedImages, PurgeDeletedUploads, PurgeExpiredIgnoredUsers, PurgeOldWebHookEvents, PurgeOldWebHookEventsDailyAggregate, PurgeUnactivated, PushNotification, RebakeCustomEmojiPosts, RebakePostsForUpload, RedeliverWebHookEvents, RefreshUsersReviewableCounts, RegenerateSitemaps, ReindexSearch, RemoveBanner, RemoveOldAutoCloseJobs, RetrieveTopic, RetroGrantAnniversary, RetroRecentTimeRead, RetrySignal, ReviewablePriorities, RunHeartbeat, RunProblemCheck, RunProblemChecks, ScheduleBackup, Scheduled, SendPushNotification, SendSystemMessage, SuspiciousLogin, SyncAclsForUploads, SyncTopicUserBookmarked, Tl3Promotions, ToggleTopicClosed, TopRefreshOlder, TopRefreshToday, TopicActionConverter, TopicTimerBase, TopicTimerEnqueuer, TruncateUserFlagStats, UnpinTopic, UnsilenceUsers, UpdateAnimatedUploads, UpdateGravatar, UpdateGroupMentions, UpdateHeatSettings, UpdateHotlinkedRaw, UpdatePostUploadsSecureStatus, UpdateTopRedirection, UpdateTopicHotScores, UpdateTopicUploadSecurity, UpdateUsername, UserEmail, Weekly

Class Method Summary collapse

Class Method Details

.cancel_scheduled_job(job_name, opts = {}) ⇒ Object



456
457
458
# File 'app/jobs/base.rb', line 456

def self.cancel_scheduled_job(job_name, opts = {})
  scheduled_for(job_name, opts).each(&:delete)
end

.enqueue(job, opts = {}) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'app/jobs/base.rb', line 375

def self.enqueue(job, opts = {})
  if job.instance_of?(Class)
    klass = job
  else
    klass = "::Jobs::#{job.to_s.camelcase}".constantize
  end

  # Unless we want to work on all sites
  unless opts.delete(:all_sites)
    opts[:current_site_id] ||= RailsMultisite::ConnectionManagement.current_db
  end

  delay = opts.delete(:delay_for)
  queue = opts.delete(:queue)

  # Only string keys are allowed in JSON. We call `.with_indifferent_access`
  # in Jobs::Base#perform, so this is invisible to developers
  opts = opts.deep_stringify_keys

  # Simulate the args being dumped/parsed through JSON
  parsed_opts = JSON.parse(JSON.dump(opts))
  if opts != parsed_opts
    Discourse.deprecate(<<~TEXT.squish, since: "2.9", drop_from: "3.0", output_in_test: true)
      #{klass.name} was enqueued with argument values which do not cleanly serialize to/from JSON.
      This means that the job will be run with slightly different values than the ones supplied to `enqueue`.
      Argument values should be strings, booleans, numbers, or nil (or arrays/hashes of those value types).
    TEXT
  end
  opts = parsed_opts

  if ::Jobs.run_later?
    hash = { "class" => klass, "args" => [opts] }

    if delay
      hash["at"] = Time.now.to_f + delay.to_f if delay.to_f > 0
    end

    hash["queue"] = queue if queue

    DB.after_commit { klass.client_push(hash) }
  else
    if Rails.env == "development"
      Scheduler::Defer.later("job") { klass.new.perform(opts) }
    else
      # Run the job synchronously
      # But never run a job inside another job
      # That could cause deadlocks during test runs
      queue = Thread.current[:discourse_nested_job_queue]
      outermost_job = !queue

      if outermost_job
        queue = Queue.new
        Thread.current[:discourse_nested_job_queue] = queue
      end

      queue.push([klass, opts])

      if outermost_job
        # responsible for executing the queue
        begin
          until queue.empty?
            queued_klass, queued_opts = queue.pop(true)
            queued_klass.new.perform_immediately(queued_opts)
          end
        ensure
          Thread.current[:discourse_nested_job_queue] = nil
        end
      end
    end
  end
end

.enqueue_at(datetime, job_name, opts = {}) ⇒ Object



451
452
453
454
# File 'app/jobs/base.rb', line 451

def self.enqueue_at(datetime, job_name, opts = {})
  secs = [datetime.to_f - Time.zone.now.to_f, 0].max
  enqueue_in(secs, job_name, opts)
end

.enqueue_in(secs, job_name, opts = {}) ⇒ Object



447
448
449
# File 'app/jobs/base.rb', line 447

def self.enqueue_in(secs, job_name, opts = {})
  enqueue(job_name, opts.merge!(delay_for: secs))
end

.last_job_performed_atObject



32
33
34
35
36
37
# File 'app/jobs/base.rb', line 32

def self.last_job_performed_at
  Sidekiq.redis do |r|
    int = r.get("last_job_perform_at")
    int ? Time.at(int.to_i) : nil
  end
end

.num_email_retry_jobsObject



39
40
41
# File 'app/jobs/base.rb', line 39

def self.num_email_retry_jobs
  Sidekiq::RetrySet.new.count { |job| job.klass =~ /Email\z/ }
end

.queuedObject



4
5
6
# File 'app/jobs/base.rb', line 4

def self.queued
  Sidekiq::Stats.new.enqueued
end

.run_immediately!Object



16
17
18
# File 'app/jobs/base.rb', line 16

def self.run_immediately!
  @run_immediately = true
end

.run_immediately?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/jobs/base.rb', line 12

def self.run_immediately?
  !!@run_immediately
end

.run_later!Object



20
21
22
# File 'app/jobs/base.rb', line 20

def self.run_later!
  @run_immediately = false
end

.run_later?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'app/jobs/base.rb', line 8

def self.run_later?
  !@run_immediately
end

.scheduled_for(job_name, opts = {}) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'app/jobs/base.rb', line 460

def self.scheduled_for(job_name, opts = {})
  opts = opts.with_indifferent_access
  unless opts.delete(:all_sites)
    opts[:current_site_id] ||= RailsMultisite::ConnectionManagement.current_db
  end

  job_class = "Jobs::#{job_name.to_s.camelcase}"
  Sidekiq::ScheduledSet.new.select do |scheduled_job|
    if scheduled_job.klass.to_s == job_class
      matched = true
      job_params = scheduled_job.item["args"][0].with_indifferent_access
      opts.each do |key, value|
        if job_params[key] != value
          matched = false
          break
        end
      end
      matched
    else
      false
    end
  end
end

.with_immediate_jobsObject



24
25
26
27
28
29
30
# File 'app/jobs/base.rb', line 24

def self.with_immediate_jobs
  prior = @run_immediately
  run_immediately!
  yield
ensure
  @run_immediately = prior
end