Skip to content

Instantly share code, notes, and snippets.

@breim
Last active March 6, 2021 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save breim/7e1e06cd141e3fb5d1aaee75c3a4df0a to your computer and use it in GitHub Desktop.
Save breim/7e1e06cd141e3fb5d1aaee75c3a4df0a to your computer and use it in GitHub Desktop.
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