-
Notifications
You must be signed in to change notification settings - Fork 14
/
update.py
69 lines (49 loc) · 2.12 KB
/
update.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from time import time
from app import db
import argparse
from jobs import update_registry
from util import elapsed
# needs to be imported so the definitions get loaded into the registry
import jobs_defs
"""
examples of calling this:
# update everything
python update.py Person.refresh --limit 10 --chunk 5 --rq
# update one thing not using rq
python update.py Person.refresh --orcid 0000-1111-2222-3333
"""
def parse_update_optional_args(parser):
# just for updating lots
parser.add_argument('--limit', "-l", nargs="?", type=int, help="how many jobs to do")
parser.add_argument('--chunk', "-ch", nargs="?", type=int, help="how many to take off db at once")
parser.add_argument('--after', nargs="?", type=str, help="minimum id or id start, ie 0000-0001")
parser.add_argument('--rq', action="store_true", default=False, help="do jobs in this thread")
# just for updating one
parser.add_argument('--id', nargs="?", type=str, help="id of the one thing you want to update")
parser.add_argument('--orcid', nargs="?", type=str, help="orcid id of the one thing you want to update")
# parse and run
parsed_args = parser.parse_args()
return parsed_args
def run_update(parsed_args):
update = update_registry.get(parsed_args.fn)
start = time()
#convenience method for handling an orcid
if parsed_args.orcid:
from models.person import Person
my_person = db.session.query(Person).filter(Person.orcid_id==parsed_args.orcid).first()
parsed_args.id = my_person.id
update.run(
use_rq=parsed_args.rq,
obj_id=parsed_args.id, # is empty unless updating just one row
min_id=parsed_args.after, # is empty unless minimum id
num_jobs=parsed_args.limit,
chunk_size=parsed_args.chunk
)
db.session.remove()
print "finished update in {}sec".format(elapsed(start))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run stuff.")
# for everything
parser.add_argument('fn', type=str, help="what function you want to run")
parsed_args = parse_update_optional_args(parser)
run_update(parsed_args)