def set_json(self, json_obj, conf): """Sets the JSON value of this object. If the JSON value is stored in the Blobstore, a task will be added to eventually delete the blob. Note that you also have to put() the model after calling this function to actually update it. Args: json_obj: The JSON-convertible object to set. conf: The configuration to use. Raises: ValueError: If this object does not have a key. """ if not self.key: raise ValueError("Set the object's key before calling set_json().") data = json.dumps(json_obj) if len(data) <= _MAX_JSON_BYTES: self.data = data else: self.data = None file_name = files.blobstore.create() with files.open(file_name, 'a') as f: f.write(data) files.finalize(file_name) self.blob_key = files.blobstore.get_blob_key(file_name) deferred.defer(_delete_blob_if_done, self.key, self.blob_key, conf, _queue=conf.test_queue, _countdown=_DELETE_BLOB_TIME_SECS)
def _delete_batch(batch_key, prev_done, conf): """Deletes the given batch and its tasks if no progress has been made. Otherwise, it will check back in _DELETE_TIME_SECS seconds. Note that any blobs used in this batch will eventually be deleted after their respective TestBatch/RunTestUnitTask objects are deleted; see models._delete_blob_if_done(). Args: batch_key: The key to the TestBatch to delete. prev_done: How many tests were done _DELETE_TIME_SECS seconds ago. If the number of completed tests has not increased, the batch is deleted. conf: The configuration to use. """ ctx_options = models.get_ctx_options(conf) batch = batch_key.get(**ctx_options) if batch is None: return # For idempotency. utils.check_type(batch, 'batch', models.TestBatch) tasks = [task for task in batch.get_tasks(conf) if task] num_done = len(tasks) if num_done == prev_done: task_keys = [task.key for task in tasks] ndb.delete_multi([batch_key] + task_keys, **ctx_options) else: deferred.defer(_delete_batch, batch_key, num_done, conf, _queue=conf.test_queue, _countdown=_DELETE_TIME_SECS)
def _delete_blob_if_done(obj_key, blob_key, conf): """Deletes a blob if its object has also been deleted. Otherwise, it will try to delete the blob later. Args: obj_key: The ndb.Key of a JsonHolder. blob_key: The BlobKey to delete. conf: The configuration to use. """ if obj_key.get(**get_ctx_options(conf)): deferred.defer(_delete_blob_if_done, obj_key, blob_key, conf, _queue=conf.test_queue, _countdown=_DELETE_BLOB_TIME_SECS) else: info = blobstore.BlobInfo.get(blob_key) if info: info.delete()