Skip to content

Instantly share code, notes, and snippets.

@shbodya
Last active February 25, 2021 11:48
Show Gist options
  • Select an option

  • Save shbodya/b6e4fe1cc0b7cfd57222cebe26c1d43b to your computer and use it in GitHub Desktop.

Select an option

Save shbodya/b6e4fe1cc0b7cfd57222cebe26c1d43b to your computer and use it in GitHub Desktop.

Revisions

  1. Bogdan revised this gist Mar 24, 2017. 2 changed files with 4 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion rpush_reload.sh
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ done < <(su -c "source /home/qb_app/.rvm/scripts/rvm && cd /home/qb_app/QuickBlo
    for i in "${treads[@]}"
    do
    THREAD=`echo $i | awk '{print $2}'`
    if [[ $THREAD != "sleep" ]]
    if [[ $THREAD == "sleep" ]]
    then
    # Fix Rpush
    su -c "source /home/qb_app/.rvm/scripts/rvm && cd /home/qb_app/QuickBlox-Server && bundle exec rails runner -e production /etc/zabbix/scripts/rpush_reload_stuck_notifications.rb" qb_app > /dev/null 2>&1
    4 changes: 3 additions & 1 deletion rpush_reload_stuck_notifications.rb
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,9 @@
    batch_size = YAML.load_file("#{Rails.root}/config/rpush.yml")[Rails.env]['batch_size'] * 5
    rpush_dump_file = File.open("#{Rails.root}/log/rpush.dump", 'a')

    client = Redis.new
    config = YAML.load_file("#{::Rails.root}/config/redis.yml")[::Rails.env]
    client = Redis.new config

    notification_ids = client.smembers('rpush:notifications:all').map(&:to_i).sort.reverse
    # This is the standard set where Rpush stores notifications, that will be processed
    # It acts like a queue
  2. Bogdan revised this gist Mar 10, 2017. No changes.
  3. Bogdan created this gist Mar 10, 2017.
    20 changes: 20 additions & 0 deletions rpush_reload.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/bin/bash

    while read line
    do
    treads+=("$line")
    done < <(su -c "source /home/qb_app/.rvm/scripts/rvm && cd /home/qb_app/QuickBlox-Server && bundle exec rpush status -e production " qb_app | grep thread_status)

    for i in "${treads[@]}"
    do
    THREAD=`echo $i | awk '{print $2}'`
    if [[ $THREAD != "sleep" ]]
    then
    # Fix Rpush
    su -c "source /home/qb_app/.rvm/scripts/rvm && cd /home/qb_app/QuickBlox-Server && bundle exec rails runner -e production /etc/zabbix/scripts/rpush_reload_stuck_notifications.rb" qb_app > /dev/null 2>&1
    # Return '1' to zabbix
    printf "1" && exit 0
    fi
    done

    printf "0"
    50 changes: 50 additions & 0 deletions rpush_reload_stuck_notifications.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #puts "Restoring stuck Rpush notifications..."

    batch_size = YAML.load_file("#{Rails.root}/config/rpush.yml")[Rails.env]['batch_size'] * 5
    rpush_dump_file = File.open("#{Rails.root}/log/rpush.dump", 'a')

    client = Redis.new
    notification_ids = client.smembers('rpush:notifications:all').map(&:to_i).sort.reverse
    # This is the standard set where Rpush stores notifications, that will be processed
    # It acts like a queue
    pending_notification_ids = client.zrange('rpush:notifications:pending', 0, -1).map(&:to_i)
    notifications_restored = []

    notification_ids.each_with_index do |id, index|
    # Skip if notification is already marked for being processed soon
    next if pending_notification_ids.include?(id)

    notification = Rpush::Notification.find(id) rescue nil
    next unless notification

    if notification.delivered || notification.failed
    # Check for at least one batch size of notifications for being delivered
    break if index > batch_size

    next
    end

    if notifications_restored.empty?
    rpush_dump_file.write("Writing dump at #{Time.now.utc}\n")
    end

    rpush_dump_file.write("#{notification.attributes}\n")
    client.zadd('rpush:notifications:pending', 0, id)
    notifications_restored << id
    end

    rpush_dump_file.write("Current rpush status:\n")
    rpush_state = `bundle exec rpush status -e #{Rails.env}`
    rpush_dump_file.write("#{rpush_state}\n")

    rpush_dump_file.write("Rpush memory usage:\n")
    rpush_dump_file.write("#{`ps aux | grep 'rpush.*dispatchers' | grep -v grep`}\n")

    rpush_dump_file.write("Overall memory usage:\n")
    rpush_dump_file.write("#{`free`}\n\n")

    rpush_dump_file.close

    system("RAILS_ENV=#{Rails.env} bundle exec god restart rpush")

    #puts "#{notifications_restored.count} notifications restored"