Da carico leggero a carico pesante con il plugin ricompense

Ciao!
L’utilizzo della CPU del nostro server è passato dal 50% al 90% da quando abbiamo installato un plugin.
Sono nuovo a Ruby on Rails/PostgreSQL. Mi chiedo se le impostazioni del plugin stiano causando un picco nell’utilizzo della CPU. È solo un plugin per un sistema a punti.
Questo è il plugin.

Spero che qualcuno possa aiutarmi a capire se la struttura del database, la frequenza di lettura/scrittura e l’uso del message bus sono ragionevoli.
Posso pagare per l’ottimizzazione, se necessario.

Dipende da quanto sono grandi il tuo database e la tua VM. Puoi condividere quelle specifiche. Non ho esaminato il plugin, ma probabilmente aggiunge un certo overhead.

1 Mi Piace

Grazie per la vostra attenzione!
Questo è il nostro server.


Questa è la dimensione del nostro db.

Penso che dovrebbe essere sufficiente per eseguire discourse. L’utilizzo della CPU dal 50% al 90% sembra un po’ anomalo.
Potreste aiutarmi a controllare il plugin?
Sembra che UserPoint.create e MessageBus.publish vengano eseguiti ogni volta che viene creato un argomento/un post/un like.

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

E il message bus xhr fallisce sempre.

Un plugin del genere, che esegue diverse ricerche nel database, letture e scritture, ogni volta che si verifica un’azione comune come un post e i like, aumenterà effettivamente il carico del tuo server e, nel tuo caso, potrebbe spingerti ad aumentare le risorse della tua istanza server per accomodarlo.

3 Mi Piace