Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hjfreyer committed May 30, 2014
1 parent 88a8e0c commit 4d7e02c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
14 changes: 9 additions & 5 deletions backend/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from google.appengine.ext import db, deferred

import commands
import env
import model
import templates

Expand Down Expand Up @@ -78,7 +79,7 @@ def get(self):
w.writerow([str(pledge.donationTime), pledge.amountCents])


def MakeCommandHandler(cmd):
def MakeCommandHandler(cmd_cls):
"""Takes a command and returns a route tuple which allows that command
to be executed.
"""
Expand All @@ -88,18 +89,21 @@ def get(self):
<h1>You are about to run command "{}". Are you sure?</h1>
<form action="" method="POST">
<button>Punch it</button>
</form>""".format(cmd.NAME))
</form>""".format(cmd_cls.NAME))

def post(self):
deferred.defer(cmd.run)
cmd_name = 'cmd-%s' % cmd_cls.SHORT_NAME
if cmd_name not in self.app.registry:
self.app.registry[cmd_name] = cmd_cls(self.app.config['env'])
deferred.defer(self.app.registry[cmd_name].run)
self.response.write('Command started.')

return ('/admin/command/' + cmd.SHORT_NAME, H)
return ('/admin/command/' + cmd_cls.SHORT_NAME, H)


COMMAND_HANDLERS = [MakeCommandHandler(c) for c in commands.COMMANDS]

app = webapp2.WSGIApplication([
('/admin/pledges.csv', PledgesCsvHandler),
('/admin/?', AdminDashboardHandler),
] + COMMAND_HANDLERS, debug=False)
] + COMMAND_HANDLERS, debug=False, config=dict(env=env.get_env()))
54 changes: 47 additions & 7 deletions backend/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
from collections import defaultdict

from google.appengine.ext import db
from google.appengine.ext import deferred

import model


class Error(Exception): pass

class Command(object):
def __init__(self, env):
self.env = env


# Format for a command. SHORT_NAME should be URL-safe. You can execute
# the command by going to "/admin/command/$SHORT_NAME".
Expand All @@ -19,17 +24,18 @@ class Error(Exception): pass
# minute deadline. Make it count, or if it takes longer than that, use
# deferred chaining by catching DeadlineExceededError and deferring a
# call to yourself which picks up where you left off.
class TestCommand(object):
class TestCommand(Command):
SHORT_NAME = 'test'
NAME = 'Perform a test'
SHOW = False
SHOW = True

def run(self):
logging.info('Do something')
logging.info(self.env.stripe_public_key)


# Populates the MissingDataUsersSecondary table.
class FindMissingDataUsersCommand(object):
class FindMissingDataUsersCommand(Command):
SHORT_NAME = 'find_missing_data_users'
NAME = 'Recompute missing data users'
SHOW = False
Expand Down Expand Up @@ -61,7 +67,7 @@ def run(self):
logging.info('Done')


class UpdateSecretsProperties(object):
class UpdateSecretsProperties(Command):
SHORT_NAME = 'update_secrets_properties'
NAME = 'Update "Secrets" model properties'
SHOW = True
Expand All @@ -70,9 +76,43 @@ def run(self):
model.Secrets.update()


class ChargeRequested(Command):
SHORT_NAME = 'execute_requested_charges'
NAME = 'Execute requested charges'
SHOW = True

def run(self):
for charge_status in model.ChargeStatus.all().filter('start_time =', None):
deferred.defer(self.charge_one,
charge_status.key().parent(),
_queue='stripeCharge')

def charge_one(self, pledge_key):
model.ChargeStatus.execute(self.env.stripe_backend, pledge_key)


class RequestAllWpPledges(Command):
SHORT_NAME = 'request_all_wp'
NAME = SHORT_NAME
SHOW = True

def run(self):
logging.info('Start')
pledge_keys = set(str(k) for k in model.WpPledge.all(keys_only=True))
requested_keys = set(str(k.parent())
for k in model.ChargeStatus.all(keys_only=True))
needed_keys = pledge_keys.difference(requested_keys)
logging.info('Loaded')
for pledge_key in needed_keys:
model.ChargeStatus.request(db.Key(pledge_key))
logging.info('Done')


# List your command here so admin.py can expose it.
COMMANDS = [
TestCommand(),
FindMissingDataUsersCommand(),
UpdateSecretsProperties(),
TestCommand,
FindMissingDataUsersCommand,
UpdateSecretsProperties,
RequestAllWpPledges,
ChargeRequested,
]
4 changes: 4 additions & 0 deletions backend/queue.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ queue:

- name: incrementTotal
rate: 60/s

- name: stripeCharge
rate: 40/m
max_concurrent_requests: 5

0 comments on commit 4d7e02c

Please sign in to comment.