本文整理汇总了Python中google.appengine.ext.deferred.defer方法的典型用法代码示例。如果您正苦于以下问题:Python deferred.defer方法的具体用法?Python deferred.defer怎么用?Python deferred.defer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.appengine.ext.deferred
的用法示例。
在下文中一共展示了deferred.defer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _run_function_as_task
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def _run_function_as_task(all_functions_list, function_name, kwargs=None):
"""Runs a specific function and its kwargs as an AppEngine task.
Args:
all_functions_list: string list, A list with all function names that are
registered as bootstrap functions on the Loaner app.
function_name: string, A specific function that should be ran as a task.
kwargs: dict, Optional kwargs to be passed to the function that will run.
Returns:
The deferred task from AppEngine taskqueue.
Raises:
Error: if requested bootstrap method is not allowed or does not exist.
"""
logging.debug('Running %s as a task.', function_name)
function = all_functions_list.get(function_name)
if function is None:
raise Error(
'Requested bootstrap method {} does not exist.'.format(function_name))
if not kwargs:
kwargs = {}
kwargs['user_email'] = user.get_user_email()
return deferred.defer(function, **kwargs)
示例2: send_mail_notification
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def send_mail_notification(subject, body, to=None, **kwargs):
if not config.CONFIG_DB.feedback_email:
return
brand_name = config.CONFIG_DB.brand_name
sender = '%s <%s>' % (brand_name, config.CONFIG_DB.feedback_email)
subject = '[%s] %s' % (brand_name, subject)
if config.DEVELOPMENT:
logging.info(
'\n'
'######### Deferring sending this email: #############################'
'\nFrom: %s\nTo: %s\nSubject: %s\n\n%s\n'
'#####################################################################',
sender, to or sender, subject, body
)
deferred.defer(mail.send_mail, sender, to or sender, subject, body, **kwargs)
###############################################################################
# Admin Notifications
###############################################################################
示例3: repo_cleanup
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def repo_cleanup(days, cursor=None):
before_date = datetime.utcnow() - timedelta(days=days)
repo_qry = model.Repo.query().filter(model.Repo.modified < before_date)
repo_keys, repo_cursors = util.get_dbs(
repo_qry,
order='modified',
keys_only=True,
cursor=cursor,
)
ndb.delete_multi(repo_keys)
if repo_cursors['next']:
deferred.defer(repo_cleanup, days, repo_cursors['next'])
###############################################################################
# Account Clean-ups
###############################################################################
示例4: MakeCommandHandler
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def MakeCommandHandler(cmd_cls):
"""Takes a command class and returns a route tuple which allows that command
to be executed.
"""
class H(webapp2.RequestHandler):
def get(self):
self.response.write("""
<h1>You are about to run command "{}". Are you sure?</h1>
<form action="" method="POST">
<button>Punch it</button>
</form>""".format(self._get_cmd().NAME))
def post(self):
deferred.defer(self._get_cmd().run)
self.response.write('Command started.')
def _get_cmd(self):
if 'cmds' not in self.app.registry:
self.app.registry['cmds'] = {}
if cmd_cls.SHORT_NAME not in self.app.registry['cmds']:
self.app.registry['cmds'][cmd_cls.SHORT_NAME] = cmd_cls(self.app.config)
return self.app.registry['cmds'][cmd_cls.SHORT_NAME]
return ('/admin/command/' + cmd_cls.SHORT_NAME, H)
示例5: _perform_backup_complete
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def _perform_backup_complete(
operation, job_id, kind, backup_info_pk, gcs_path_prefix, filenames, queue):
backup_info = BackupInformation.get(backup_info_pk)
if backup_info:
if job_id in backup_info.active_jobs:
backup_info.active_jobs.remove(job_id)
backup_info.completed_jobs = list(
set(backup_info.completed_jobs + [job_id]))
filenames = [GCSUtil.add_gs_prefix_if_missing(name) for name in filenames]
kind_backup_files = backup_info.get_kind_backup_files([kind])[0]
if kind_backup_files:
kind_backup_files.files = list(set(kind_backup_files.files + filenames))
else:
kind_backup_files = backup_info.create_kind_backup_files(kind, filenames)
db.put((backup_info, kind_backup_files), force_writes=True)
if operation.status == utils.DatastoreAdminOperation.STATUS_COMPLETED:
deferred.defer(finalize_backup_info, backup_info.key(),
gcs_path_prefix,
_url=config.DEFERRED_PATH,
_queue=queue,
_transactional=True)
else:
logging.warn('BackupInfo was not found for %s', backup_info_pk)
示例6: Dispatch
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def Dispatch():
"""Dispatches per-host tasks onto the event processing queue."""
total_dispatch_count = 0
logging.info('Starting a new dispatch task')
# Query for all distinct host_id values among the _UnsyncedEvents, in batches,
# either until we run out, or the task nears its deadline.
query = _UnsyncedEvent.query(
projection=[_UnsyncedEvent.host_id], distinct=True)
for event_page in datastore_utils.Paginate(query, page_size=25):
host_ids = [event.host_id for event in event_page]
for host_id in host_ids:
deferred.defer(Process, host_id, _queue=constants.TASK_QUEUE.BIT9_PROCESS)
total_dispatch_count += 1
logging.info('Dispatched %d task(s)', total_dispatch_count)
示例7: get
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def get(self):
now = datetime.datetime.utcnow()
# Notify all users whose Exemptions now have less than a week left, in order
# to give reasonable advance warning (e.g. long weekends, holidays, etc).
one_week_start_dt = now + datetime.timedelta(days=7, hours=-1)
one_week_end_dt = now + datetime.timedelta(days=7)
# Notify all users whose Exemptions now have less that 24 hours left. This
# will act as a final reminder, and will also ensure that even users who
# choose a 1-day Exemption will get an email warning (for what it's worth).
one_day_start_dt = now + datetime.timedelta(days=1, hours=-1)
one_day_end_dt = now + datetime.timedelta(days=1)
tuples = [
(one_week_start_dt, one_week_end_dt),
(one_day_start_dt, one_day_end_dt)]
# Defer a task for each batch of notifications.
for start_dt, end_dt in tuples:
deferred.defer(
_NotifyExpirationsInRange, start_dt, end_dt,
_queue=constants.TASK_QUEUE.EXEMPTIONS)
示例8: cron
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def cron():
stories = topstories()
chunks = chunkify(stories, 20)
for chunk in chunks:
deferred.defer(task, chunk)
return 'OK'
示例9: clear
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def clear(self, request):
"""Clears a search index for the given type."""
if request.model == search_messages.SearchIndexEnum.DEVICE:
deferred.defer(device_model.Device.clear_index)
elif request.model == search_messages.SearchIndexEnum.SHELF:
deferred.defer(shelf_model.Shelf.clear_index)
return message_types.VoidMessage()
示例10: reindex
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def reindex(self, request):
"""Reindexes a search index for the given type."""
if request.model == search_messages.SearchIndexEnum.DEVICE:
deferred.defer(device_model.Device.index_entities_for_search)
elif request.model == search_messages.SearchIndexEnum.SHELF:
deferred.defer(shelf_model.Shelf.index_entities_for_search)
return message_types.VoidMessage()
示例11: enable_guest_mode
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def enable_guest_mode(self, user_email):
"""Moves a device into guest mode if allowed.
Args:
user_email: str, The email of the acting user.
Raises:
GuestNotAllowedError: when the allow_guest_mode config is not True.
EnableGuestError: if there is an RPC error in the Directory API, or the
allow_guest_mode setting is not True.
UnassignedDeviceError: if the device is not assigned, guest mode should
not be allowed.
"""
if not self.is_assigned:
raise UnassignedDeviceError(_UNASSIGNED_DEVICE % self.identifier)
if config_model.Config.get('allow_guest_mode'):
directory_client = directory.DirectoryApiClient(user_email)
guest_ou = constants.ORG_UNIT_DICT['GUEST']
try:
directory_client.move_chrome_device_org_unit(
device_id=self.chrome_device_id,
org_unit_path=guest_ou)
except directory.DirectoryRPCError as err:
raise EnableGuestError(str(err))
else:
self.current_ou = guest_ou
self.ou_changed_date = datetime.datetime.utcnow()
self.stream_to_bq(
user_email, 'Moving device %s into Guest Mode.' % self.identifier)
self.put()
if config_model.Config.get('timeout_guest_mode'):
countdown = datetime.timedelta(
hours=config_model.Config.get(
'guest_mode_timeout_in_hours')).total_seconds()
deferred.defer(
self._disable_guest_mode, user_email, _countdown=countdown)
else:
raise GuestNotAllowedError(_GUEST_MODE_DISABLED_MSG)
示例12: _pre_delete_hook
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def _pre_delete_hook(cls, key):
"""Cleans up any entities that reference the key.
Note that this operation can have a long tail in that it requires a bulk tag
disassociation across potentially many entities in Datastore.
Args:
key: ndb.Key, a Tag model key.
"""
logging.info(
'Destroying the tag with urlsafe key %r and name %r.',
key.urlsafe(), key.get().name)
for model in _MODELS_WITH_TAGS:
deferred.defer(_delete_tags, model, key.get())
示例13: _delete_tags
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def _delete_tags(model, tag, cursor=None, num_updated=0, batch_size=100):
"""Cleans up any entities on the given model that reference the given key.
Args:
model: ndb.Model, a Model with a repeated TagData property.
tag: Tag, an instance of a Tag model.
cursor: Optional[datastore_query.Cursor], pointing to the last result.
num_updated: int, the number of entities that were just updated.
batch_size: int, the number of entities to include in the batch.
"""
entities, next_cursor, more = model.query(
model.tags.tag == tag).fetch_page(batch_size, start_cursor=cursor)
for entity in entities:
entity.tags = [
model_tag for model_tag in entity.tags if model_tag.tag != tag
]
ndb.put_multi(entities)
num_updated += len(entities)
logging.info(
'Destroyed %d occurrence(s) of the tag with URL safe key %r',
len(entities), tag.key.urlsafe())
if more:
deferred.defer(
_delete_tags, model, tag,
cursor=next_cursor, num_updated=num_updated, batch_size=batch_size)
else:
logging.info(
'Destroyed a total of %d occurrence(s) of the tag with URL safe key %r',
num_updated, tag.key.urlsafe())
示例14: get_test_set_from_results_str
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def get_test_set_from_results_str(category, results_str):
"""Creates a runtime version of a browserscope TestSet by parsing strings.
Args:
category: A string that looks like 'usertest_sad7dsa987sa9dsa7dsa9'.
results_str: A string that looks like 'test_1=0,test_2=1'.
Returns:
A models.user_test.TestSet instance.
Raises:
KeyTooLong: When any of the key names is longer than MAX_KEY_LENGTH.
"""
category_prefix = '%s_' % Test.get_prefix()
if category_prefix not in category:
return None
key = category.replace(category_prefix, '')
test = Test.get_mem(key)
if not test:
return None
test_scores = [x.split('=') for x in str(results_str).split(',')]
test_keys = sorted([x[0] for x in test_scores])
ValidateTestKeys(test_keys)
# If it's test run #1, save what we've got for test keys and swap
# memcache.
if not test.test_keys:
test.test_keys = test_keys
test.save_memcache()
else:
deferred.defer(update_test_keys, key, test_keys)
# Regardless we'll defer updating the TestMeta reference.
deferred.defer(update_test_meta, key, test_scores)
test_set = test.get_test_set_from_test_keys(test_keys)
return test_set
示例15: queue_account
# 需要导入模块: from google.appengine.ext import deferred [as 别名]
# 或者: from google.appengine.ext.deferred import defer [as 别名]
def queue_account(account_db):
import logging
if account_db.status in ['404']:
return
max_repos = 3000
queue_it = False
delta = datetime.utcnow() - account_db.synced
if account_db.status in ['new', 'error']:
account_db.status = 'syncing'
account_db.synced = datetime.utcnow()
account_db.put()
queue_it = True
elif delta.days > 0 and account_db.status == 'failed' and account_db.public_repos < max_repos:
account_db.status = 'syncing'
account_db.synced = datetime.utcnow()
account_db.put()
queue_it = True
elif account_db.status == 'syncing':
if delta.seconds > 60 * 60 or account_db.public_repos > max_repos:
account_db.status = 'failed'
account_db.synced = datetime.utcnow()
account_db.put()
elif delta.seconds > 30 * 60:
queue_it = True
# older than 4 hours long sunc them
if (delta.days > 0 or delta.seconds > 60 * 60 * 4) and account_db.status != 'failed':
account_db.status = 'syncing'
account_db.synced = datetime.utcnow()
account_db.put()
queue_it = True
if queue_it:
deferred.defer(sync_account, account_db)