从轻负载到重负载,使用奖励插件

您好!
自从我们安装了一个插件以来,我们服务器的 CPU 使用率从 50% 上升到了 90%。
我是 Ruby on Rails/PostgreSQL 的新手。我想知道插件的设置是否会导致 CPU 使用率飙升。它只是一个积分系统插件。
这是插件。

我希望有人能帮助我看看数据库结构、读写频率以及消息总线的使用是否合理。
如果需要,我可以付费进行优化。

这取决于您的数据库和虚拟机的大小。您可以分享这些配置。我还没有查看过该插件,但它可能会增加一些开销。

1 个赞

感谢您的关注!
这是我们的服务器。


这是我们的数据库大小。

我认为这足以运行 Discourse。CPU 使用率从 50% 到 90% 似乎有点不正常。
您能否帮我看看插件?
似乎每当创建主题/帖子/点赞时都会执行 UserPoint.createMessageBus.publish

on(:notification_created) do |notification|
    data = JSON.parse(notification.data).with_indifferent_access

    badge = Badge.find(data[:badge_id]) if notification.notification_type == Notification.types[:granted_badge]

    if badge && badge.badge_grouping_id == 6
      user_badge = UserBadge.where(user_id: notification.user_id, badge_id: data[:badge_id]).order(created_at: :desc).first

      points = 0

      if badge.badge_type_id == BadgeType::Bronze
        points = SiteSetting.discourse_rewards_points_for_bronze_badges.to_i
      elsif badge.badge_type_id == BadgeType::Silver
        points = SiteSetting.discourse_rewards_points_for_silver_badges.to_i
      elsif badge.badge_type_id == BadgeType::Gold
        points = SiteSetting.discourse_rewards_points_for_gold_badges.to_i
      end

      description = {
        type: 'badge',
        badge_id: badge.id,
        name: badge.name
      }
      DiscourseRewards::UserPoint.create(user_id: notification.user_id, user_points_category_id: 1, user_badge_id: user_badge.id, reward_points: points, description: description.to_json) if points > 0

      user_message = {
        available_points: user_badge.user.available_points
      }

      MessageBus.publish("/u/#{user_badge.user.id}/rewards", user_message)
    end
  end

  on(:post_created) do |post|
    if post.user_id > 0 && post.post_number > 1 && post.topic.archetype != Archetype.private_message
      top_posts = Post.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
        .where(user_id: post.user_id)
        .where("post_number > 1")
        .order(:created_at)
        .limit(SiteSetting.discourse_rewards_daily_top_replies_to_grant_points.to_i)
        .pluck(:id) if SiteSetting.discourse_rewards_daily_top_replies_to_grant_points.to_i > 0

      if !top_posts || top_posts.include?(post.id)
        points = SiteSetting.discourse_rewards_points_for_post_create.to_i

        user = User.find(post.user_id)

        description = {
          type: 'post',
          post_id: post.id,
          post_number: post.post_number,
          topic_slug: post.topic.slug,
          topic_id: post.topic.id,
          topic_title: post.topic.title
        }

        DiscourseRewards::UserPoint.create(user_id: post.user_id, user_points_category_id: 4, reward_points: points, description: description.to_json) if points > 0

        user_message = {
          available_points: post.user.available_points,
          points: post.user.total_earned_points
        }

        MessageBus.publish("/u/#{post.user_id}/rewards", user_message)
      end
    end
  end

  on(:topic_created) do |topic|
    if topic.user_id > 0 && topic.archetype != Archetype.private_message
      top_topics = Topic.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
        .where(user_id: topic.user_id)
        .order(:created_at)
        .limit(SiteSetting.discourse_rewards_daily_top_topics_to_grant_points.to_i)
        .pluck(:id) if SiteSetting.discourse_rewards_daily_top_topics_to_grant_points.to_i > 0

      if !top_topics || top_topics.include?(topic.id)
        points = topic.category.custom_fields['rewards_points_for_topic_create'].to_i

        points = SiteSetting.discourse_rewards_points_for_topic_create.to_i if points <= 0

        user = User.find(topic.user_id)

        description = {
          type: 'topic',
          post_number: 1,
          topic_slug: topic.slug,
          topic_id: topic.id,
          topic_title: topic.title
        }

        DiscourseRewards::UserPoint.create(user_id: topic.user_id, user_points_category_id: 4, reward_points: points, description: description.to_json) if points > 0

        user_message = {
          available_points: topic.user.available_points
        }

        MessageBus.publish("/u/#{topic.user_id}/rewards", user_message)
      end
    end
  end

  on(:like_created) do |like|
    points = SiteSetting.discourse_rewards_points_for_like_received.to_i
    post = Post.find(like.post_id)
    user = post.user

    if user.id > 0
      top_likes = PostAction.where(post_action_type_id: PostActionType.types[:like])
        .where(post_id: Post.where(user_id: user.id), created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
        .order(:created_at)
        .limit(SiteSetting.discourse_rewards_daily_top_like_received_to_grant_points.to_i)
        .pluck(:id) if SiteSetting.discourse_rewards_daily_top_like_received_to_grant_points.to_i > 0

      if !top_likes || top_likes.include?(like.id)
        description = {
          type: 'like',
          post_id: post.id,
          post_number: post.post_number,
          topic_id: post.topic.id,
          topic_slug: post.topic.slug,
          topic_title: post.topic.title
        }

        DiscourseRewards::UserPoint.create(user_id: user.id, user_points_category_id: 4, reward_points: points, description: description.to_json) if points > 0

        user_message = {
          available_points: user.available_points
        }

        MessageBus.publish("/u/#{user.id}/rewards", user_message)
      end
    end
  end

并且 Message Bus XHR 总是失败。

像这样的插件,在发生诸如发帖和点赞等常见操作时,会执行多次数据库查找、读取和写入,这确实会增加您的服务器负载,在您的情况下,可能需要增加服务器实例资源来适应它。

3 个赞