code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
outer_definition = cls.message_definition() if not outer_definition: return util.get_package_for_module(cls.__module__) return outer_definition.definition_package()
def definition_package(cls)
Helper method for creating creating the package of a definition. Returns: Name of package that definition belongs to.
6.621135
6.502505
1.018244
return dict((item.name, item.number) for item in iter(cls))
def to_dict(cls)
Make dictionary version of enumerated class. Dictionary created this way can be used with def_num. Returns: A dict (name) -> number
10.329203
9.958587
1.037216
for name, field in self.__by_name.items(): value = getattr(self, name) if value is None: if field.required: raise ValidationError( "Message %s is missing required field %s" % (type(self).__name__...
def check_initialized(self)
Check class for initialization status. Check that all required fields are initialized Raises: ValidationError: If message is not initialized.
2.700298
2.47115
1.092729
message_type = type(self) try: field = message_type.field_by_name(name) except KeyError: raise AttributeError('Message %s has no field %s' % ( message_type.__name__, name)) return self.__tags.get(field.number)
def get_assigned_value(self, name)
Get the assigned value of an attribute. Get the underlying value of an attribute. If value has not been set, will not return the default for the field. Args: name: Name of attribute to get. Returns: Value of attribute, None if it has not been set.
3.588796
4.360319
0.823058
message_type = type(self) try: field = message_type.field_by_name(name) except KeyError: if name not in message_type.__by_name: raise AttributeError('Message %s has no field %s' % ( message_type.__name__, name)) if fiel...
def reset(self, name)
Reset assigned value for field. Resetting a field will return it to its default value or None. Args: name: Name of field to reset.
3.494228
3.856391
0.906087
value, variant = self.__unrecognized_fields.get(key, (value_default, variant_default)) return value, variant
def get_unrecognized_field_info(self, key, value_default=None, variant_default=None)
Get the value and variant of an unknown field in this message. Args: key: The name or number of the field to retrieve. value_default: Value to be returned if the key isn't found. variant_default: Value to be returned as variant if the key isn't found. Returns:...
3.442383
4.703367
0.731897
if not isinstance(variant, Variant): raise TypeError('Variant type %s is not valid.' % variant) self.__unrecognized_fields[key] = value, variant
def set_unrecognized_field(self, key, value, variant)
Set an unrecognized field, used when decoding a message. Args: key: The name or number used to refer to this unknown value. value: The value of the field. variant: Type information needed to interpret the value or re-encode it. Raises: TypeError: If ...
4.445238
4.737941
0.938221
self.__field.validate_element(value) return list.append(self, value)
def append(self, value)
Validate item appending to list.
11.373289
8.729624
1.302838
self.__field.validate(sequence) return list.extend(self, sequence)
def extend(self, sequence)
Validate extension of list.
13.899603
8.684708
1.600469
self.__field.validate_element(value) return list.insert(self, index, value)
def insert(self, index, value)
Validate item insertion to list.
9.037871
7.463728
1.210906
if not isinstance(value, self.type): # Authorize int values as float. if isinstance(value, six.integer_types) and self.type == float: return float(value) if value is None: if self.required: raise ValidationError('...
def validate_element(self, value)
Validate single element of field. This is different from validate in that it is used on individual values of repeated fields. Args: value: Value to validate. Returns: The value casted in the expected type. Raises: ValidationError if value is not ...
2.946279
2.919302
1.009241
if not self.repeated: return validate_element(value) else: # Must be a list or tuple, may not be a string. if isinstance(value, (list, tuple)): result = [] for element in value: if element is None: ...
def __validate(self, value, validate_element)
Internal validation function. Validate an internal value using a function to validate individual elements. Args: value: Value to validate. validate_element: Function to use to validate individual elements. Raises: ValidationError if value is not expected ...
2.659888
2.680938
0.992148
# If value is str is it considered valid. Satisfies "required=True". if isinstance(value, bytes): try: six.text_type(value, 'UTF-8') except UnicodeDecodeError as err: try: _ = self.name except Attribute...
def validate_element(self, value)
Validate StringField allowing for str and unicode. Raises: ValidationError if a str value is not UTF-8.
3.588481
3.35872
1.068407
if self.__type is None: message_type = find_definition( self.__type_name, self.message_definition()) if not (message_type is not Message and isinstance(message_type, type) and issubclass(message_type, Message)): ...
def type(self)
Message type used for field.
4.533702
3.786701
1.19727
if not isinstance(message, self.message_type): raise DecodeError('Expected type %s, got %s: %r' % (self.message_type.__name__, type(message).__name__, message)) return message
def value_from_message(self, message)
Convert a message to a value instance. Used by deserializers to convert from underlying messages to value of expected user type. Args: message: A message instance of type self.message_type. Returns: Value of self.message_type.
2.931603
3.157708
0.928396
if not isinstance(value, self.type): raise EncodeError('Expected type %s, got %s: %r' % (self.type.__name__, type(value).__name__, value)) return value
def value_to_message(self, value)
Convert a value instance to a message. Used by serializers to convert Python user types to underlying messages for transmission. Args: value: A value of type self.type. Returns: An instance of type self.message_type.
3.263709
3.4271
0.952324
if isinstance(value, (six.string_types, six.integer_types)): # Validation of the value does not happen for delayed resolution # enumerated types. Ignore if type is not yet resolved. if self.__type: self.__type(value) return value ...
def validate_default_element(self, value)
Validate default element of Enum field. Enum fields allow for delayed resolution of default values when the type of the field has not been resolved. The default value of a field may be a string or an integer. If the Enum type of the field has been resolved, the default value is ...
7.382207
6.515931
1.132947
if self.__type is None: found_type = find_definition( self.__type_name, self.message_definition()) if not (found_type is not Enum and isinstance(found_type, type) and issubclass(found_type, Enum)): raise Fie...
def type(self)
Enum type used for field.
4.830503
4.036363
1.196746
try: return self.__resolved_default except AttributeError: resolved_default = super(EnumField, self).default if isinstance(resolved_default, (six.string_types, six.integer_types)): # pylint:disable=...
def default(self)
Default for enum field. Will cause resolution of Enum type and unresolved default value.
3.186889
2.849088
1.118565
def Register(cls): _CUSTOM_MESSAGE_CODECS[cls] = _Codec(encoder=encoder, decoder=decoder) return cls return Register
def RegisterCustomMessageCodec(encoder, decoder)
Register a custom encoder/decoder for this message class.
4.372041
4.243657
1.030253
def Register(field): _CUSTOM_FIELD_CODECS[field] = _Codec(encoder=encoder, decoder=decoder) return field return Register
def RegisterCustomFieldCodec(encoder, decoder)
Register a custom encoder/decoder for this field.
4.818911
4.782876
1.007534
def Register(field_type): _FIELD_TYPE_CODECS[field_type] = _Codec( encoder=encoder, decoder=decoder) return field_type return Register
def RegisterFieldTypeCodec(encoder, decoder)
Register a custom encoder/decoder for all fields of this type.
3.758915
4.073177
0.922846
result = _ProtoJsonApiTools.Get().encode_message(message) return _IncludeFields(result, message, include_fields)
def MessageToJson(message, include_fields=None)
Convert the given message to JSON.
20.840071
23.076933
0.903069
items = properties.items() if sort_items: items = sorted(items) map_ = [] for key, value in items: map_.append(additional_property_type.AdditionalProperty( key=key, value=value)) return additional_property_type(additionalProperties=map_)
def DictToAdditionalPropertyMessage(properties, additional_property_type, sort_items=False)
Convert the given dictionary to an AdditionalProperty message.
2.292591
2.317397
0.989296
# TODO(jasmuth): craigcitro suggests a pretty-printer from apitools/gen. indent = kwargs.get('indent', 0) def IndentKwargs(kwargs): kwargs = dict(kwargs) kwargs['indent'] = kwargs.get('indent', 0) + 4 return kwargs if isinstance(msg, list): s = '[' for it...
def MessageToRepr(msg, multiline=False, **kwargs)
Return a repr-style string for a protorpc message. protorpc.Message.__repr__ does not return anything that could be considered python code. Adding this function lets us print a protorpc message in such a way that it could be pasted into code later, and used to compare against other things. Args: ...
2.488763
2.386808
1.042716
if include_fields is None: return encoded_message result = json.loads(encoded_message) for field_name in include_fields: try: value = _GetField(message, field_name.split('.')) nullvalue = None if isinstance(value, list): nullvalue = []...
def _IncludeFields(encoded_message, message, include_fields)
Add the requested fields to the encoded message.
2.797394
2.847429
0.982428
destination = _UNRECOGNIZED_FIELD_MAPPINGS.get(type(message)) if destination is None: return message pair_field = message.field_by_name(destination) if not isinstance(pair_field, messages.MessageField): raise exceptions.InvalidDataFromServerError( 'Unrecognized fields mu...
def _DecodeUnknownFields(message, encoded_message)
Rewrite unknown fields in message into message.destination.
5.643299
5.551672
1.016504
field_type = pair_type.value.type new_values = [] all_field_names = [x.name for x in message.all_fields()] for name, value_dict in six.iteritems(encoded_message): if name in all_field_names: continue value = PyValueToMessage(field_type, value_dict) if pair_type.v...
def _DecodeUnknownMessages(message, encoded_message, pair_type)
Process unknown fields in encoded_message of a message type.
3.393642
3.335556
1.017414
new_values = [] codec = _ProtoJsonApiTools.Get() for unknown_field in message.all_unrecognized_fields(): # TODO(craigcitro): Consider validating the variant if # the assignment below doesn't take care of it. It may # also be necessary to check it in the case that the # t...
def _DecodeUnrecognizedFields(message, pair_type)
Process unrecognized fields in message.
4.696662
4.725658
0.993864
source = _UNRECOGNIZED_FIELD_MAPPINGS.get(type(message)) if source is None: return message # CopyProtoMessage uses _ProtoJsonApiTools, which uses this message. Use # the vanilla protojson-based copy function to avoid infinite recursion. result = _CopyProtoMessageVanillaProtoJson(message...
def _EncodeUnknownFields(message)
Remap unknown fields in message out of message.source.
5.25918
5.195466
1.012263
try: if field.repeated: result = [base64.urlsafe_b64encode(byte) for byte in value] else: result = base64.urlsafe_b64encode(value) complete = True except TypeError: result = value complete = False return CodecResult(value=result, complete=...
def _SafeEncodeBytes(field, value)
Encode the bytes in value as urlsafe base64.
2.703897
2.480128
1.090225
try: result = base64.urlsafe_b64decode(str(value)) complete = True except TypeError: result = value complete = False return CodecResult(value=result, complete=complete)
def _SafeDecodeBytes(unused_field, value)
Decode the urlsafe base64 value into bytes.
3.488965
3.204275
1.088847
if not encoded_message: return message decoded_message = json.loads(six.ensure_str(encoded_message)) for field in message.all_fields(): if (isinstance(field, messages.EnumField) and field.name in decoded_message and message.get_assigned_value(field.name) ...
def _ProcessUnknownEnums(message, encoded_message)
Add unknown enum values from encoded_message as unknown fields. ProtoRPC diverges from the usual protocol buffer behavior here and doesn't allow unknown fields. Throwing on unknown fields makes it impossible to let servers add new enum values and stay compatible with older clients, which isn't reasonab...
3.169204
3.103385
1.021209
if not encoded_message: return message decoded_message = json.loads(six.ensure_str(encoded_message)) message_fields = [x.name for x in message.all_fields()] + list( message.all_unrecognized_fields()) missing_fields = [x for x in decoded_message.keys() if x not ...
def _ProcessUnknownMessages(message, encoded_message)
Store any remaining unknown fields as strings. ProtoRPC currently ignores unknown values for which no type can be determined (and logs a "No variant found" message). For the purposes of reserializing, this is quite harmful (since it throws away information). Here we simply add those as unknown fields o...
2.99649
2.809792
1.066445
raise exceptions.TypecheckError( 'Cannot set JSON enum mapping for non-enum "%s"' % enum_type) if python_name not in enum_type.names(): raise exceptions.InvalidDataError( 'Enum value %s not a value for type %s' % (python_name, enum_type)) field_mappings = _JSON_ENUM_MAPPI...
def AddCustomJsonEnumMapping(enum_type, python_name, json_name, package=None): # pylint: disable=unused-argument if not issubclass(enum_type, messages.Enum)
Add a custom wire encoding for a given enum value. This is primarily used in generated code, to handle enum values which happen to be Python keywords. Args: enum_type: (messages.Enum) An enum type python_name: (basestring) Python name for this value. json_name: (basestring) JSON name to ...
4.362165
4.712683
0.925622
raise exceptions.TypecheckError( 'Cannot set JSON field mapping for ' 'non-message "%s"' % message_type) try: _ = message_type.field_by_name(python_name) except KeyError: raise exceptions.InvalidDataError( 'Field %s not recognized for type %s' % ( ...
def AddCustomJsonFieldMapping(message_type, python_name, json_name, package=None): # pylint: disable=unused-argument if not issubclass(message_type, messages.Message)
Add a custom wire encoding for a given message field. This is primarily used in generated code, to handle enum values which happen to be Python keywords. Args: message_type: (messages.Message) A message type python_name: (basestring) Python name for this value. json_name: (basestring) JS...
3.773499
4.182185
0.902279
return _FetchRemapping(enum_type, 'enum', python_name=python_name, json_name=json_name, mappings=_JSON_ENUM_MAPPINGS)
def GetCustomJsonEnumMapping(enum_type, python_name=None, json_name=None)
Return the appropriate remapping for the given enum, or None.
6.304752
5.019571
1.256034
return _FetchRemapping(message_type, 'field', python_name=python_name, json_name=json_name, mappings=_JSON_FIELD_MAPPINGS)
def GetCustomJsonFieldMapping(message_type, python_name=None, json_name=None)
Return the appropriate remapping for the given field, or None.
6.313835
4.924222
1.2822
if python_name and json_name: raise exceptions.InvalidDataError( 'Cannot specify both python_name and json_name ' 'for %s remapping' % mapping_type) if not (python_name or json_name): raise exceptions.InvalidDataError( 'Must specify either python_name or ...
def _FetchRemapping(type_name, mapping_type, python_name=None, json_name=None, mappings=None)
Common code for fetching a key or value from a remapping dict.
2.187247
2.118421
1.032489
if mapping_type == 'field': getter = GetCustomJsonFieldMapping elif mapping_type == 'enum': getter = GetCustomJsonEnumMapping remapping = getter(message_type, python_name=python_name) if remapping is not None and remapping != json_name: raise exceptions.InvalidDataError( ...
def _CheckForExistingMappings(mapping_type, message_type, python_name, json_name)
Validate that no mappings exist for the given values.
2.213546
2.194616
1.008626
# This really needs to live in extra_types, but extra_types needs # to import this file to be able to register codecs. # TODO(craigcitro): Split out a codecs module and fix this ugly # import. from apitools.base.py import extra_types def _IsRepeatedJsonValue(msg): if isins...
def _AsMessageList(msg)
Convert the provided list-as-JsonValue to a list.
5.194544
4.979972
1.043087
value = message.get_assigned_value(field.name) if not isinstance(value, messages.Message): return False try: additional_properties = value.field_by_name('additionalProperties') except KeyError: return False else: return additional_properties.repeated
def _IsMap(message, field)
Returns whether the "field" is actually a map-type.
3.658957
3.408329
1.073534
assert _IsMap(message, field) map_message = message.get_assigned_value(field.name) additional_properties = map_message.get_assigned_value( 'additionalProperties') for kv_pair in additional_properties: yield kv_pair.key, kv_pair.value
def _MapItems(message, field)
Yields the (key, value) pair of the map values.
4.585144
4.082939
1.123001
# This is a primitive leaf, no errors found down this path. return field_names = message.all_unrecognized_fields() if field_names: # This message is malformed. Stop recursing and report it. yield _edges, field_names return # Recurse through all fields in the current...
def UnrecognizedFieldIter(message, _edges=()): # pylint: disable=invalid-name if not isinstance(message, messages.Message)
Yields the locations of unrecognized fields within "message". If a sub-message is found to have unrecognized fields, that sub-message will not be searched any further. We prune the search of the sub-message because we assume it is malformed and further checks will not yield productive errors. Args...
2.856856
2.845767
1.003897
for decoder in _GetFieldCodecs(field, 'decoder'): result = decoder(field, value) value = result.value if result.complete: return value if isinstance(field, messages.MessageField): field_value = self.decode_message( ...
def decode_field(self, field, value)
Decode the given JSON value. Args: field: a messages.Field for the field we're decoding. value: a python value we'd like to decode. Returns: A value suitable for assignment to field.
3.973898
4.006192
0.991939
for encoder in _GetFieldCodecs(field, 'encoder'): result = encoder(field, value) value = result.value if result.complete: return value if isinstance(field, messages.EnumField): if field.repeated: remapped_value = [G...
def encode_field(self, field, value)
Encode the given value as JSON. Args: field: a messages.Field for the field we're encoding. value: a value for field. Returns: A python value suitable for json.dumps.
4.41792
4.333578
1.019462
# Use slices instead of an iterator when we have a flat list if isinstance(iterator, list): length = len(iterator) for i in range(int(math.ceil(old_div(float(length), n)))): yield iterator[i * n: (i + 1) * n] else: accumulator = [] for item in iterator: ...
def group_iter(iterator, n=2)
Given an iterator, it returns sub-lists made of n items. (except the last that can have len < n)
3.068973
3.084472
0.994975
class memodict(dict): def __getitem__(self, *key): return dict.__getitem__(self, key) def __missing__(self, key): ret = self[key] = f(*key) return ret return memodict().__getitem__
def memoize(f)
Memoization decorator for a function taking one or more arguments.
1.954061
1.915194
1.020294
class memodict(dict): def __missing__(self, key): ret = self[key] = f(key) return ret return memodict().__getitem__
def memoize_single_argument(f)
Memoization decorator for a function taking a single argument
2.290882
2.344671
0.977059
return getattr( importlib.import_module( re.sub( r"\.[^.]+$", "", taskpath)), re.sub( r"^.*\.", "", taskpath))
def load_class_by_path(taskpath)
Given a taskpath, returns the main task class.
3.730915
3.346429
1.114895
import socket import errno s = socket.socket() if timeout: from time import time as now # time module is needed to calc timeout shared between two exceptions end = now() + timeout while True: try: if timeout: next_timeout = end - now...
def wait_for_net_service(server, port, timeout=None, poll_interval=0.1)
Wait for network service to appear @param timeout: in seconds, if None or 0 wait forever @return: True of False, if timeout is None may return only True or throw unhandled network exception
3.663835
3.646083
1.004869
from .queue import Queue queue_obj = Queue(queue) queue_obj.enqueue_raw_jobs(params_list, **kwargs)
def queue_raw_jobs(queue, params_list, **kwargs)
Queue some jobs on a raw queue
3.969004
4.142934
0.958018
if len(params_list) == 0: return [] if queue is None: task_def = context.get_current_config().get("tasks", {}).get(main_task_path) or {} queue = task_def.get("queue", "default") from .queue import Queue queue_obj = Queue(queue) if queue_obj.is_raw: raise Except...
def queue_jobs(main_task_path, params_list, queue=None, batch_size=1000)
Queue multiple jobs on a regular queue
3.869026
3.807317
1.016208
if self.id is None: return self if full_data is True: fields = None elif isinstance(full_data, dict): fields = full_data else: fields = { "_id": 0, "path": 1, "params": 1, ...
def fetch(self, start=False, full_data=True)
Get the current job data and possibly flag it as started.
3.722093
3.588688
1.037174
if not self.saved and self.data and "progress" in self.data: # TODO should we save more fields? self.collection.update({"_id": self.id}, {"$set": { "progress": self.data["progress"] }}) self.saved = True
def save(self)
Persists the current job metadata to MongoDB. Will be called at each worker report.
5.066667
3.976207
1.274246
now = datetime.datetime.utcnow() for data in jobs_data: if data["status"] == "started": data["datestarted"] = now no_storage = (statuses_no_storage is not None) and ("started" in statuses_no_storage) if no_storage and return_jobs: for da...
def insert(cls, jobs_data, queue=None, statuses_no_storage=None, return_jobs=True, w=None, j=None)
Insert a job into MongoDB
2.79873
2.778944
1.00712
original_exception = sys.exc_info() if original_exception[0] is not None: exc.original_exception = original_exception
def _attach_original_exception(self, exc)
Often, a retry will be raised inside an "except" block. This Keep track of the first exception for debugging purposes
3.72169
3.794581
0.980791
max_retries = max_retries if max_retries is None: max_retries = self.max_retries if self.data.get("retry_count", 0) >= max_retries: raise MaxRetriesInterrupt() exc = RetryInterrupt() exc.queue = queue or self.queue or self.data.get("queue") or...
def retry(self, queue=None, delay=None, max_retries=None)
Marks the current job as needing to be retried. Interrupts it.
3.058318
2.929304
1.044042
if not queue: if not self.data or not self.data.get("queue"): self.fetch(full_data={"_id": 0, "queue": 1, "path": 1}) queue = self.data["queue"] self._save_status("queued", updates={ "queue": queue, "datequeued": datetime.datetim...
def requeue(self, queue=None, retry_count=0)
Requeues the current job. Doesn't interrupt it
4.988682
5.033731
0.991051
if self.data is None: return context.log.debug("Starting %s(%s)" % (self.data["path"], self.data["params"])) task_class = load_class_by_path(self.data["path"]) self.task = task_class() self.task.is_main_task = True if not self.task.max_concurrenc...
def perform(self)
Loads and starts the main task for this job, the saves the result.
4.75246
4.532958
1.048424
end_time = None if timeout: end_time = time.time() + timeout while end_time is None or time.time() < end_time: job_data = self.collection.find_one({ "_id": ObjectId(self.id), "status": {"$nin": ["started", "queued"]} ...
def wait(self, poll_interval=1, timeout=None, full_data=False)
Wait for this job to finish.
2.846248
2.731022
1.042191
current_greenletid = id(gevent.getcurrent()) trace = "Job killed: %s" % reason for greenlet, job in context._GLOBAL_CONTEXT["greenlets"].values(): greenletid = id(greenlet) if job and job.id == self.id and greenletid != current_greenletid: green...
def kill(self, block=False, reason="unknown")
Forcefully kill all greenlets associated with this job
5.074883
4.565139
1.11166
failure_date = datetime.datetime.utcnow() new_history = { "date": failure_date, "status": status, "exceptiontype": job_exc.__name__ } traces = trace.split("---- Original exception: -----") if len(traces) > 1: new_history[...
def _save_traceback_history(self, status, trace, job_exc)
Create traceback history or add a new traceback to history.
3.804832
3.680728
1.033717
urllib.parse.clear_cache() re.purge() linecache.clearcache() copyreg.clear_extension_cache() if hasattr(fnmatch, "purge"): fnmatch.purge() # pylint: disable=no-member elif hasattr(fnmatch, "_purge"): fnmatch._purge() # pylint: disable=...
def trace_memory_clean_caches(self)
Avoid polluting results with some builtin python caches
4.324944
4.075519
1.061201
self.trace_memory_clean_caches() objgraph.show_growth(limit=30) gc.collect() self._memory_start = self.worker.get_memory()["total"]
def trace_memory_start(self)
Starts measuring memory consumption
10.825061
9.842453
1.099834
self.trace_memory_clean_caches() objgraph.show_growth(limit=30) trace_type = context.get_current_config()["trace_memory_type"] if trace_type: filename = '%s/%s-%s.png' % ( context.get_current_config()["trace_memory_output_dir"], tr...
def trace_memory_stop(self)
Stops measuring memory consumption
5.015399
4.990148
1.00506
all_queues_from_mongodb = Queue.all_known(sources=("jobs", )) idprefix = self.id if not idprefix.endswith("/"): idprefix += "/" return {q for q in all_queues_from_mongodb if q.startswith(idprefix)}
def get_known_subqueues(self)
Returns all known subqueues
10.160137
9.6026
1.058061
if self.id.endswith("/"): subqueues = self.get_known_subqueues() if len(subqueues) == 0: return 0 else: with context.connections.redis.pipeline(transaction=False) as pipe: for subqueue in subqueues: ...
def size(self)
Returns the total number of queued jobs on the queue
3.631152
3.250754
1.117018
return [str(x["_id"]) for x in self.collection.find( {"status": "queued"}, sort=[("_id", -1 if self.is_reverse else 1)], projection={"_id": 1}) ]
def list_job_ids(self, skip=0, limit=20)
Returns a list of job ids on a queue
4.241097
4.134817
1.025704
if job_class is None: from .job import Job job_class = Job count = 0 # TODO: remove _id sort after full migration to datequeued sort_order = [("datequeued", -1 if self.is_reverse else 1), ("_id", -1 if self.is_reverse else 1)] # MongoDB opti...
def dequeue_jobs(self, max_jobs=1, job_class=None, worker=None)
Fetch a maximum of max_jobs from this queue
3.652496
3.630853
1.005961
return Response( json.dumps( dict( *args, **kwargs), cls=MongoJSONEncoder), mimetype='application/json')
def jsonify(*args, **kwargs)
jsonify with support for MongoDB ObjectId
4.032166
3.343535
1.205959
cfg = get_current_config() return username == cfg["dashboard_httpauth"].split( ":")[0] and pwd == cfg["dashboard_httpauth"].split(":")[1]
def check_auth(username, pwd)
This function is called to check if a username / password combination is valid.
6.791862
6.146551
1.104987
while True: try: self.manage() except Exception as e: # pylint: disable=broad-except log.error("When reporting: %s" % e) finally: time.sleep(self.config["report_interval"])
def greenlet_manage(self)
This greenlet always runs in background to update current status in MongoDB every N seconds.
4.624247
4.148387
1.114709
start_time = time.time() log.debug("Starting queue stats...") # Fetch all known queues queues = [Queue(q) for q in Queue.all_known()] new_queues = {queue.id for queue in queues} old_queues = set(self.queue_etas.keys()) for deleted_queue in old_queues....
def queuestats(self)
Compute ETAs for every known queue & subqueue
3.86062
3.606116
1.070576
log.debug("Starting orchestration run for worker group %s" % group["_id"]) agents = self.fetch_worker_group_agents(group) # Evaluate what workers are currently, rightfully there. They won't be touched. for agent in agents: desired_workers = self.get_desired_worker...
def do_orchestrate(self, group)
Manage the desired workers of *all* the agents in the given group
5.390863
5.082239
1.060726
from .context import get_current_job, set_current_job, log if not pool_size: return [func(*args) for args in iterable] counter = itertools_count() current_job = get_current_job() def inner_func(*args): next(counter) if current_job: set_current_j...
def subpool_map(pool_size, func, iterable)
Starts a Gevent pool and run a map. Takes care of setting current_job and cleaning up.
2.83692
2.657787
1.067399
from .context import get_current_job, set_current_job, log if not pool_size: for args in iterable: yield func(*args) counter = itertools_count() current_job = get_current_job() def inner_func(*args): next(counter) if current_job: set_current_job(current_job) try: ...
def subpool_imap(pool_size, func, iterable, flatten=False, unordered=False, buffer_size=None)
Generator version of subpool_map. Should be used with unordered=True for optimal performance
2.674273
2.64537
1.010926
params = task.get("params") if params: params = json.dumps(sorted(list(task["params"].items()), key=lambda x: x[0])) # pylint: disable=no-member full = [str(task.get(x)) for x in ["path", "interval", "dailytime", "weekday", "monthday", "queue"]] full.extend([str(params)]) return " "...
def _hash_task(task)
Returns a unique hash for identify a task and its params
5.300741
4.611589
1.149439
tasks_by_hash = {_hash_task(t): t for t in self.config_tasks} if len(tasks_by_hash) != len(self.config_tasks): raise Exception("Fatal error: there was a hash duplicate in the scheduled tasks config.") for h, task in tasks_by_hash.items(): if task.get("monthday"...
def check_config_integrity(self)
Make sure the scheduler config is valid
3.084219
2.928855
1.053046
tasks_by_hash = {_hash_task(t): t for t in self.config_tasks} for task in self.all_tasks: if tasks_by_hash.get(task["hash"]): del tasks_by_hash[task["hash"]] else: self.collection.remove({"_id": task["_id"]}) log.debug("S...
def sync_config_tasks(self)
Performs the first sync of a list of tasks, often defined in the config file.
3.765112
3.78726
0.994152
if redis is None: redis = connections.redis # http://redis.io/commands/INCR now = int(time.time()) k = "ratelimit:%s:%s" % (key, now // per) with redis.pipeline(transaction=True) as pipeline: pipeline.incr(k, 1) pipeline.expire(k, per + 10) value = pipeline.e...
def ratelimit(key, limit, per=1, redis=None)
Returns an integer with the number of available actions for the current period in seconds. If zero, rate was already reached.
3.016551
3.017033
0.99984
# Nothing to change! if not config["print_mongodb"] and not config["trace_io"]: return from termcolor import cprint # Print because we are very early and log() may not be ready yet. cprint("Monkey-patching MongoDB methods...", "white") def gen_monkey_patch(base_object, method): ...
def patch_pymongo(config)
Monkey-patch pymongo's collections to add some logging
2.837882
2.796557
1.014777
# Accept float(0.1), "0.1", "0.1-0.2" def sleep(): if isinstance(seconds, float): time.sleep(seconds) elif isinstance(seconds, basestring): # pylint: disable=maybe-no-member if "-" in seconds: time.sleep(random.uniform( ...
def patch_network_latency(seconds=0.01)
Add random latency to all I/O operations
2.529671
2.554373
0.99033
self.report_worker(w=1) while True: try: self.report_worker() except Exception as e: # pylint: disable=broad-except self.log.error("When reporting: %s" % e) finally: time.sleep(self.config["report_interval"])
def greenlet_report(self)
This greenlet always runs in background to update current status in MongoDB every N seconds. Caution: it might get delayed when doing long blocking operations. Should we do this in a thread instead?
4.517224
4.012567
1.125769
while True: try: self.flush_logs() except Exception as e: # pylint: disable=broad-except self.log.error("When flushing logs: %s" % e) finally: time.sleep(self.config["report_interval"])
def greenlet_logs(self)
This greenlet always runs in background to update current logs in MongoDB every 10 seconds. Caution: it might get delayed when doing long blocking operations. Should we do this in a thread instead?
4.049562
3.586531
1.129103
try: queues = [] prefixes = [q for q in self.config["queues"] if q.endswith("/")] known_subqueues = Queue.all_known(prefixes=prefixes) for q in self.config["queues"]: queues.append(Queue(q)) if q.endswith("/"): ...
def refresh_queues(self, fatal=False)
Updates the list of currently known queues and subqueues
3.346199
3.113523
1.074731
greenlets = [] for greenlet in list(self.gevent_pool): g = {} short_stack = [] stack = traceback.format_stack(greenlet.gr_frame) for s in stack[1:]: if "/gevent/hub.py" in s: break short_stack.a...
def get_worker_report(self, with_memory=False)
Returns a dict containing all the data we can about the current status of the worker and its jobs.
3.585325
3.539154
1.013046
while True: now = datetime.datetime.utcnow() for greenlet in list(self.gevent_pool): job = get_current_job(id(greenlet)) if job and job.timeout and job.datestarted: expires = job.datestarted + datetime.timedelta(seconds=job.ti...
def greenlet_timeouts(self)
This greenlet kills jobs in other greenlets if they timeout.
4.682969
4.024083
1.163736
if self.config["processes"] > 1: self.log.debug( "Admin server disabled because of multiple processes.") return class Devnull(object): def write(self, *_): pass from gevent import pywsgi def admin_routes(env...
def greenlet_admin(self)
This greenlet is used to get status information about the worker when --admin_port was given
3.017244
2.937162
1.027265
# Be mindful that this is being executed in a different greenlet than the work_* methods. while True: time.sleep(0.01) with self.work_lock: if self.status != "wait": continue if len(self.gevent_pool) > 0: ...
def wait_for_idle(self)
Waits until the worker has nothing more to do. Very useful in tests
9.507502
8.884959
1.070067
self.work_init() self.work_loop(max_jobs=self.max_jobs, max_time=self.max_time) self.work_stop()
def work(self)
Starts the work loop.
4.956993
4.119397
1.20333
dequeued_jobs = 0 available_queues = [ queue for queue in self.queues if queue.root_id not in self.paused_queues and queue.id not in self.paused_queues ] for queue_i in range(len(available_queues)): queue = available_queues[(qu...
def work_once(self, free_pool_slots=1, max_jobs=None)
Does one lookup for new jobs, inside the inner work loop
3.3976
3.410588
0.996192
if len(self.queues_with_notify) > 0: # https://github.com/antirez/redis/issues/874 connections.redis.blpop(*(self.queues_with_notify + [max(1, int(self.config["max_latency"]))])) else: gevent.sleep(self.config["max_latency"])
def work_wait(self)
Wait for new jobs to arrive
6.670034
6.439069
1.035869
if self.config["trace_memory"]: job.trace_memory_start() set_current_job(job) try: job.perform() except MaxConcurrencyInterrupt: self.log.error("Max concurrency reached") job._save_status("maxconcurrency", exception=True) ...
def perform_job(self, job)
Wraps a job.perform() call with timeout logic and exception handlers. This is the first call happening inside the greenlet.
2.570103
2.520159
1.019817
self.log.info("Forced shutdown...") self.status = "killing" self.gevent_pool.kill(exception=JobInterrupt, block=False) raise StopRequested()
def shutdown_now(self)
Forced shutdown: interrupts all the jobs.
12.666078
9.097375
1.392278
if len(job_ids) == 0 or self.use_large_ids: return job_ids elif isinstance(job_ids[0], ObjectId): return [x.binary for x in job_ids] else: return [bytes.fromhex(str(x)) for x in job_ids]
def serialize_job_ids(self, job_ids)
Returns job_ids serialized for storage in Redis
3.062304
2.843415
1.076981
if len(job_ids) == 0 or self.use_large_ids: return job_ids else: return [binascii.hexlify(x.encode('utf-8') if (PY3 and isinstance(x, str)) else x).decode('ascii') for x in job_ids]
def unserialize_job_ids(self, job_ids)
Unserialize job_ids stored in Redis
3.595101
3.559162
1.010098
queue = self.id if self.id.endswith("/"): queue = self.root_id return queue
def _get_pausable_id(self)
Get the queue id (either id or root_id) that should be used to pause/unpause the current queue TODO: handle subqueues with more than one level, e.g. "queue/subqueue/"
14.788465
6.488349
2.279234
root_is_paused = False if self.root_id != self.id: root_is_paused = context.connections.redis.sismember(redis_key("paused_queues"), self.root_id) return root_is_paused or context.connections.redis.sismember(redis_key("paused_queues"), self.id)
def is_paused(self)
Returns wether the queue is paused or not. Warning: this does NOT ensure that the queue was effectively added to the set of paused queues. See the 'paused_queues_refresh_interval' option.
4.15696
3.434378
1.210397
prefix = context.get_current_config()["redis_prefix"] queues = [] for key in context.connections.redis.keys(): if key.startswith(prefix): queues.append(Queue(key[len(prefix) + 3:])) return queues
def all_active(cls)
List active queues, based on their lengths in Redis. Warning, uses the unscalable KEYS redis command
7.156478
5.61237
1.275126