# מטעינה קלה לעומס כבד עם תוסף תגמולים

**URL:** <https://meta.discourse.org/t/from-light-load-to-heavy-load-with-rewards-plugin/236272>\
**Category:** Development\
**Created:** [16 באוגוסט,‏ 2022,‏ 7:26am UTC](https://meta.discourse.org/t/from-light-load-to-heavy-load-with-rewards-plugin/236272 "2022-08-16T07:26:37Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Alienazk](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alienazk/32/261130_2.png) [@Alienazk](https://meta.discourse.org/u/Alienazk)\
**Post date:** [16 באוגוסט,‏ 2022,‏ 10:59am UTC](https://meta.discourse.org/t/from-light-load-to-heavy-load-with-rewards-plugin/236272/3 "2022-08-16T10:59:51Z")

</div>

Thank you for your attention!  
This is our server.

 ![屏幕快照 2022-08-16 19.47.31](https://global.discourse-cdn.com/meta/original/4X/2/3/4/23411dc112c2a196a9a6323365d27d7c2d868afb.png)  
This is size of our db.  
 ![屏幕快照 2022-08-16 19.44.26](https://global.discourse-cdn.com/meta/original/4X/e/9/7/e97ac37453fe2bfe353af98356c9b75cec543abe.png)  
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.

```plaintext
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](https://global.discourse-cdn.com/meta/original/4X/1/2/5/1258ca467dc0207894f68befb8795f3cba9e15c8.png)

---

_[View the full topic](https://meta.discourse.org/t/from-light-load-to-heavy-load-with-rewards-plugin/236272)._
