mirror of
https://github.com/internetarchive/warcprox.git
synced 2025-01-18 13:22:09 +01:00
changed after reviewing merge request
This commit is contained in:
parent
a877fa0fd8
commit
872ef2d93b
45
doublethink/cli.py
Normal file
45
doublethink/cli.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
'''
|
||||||
|
doublethink/orm.py - rethinkdb ORM Command Line Interface
|
||||||
|
|
||||||
|
Copyright (C) 2017 Internet Archive
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
'''
|
||||||
|
|
||||||
|
import os, sys
|
||||||
|
import argparse
|
||||||
|
import doublethink
|
||||||
|
|
||||||
|
def purge_stale_services(argv=None):
|
||||||
|
"""Command-line utility to periodically purge stale entries from the "services" table.
|
||||||
|
|
||||||
|
It is designed to be used in conjunction with cron.
|
||||||
|
"""
|
||||||
|
argv = argv or sys.argv
|
||||||
|
arg_parser = argparse.ArgumentParser(
|
||||||
|
prog=os.path.basename(argv[0]),
|
||||||
|
description='purge-stale-services: utility to periodically purge stale entries from the "services" table.')
|
||||||
|
|
||||||
|
arg_parser.add_argument("db", help="A RethinkDB database containing a 'services' table")
|
||||||
|
|
||||||
|
arg_parser.add_argument("-s", "--rethinkdb-servers",
|
||||||
|
metavar="SERVERS", dest="servers", required=True,
|
||||||
|
help="a comma-separated list of hostnames of rethinkdb servers. Required.")
|
||||||
|
args = arg_parser.parse_args(argv[1:])
|
||||||
|
|
||||||
|
args.servers = [srv.strip() for srv in args.servers.split(",")]
|
||||||
|
|
||||||
|
rethinker = doublethink.Rethinker(servers=args.servers, db=args)
|
||||||
|
registry = doublethink.services.ServiceRegistry(rethinker)
|
||||||
|
registry.purge_stale_services()
|
@ -312,8 +312,8 @@ class ServiceRegistry(object):
|
|||||||
available_service = healthy_service
|
available_service = healthy_service
|
||||||
available_services = healthy_services
|
available_services = healthy_services
|
||||||
|
|
||||||
def purge_stale_services(self, grace_period=0):
|
def purge_stale_services(self, ttls_until_deletion=2):
|
||||||
result = self.rr.table('services').filter(
|
result = self.rr.table('services').filter(
|
||||||
lambda svc: r.now().sub(svc["last_heartbeat"]).gt(svc["ttl"] + grace_period)
|
lambda svc: r.now().sub(svc["last_heartbeat"]).gt(svc["ttl"] * ttls_until_deletion)
|
||||||
).delete().run()
|
).delete().run()
|
||||||
return result
|
return result
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
import sys
|
|
||||||
from optparse import OptionParser
|
|
||||||
from doublethink import Rethinker
|
|
||||||
from doublethink.services import ServiceRegistry
|
|
||||||
|
|
||||||
usage = """usage: %prog [options] db
|
|
||||||
where 'db' is the the name of a RethinkDB database that contains a "services" table.
|
|
||||||
|
|
||||||
This script can be used to periodically purge stale entries from the "services" table.
|
|
||||||
|
|
||||||
It is designed to be used in conjunction with cron.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
%prog -s rethink-host0,rethink-host1,rethink-host2 doublethink_database"""
|
|
||||||
parser = OptionParser(usage=usage)
|
|
||||||
parser.add_option("-s", "--rethinkdb-servers",
|
|
||||||
metavar="SERVERS", dest="servers",
|
|
||||||
help="a comma-separated list of hostnames of rethinkdb servers. Required. [default: none]")
|
|
||||||
parser.add_option("-g", "--grace-period",
|
|
||||||
metavar="SECONDS", dest="grace_period", type="int",
|
|
||||||
help="leave records that have been stale for up to SECONDS seconds. [default: 0]")
|
|
||||||
(options, args) = parser.parse_args()
|
|
||||||
|
|
||||||
if len(args) < 1:
|
|
||||||
sys.exit('"db" is a required argument and should be the name of a RethinkDB database that contains a "services" table. See "--help" for a list of options')
|
|
||||||
|
|
||||||
if not options.servers:
|
|
||||||
sys.exit('--rethinkdb-servers (-s) is a required argument. It should be a comma-separated list of rethinkdb servers. See --help for more information')
|
|
||||||
|
|
||||||
options.servers = [srv.strip() for srv in options.servers.split(",")]
|
|
||||||
|
|
||||||
rethinker = Rethinker(servers=options.servers, db=args[0])
|
|
||||||
registry = ServiceRegistry(rethinker)
|
|
||||||
registry.purge_stale_services(grace_period=options.grace_period)
|
|
6
setup.py
6
setup.py
@ -18,5 +18,9 @@ setuptools.setup(
|
|||||||
description='rethinkdb python library',
|
description='rethinkdb python library',
|
||||||
long_description=codecs.open(
|
long_description=codecs.open(
|
||||||
'README.rst', mode='r', encoding='utf-8').read(),
|
'README.rst', mode='r', encoding='utf-8').read(),
|
||||||
scripts=glob.glob('scripts/*.py'),
|
entry_points={
|
||||||
|
'console_scripts': [
|
||||||
|
'purge-stale-services=doublethink.cli:purge_stale_services',
|
||||||
|
]
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user