Last active
March 6, 2021 15:37
-
-
Save breim/7e1e06cd141e3fb5d1aaee75c3a4df0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task channel_keep_alive: :environment do | |
ws = Binance::Client::WebSocket.new | |
message = proc { |e| puts e.data } | |
wallets_total = Wallet.count | |
EM.run do | |
spawned_process = EM.spawn do |params| | |
wallets = Wallet.where(disabled: false) | |
wallets_total = Wallet.count | |
channels = [] | |
# Load channel Keys | |
wallets.each do |wallet| | |
client = Binance::Client::REST.new(api_key: wallet.api_key, secret_key: wallet.api_secret_key) | |
channels << client.listen_key['listenKey'] | |
end | |
# Open sockets | |
channels.each do |channel| | |
ws.user_data listen_key: channel, methods: {message: message} | |
end | |
# Renew Channel Keys | |
EM.add_periodic_timer(600) { | |
wallets.each do |wallet| | |
client = Binance::Client::REST.new(api_key: wallet.api_key, secret_key: wallet.api_secret_key) | |
client.keep_alive_stream! | |
end | |
} | |
end | |
# Check new wallets | |
EM.add_periodic_timer(300) do | |
spawned_process.notify "new_wallet" if wallets_total < Wallet.count | |
end | |
# Start process | |
spawned_process.notify "start" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment