From light load to heavy load with rewards plugin

Hello!
Our server’s cpu usage has gone from 50% to 90% since we installed a plugin.
I am new to ruby on rails/postgresql. I’m wondering if the plugin’s settings are causing the cpu usage to spike. It is just a points system plugin.
This is the plugin.

I hope someone can help me to see if the database structure and read/write frequency, and the use of message bus are reasonable.
I can pay for the optimization if needed.

It depends on how big your database and vm are. You can share those specs. I haven’t looked at the plugin, but it likely adds some overhead.

1 Like

Thank you for your attention!
This is our server.
屏幕快照 2022-08-16 19.47.31
This is size of our db.


I think it should be enough to run discourse. Cpu usage from 50% to 90% seems a bit abnormal.
Could you please help me looked at the plugin?
It seems like UserPoint.create and MessageBus.publish are performed whenever a topic/a post/a like is created.

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

And message bus xhr always failed.
屏幕快照 2022-08-16 19.59.17

A plugin like that, that performs several databases lookups, reads and writes, whenever a common action like a post and likes happen will indeed increase your server load, and in your case may prompt you do increase your server instance resources in order to accommodate it.

3 Likes