-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.py
48 lines (37 loc) · 1.27 KB
/
service.py
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
import time
import logging
from nameko.web.handlers import http
from nameko.timer import timer
from nameko_consul.dependencies import Consul
from nameko_consul.entrypoints import watch
class ExampleService(object):
name = 'example'
consul = Consul()
#@timer(interval=2)
#def get_count(self):
# print('timer: ' + str(self.consul.kv.get('count')))
@http('GET', '/')
def hello_world(self, request):
return 'hello world\n'
@http('GET', '/reset-count')
def reset_count(self, request):
self.consul.kv.put('count', '0')
return 'reset count to: 0\n'
@http('GET', '/increment-count')
def increment_count(self, request):
index, data = self.consul.kv.get('count')
print('index: %s; data: %s' % (index, data))
if data:
count = int(data['Value'])
else:
index = 0
count = 0
new_count = str(count+1)
self.consul.kv.put('count', new_count, cas=index)
return 'count is now: %s\n' % new_count
@watch.key('count')
def handle_count(self, *args, **kwargs):
print('handle_count: %s; %s' % (args, kwargs))
@watch.event('fire')
def handle_fire(self, *args, **kwargs):
print('handle_fire: %s; %s' % (args, kwargs))