This is my fix for award-lifecycle-badges. Changes;
- Pulling the userlist from fas now loops over each letter of the alphabet to avoid time out issues - I call time.sleep(60) after awarding a badge to help avoid overwhelming the frontend
This commit is contained in:
parent
9f46774b7f
commit
6ccf653415
2 changed files with 159 additions and 28 deletions
|
@ -32,18 +32,16 @@ fedmsg.init(**fm_config)
|
|||
|
||||
import fedbadges.utils
|
||||
|
||||
# generates a list of search terms
|
||||
# alpha map is just a lowercase english alphabet
|
||||
|
||||
#a simple listcomp to generate a lists of searchterms
|
||||
#allows us to break the fas userlist down to smaller chunks
|
||||
#so the cron job doesn't hang
|
||||
def get_fas_searchterm():
|
||||
ast = "*"
|
||||
def gen_fas_searchterms():
|
||||
alpha = map(chr, range(97, 123))
|
||||
searchterms = [ term_str + ast for term_str in alpha ]
|
||||
searchterms = [ alpha_ltr + "*" for alpha_ltr in alpha ]
|
||||
return searchterms
|
||||
|
||||
|
||||
def get_fas_userlist(fas_credentials):
|
||||
def get_fas_userlist(fas_credentials, search_qry):
|
||||
creds = fas_credentials
|
||||
|
||||
fasclient = fedora.client.fas2.AccountSystem(
|
||||
|
@ -51,28 +49,24 @@ def get_fas_userlist(fas_credentials):
|
|||
password=creds['password'],
|
||||
)
|
||||
|
||||
timeout
|
||||
timeout = socket.getdefaulttimeout()
|
||||
socket.setdefaulttimeout(600)
|
||||
searchterm = get_fas_searchterms()
|
||||
for search_elem in searchterm:
|
||||
try:
|
||||
log.info("Downloading FAS cache")
|
||||
request = fasclient.send_request('/user/list',
|
||||
req_params={'search': search_elem},
|
||||
try:
|
||||
log.info("Downloading FAS cache")
|
||||
request = fasclient.send_request('/user/list',
|
||||
req_params={'search': search_qry},
|
||||
auth=True)
|
||||
fas_userlist.update(request)
|
||||
finally:
|
||||
socket.setdefaulttimeout(timeout)
|
||||
|
||||
finally:
|
||||
socket.setdefaulttimeout(timeout)
|
||||
|
||||
# We don't actually check for CLA+1, just "2 groups"
|
||||
return [p for p in fas_userlist['people'] if len(p.memberships) > 1]
|
||||
return [p for p in request['people'] if len(p.memberships) > 1]
|
||||
|
||||
|
||||
def main():
|
||||
now = datetime.datetime.utcnow()
|
||||
year = datetime.timedelta(days=365.5)
|
||||
search_terms = get_fas_searchterms()
|
||||
mapping = {
|
||||
'egg': year * 1,
|
||||
'embryo': year * 2,
|
||||
|
@ -88,16 +82,22 @@ def main():
|
|||
assert(badge.id)
|
||||
|
||||
# Then, do a long query against FAS for our candidates.
|
||||
# looping over a list of search terms allows us to work around the socket timeout
|
||||
results = get_fas_userlist(fas_credentials=fm_config['fas_credentials'])
|
||||
# Here I call search terms to generate a lists of search terms
|
||||
# Looping over the list of search terms, pass the search term to get_fas_userlists
|
||||
|
||||
for badge_id, delta in mapping.items():
|
||||
badge = tahrir.get_badge(badge_id=badge_id)
|
||||
for person in results:
|
||||
creation = datetime.datetime.strptime(
|
||||
person.creation.split('.')[0], '%Y-%m-%d %H:%M:%S')
|
||||
if now - creation > delta:
|
||||
hit_em_up(badge, person)
|
||||
fas_credentials = fm_config['fas_credentials']
|
||||
searchterms = gen_fas_searchterms()
|
||||
for search_elem in searchterms:
|
||||
results = get_fas_userlist(fas_credentials, search_elem)
|
||||
|
||||
for badge_id, delta in mapping.items():
|
||||
badge = tahrir.get_badge(badge_id=badge_id)
|
||||
for person in results:
|
||||
creation = datetime.datetime.strptime(
|
||||
person.creation.split('.')[0], '%Y-%m-%d %H:%M:%S')
|
||||
if now - creation > delta:
|
||||
hit_em_up(badge, person)
|
||||
time.sleep(60)
|
||||
|
||||
|
||||
def hit_em_up(badge, fas_user):
|
||||
|
|
131
roles/badges/backend/files/cron/award-lifecycle-badges-old
Executable file
131
roles/badges/backend/files/cron/award-lifecycle-badges-old
Executable file
|
@ -0,0 +1,131 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import __main__
|
||||
# This is going to require sqlalchemy 0.8 sooner than later.
|
||||
__main__.__requires__ = __requires__ = ["tahrir-api", "sqlalchemy>=0.7"];
|
||||
import pkg_resources
|
||||
pkg_resources.require(__requires__)
|
||||
|
||||
import datetime
|
||||
import time
|
||||
import urllib
|
||||
import socket
|
||||
|
||||
from tahrir_api.dbapi import TahrirDatabase
|
||||
import transaction
|
||||
|
||||
_fas_cache = {}
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
logging.basicConfig()
|
||||
import fedora.client.fas2
|
||||
|
||||
import fedmsg
|
||||
import fedmsg.config
|
||||
|
||||
fm_config = fedmsg.config.load_config()
|
||||
fm_config['cert_prefix'] = 'fedbadges'
|
||||
fm_config['name'] = 'relay_inbound'
|
||||
fm_config['active'] = True
|
||||
fedmsg.init(**fm_config)
|
||||
|
||||
import fedbadges.utils
|
||||
|
||||
|
||||
#a simple listcomp to generate a lists of searchterms
|
||||
#allows us to break the fas userlist down to smaller chunks
|
||||
#so the cron job doesn't hang
|
||||
def get_fas_searchterm():
|
||||
ast = "*"
|
||||
alpha = map(chr, range(97, 123))
|
||||
searchterms = [ term_str + ast for term_str in alpha ]
|
||||
return searchterms
|
||||
|
||||
|
||||
def get_fas_userlist(fas_credentials):
|
||||
creds = fas_credentials
|
||||
|
||||
fasclient = fedora.client.fas2.AccountSystem(
|
||||
username=creds['username'],
|
||||
password=creds['password'],
|
||||
)
|
||||
|
||||
timeout
|
||||
timeout = socket.getdefaulttimeout()
|
||||
socket.setdefaulttimeout(600)
|
||||
searchterm = get_fas_searchterms()
|
||||
for search_elem in searchterm:
|
||||
try:
|
||||
log.info("Downloading FAS cache")
|
||||
request = fasclient.send_request('/user/list',
|
||||
req_params={'search': search_elem},
|
||||
auth=True)
|
||||
fas_userlist.update(request)
|
||||
finally:
|
||||
socket.setdefaulttimeout(timeout)
|
||||
|
||||
# We don't actually check for CLA+1, just "2 groups"
|
||||
return [p for p in fas_userlist['people'] if len(p.memberships) > 1]
|
||||
|
||||
|
||||
def main():
|
||||
now = datetime.datetime.utcnow()
|
||||
year = datetime.timedelta(days=365.5)
|
||||
search_terms = get_fas_searchterms()
|
||||
mapping = {
|
||||
'egg': year * 1,
|
||||
'embryo': year * 2,
|
||||
'tadpole': year * 3,
|
||||
'tadpole-with-legs': year * 5,
|
||||
'froglet': year * 7,
|
||||
'adult-frog': year * 10,
|
||||
}
|
||||
|
||||
# First, some validation that the badge ids actually exist.
|
||||
for badge_id, delta in mapping.items():
|
||||
badge = tahrir.get_badge(badge_id=badge_id)
|
||||
assert(badge.id)
|
||||
|
||||
# Then, do a long query against FAS for our candidates.
|
||||
# looping over a list of search terms allows us to work around the socket timeout
|
||||
results = get_fas_userlist(fas_credentials=fm_config['fas_credentials'])
|
||||
|
||||
for badge_id, delta in mapping.items():
|
||||
badge = tahrir.get_badge(badge_id=badge_id)
|
||||
for person in results:
|
||||
creation = datetime.datetime.strptime(
|
||||
person.creation.split('.')[0], '%Y-%m-%d %H:%M:%S')
|
||||
if now - creation > delta:
|
||||
hit_em_up(badge, person)
|
||||
|
||||
|
||||
def hit_em_up(badge, fas_user):
|
||||
email = fas_user.username + "@fedoraproject.org"
|
||||
user = tahrir.get_person(email)
|
||||
|
||||
if not user:
|
||||
return
|
||||
|
||||
if tahrir.assertion_exists(badge.id, email):
|
||||
print email, "already has", badge.id, "skipping."
|
||||
return
|
||||
|
||||
time.sleep(1)
|
||||
print "awarding", badge.id, "to", email
|
||||
try:
|
||||
transaction.begin()
|
||||
tahrir.add_assertion(badge.id, email, None)
|
||||
transaction.commit()
|
||||
except Exception as e:
|
||||
transaction.abort()
|
||||
print "Failure:", e
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
uri = fm_config['badges_global']['database_uri']
|
||||
tahrir = TahrirDatabase(
|
||||
uri,
|
||||
notification_callback=fedbadges.utils.notification_callback,
|
||||
)
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue