-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmonitor.py
More file actions
executable file
·131 lines (110 loc) · 4.26 KB
/
monitor.py
File metadata and controls
executable file
·131 lines (110 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
#
# This file is part of osCommerce Bitcoin Payment Module
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import time
import re
import requests
import json
import signal
from requests.exceptions import ConnectionError
from time import sleep
from decimal import *
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Sale
# our configuration file - copy defaultsettings.py to settings.py and edit
from settings import *
from rpc import RPC
# connect to db
engine = create_engine(DATABASE_URI)
DBSession = sessionmaker(bind=engine)
session = DBSession()
# setup logging
import logging
logger = logging.getLogger('cw-monitor')
hdlr = logging.FileHandler('cw-monitor.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
# Daemon - handles running bitcoind and getting results back from it
class Daemon():
def __init__(self):
self.bitcoind_command = ['bitcoind']
self.rpc = RPC(RPCUSER, RPCPASS, SERVER, RPCPORT)
def check(self):
try:
self.rpc.get('getinfo')
except:
os.system("kill -9 `ps -ef | grep bitcoind | grep -v grep | awk '{print $2}'`")
logger.warning(json.dumps({'message': 'unresponsive bitcoind killed' }))
sleep(60) # give bitcoind time to die
os.system("bitcoind &")
logger.warning(json.dumps({'message': 'bitcoind started' }))
sleep(300) # wait a bit on the long side for more reliability
def get_transactions(self,number):
return self.rpc.get('listtransactions', ['*', int(number)])
def get_accountaddress(self,account):
return self.rpc.get('getaccountaddress', [account])
def get_receivedbyaddress(self,address,minconf):
res = self.rpc.get('getreceivedbyaddress', [address, int(minconf)])
return res['output']['result']
def get_balance(self,minconf):
res = self.rpc.get('getbalance', ['*', int(minconf)])
return Decimal(str(res))
def send(self,address,amount):
pass
#res = rpc.get(['sendtoaddress',address,str(amount),'testing'])
#return res
class Sales :
def __init__(self):
pass
def enter_deposits(self):
d = Daemon()
unpaid = Sale.get_unpaid(session)
logger.debug(json.dumps({"action":"enter deposits"}))
# get list of pending orders with amounts and addresses
for order in unpaid:
if order.payment_address is None:
continue
# get total out
total = Decimal(str(order.price))
address = order.payment_address
received = d.get_receivedbyaddress(address,MINCONF)
logger.info(json.dumps({"action":"check received", "expected": str(total), "received": received, "address": address}))
if( received >= total ):
logger.info(json.dumps({"action":"payment complete", "order_id": str(order.id)}))
# do things when payment received - mark a bucket paid, send an email, etc.
order.paid = True
session.add(order)
session.commit()
if __name__ == "__main__":
def signal_handler(signal, frame):
logger.info(json.dumps({"message": "exit"}))
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
logger.info(json.dumps({"message": "start"}))
d = Daemon()
s = Sales()
refreshcount = 0
while(1):
d.check()
s.enter_deposits()
refreshcount = refreshcount + 1
REFRESH_PERIOD = 60
sleep(REFRESH_PERIOD)