| OLD | NEW |
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 # Copyright 2014 The Swarming Authors. All rights reserved. | 2 # Copyright 2014 The Swarming Authors. All rights reserved. |
| 3 # Use of this source code is governed by the Apache v2.0 license that can be | 3 # Use of this source code is governed by the Apache v2.0 license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Tasks definition. | 6 """Tasks definition. |
| 7 | 7 |
| 8 Each user request creates a new TaskRequest. The TaskRequest instance saves the | 8 Each user request creates a new TaskRequest. The TaskRequest instance saves the |
| 9 metadata of the request, e.g. who requested it, when why, etc. It links to the | 9 metadata of the request, e.g. who requested it, when why, etc. It links to the |
| 10 actual data of the request in a TaskProperties. The TaskProperties represents | 10 actual data of the request in a TaskProperties. The TaskProperties represents |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 """Adds automatic tags.""" | 426 """Adds automatic tags.""" |
| 427 super(TaskRequest, self)._pre_put_hook() | 427 super(TaskRequest, self)._pre_put_hook() |
| 428 self.properties._pre_put_hook() | 428 self.properties._pre_put_hook() |
| 429 if self.properties.is_terminate: | 429 if self.properties.is_terminate: |
| 430 if not self.priority == 0: | 430 if not self.priority == 0: |
| 431 raise datastore_errors.BadValueError( | 431 raise datastore_errors.BadValueError( |
| 432 'terminate request must be priority 0') | 432 'terminate request must be priority 0') |
| 433 elif self.priority == 0: | 433 elif self.priority == 0: |
| 434 raise datastore_errors.BadValueError( | 434 raise datastore_errors.BadValueError( |
| 435 'priority 0 can only be used for terminate request') | 435 'priority 0 can only be used for terminate request') |
| 436 self.tags.append('priority:%s' % self.priority) | 436 |
| 437 self.tags.append('user:%s' % self.user) | 437 if not self.properties.dimensions: |
| 438 for key, value in self.properties.dimensions.iteritems(): | 438 raise datastore_errors.BadValueError('dimensions must be specified') |
| 439 self.tags.append('%s:%s' % (key, value)) | 439 dim_keys = self.properties.dimensions.keys() |
| 440 self.tags = sorted(set(self.tags)) | 440 if 'pool' not in dim_keys and 'id' not in dim_keys: |
| 441 raise datastore_errors.BadValueError( |
| 442 'At least one of \'id\' or \'pool\' must be used as dimensions') |
| 443 |
| 441 if (self.pubsub_topic and | 444 if (self.pubsub_topic and |
| 442 not pubsub.validate_full_name(self.pubsub_topic, 'topics')): | 445 not pubsub.validate_full_name(self.pubsub_topic, 'topics')): |
| 443 raise datastore_errors.BadValueError( | 446 raise datastore_errors.BadValueError( |
| 444 'bad pubsub topic name - %s' % self.pubsub_topic) | 447 'bad pubsub topic name - %s' % self.pubsub_topic) |
| 445 if self.pubsub_auth_token and not self.pubsub_topic: | 448 if self.pubsub_auth_token and not self.pubsub_topic: |
| 446 raise datastore_errors.BadValueError( | 449 raise datastore_errors.BadValueError( |
| 447 'pubsub_auth_token requires pubsub_topic') | 450 'pubsub_auth_token requires pubsub_topic') |
| 448 if self.pubsub_userdata and not self.pubsub_topic: | 451 if self.pubsub_userdata and not self.pubsub_topic: |
| 449 raise datastore_errors.BadValueError( | 452 raise datastore_errors.BadValueError( |
| 450 'pubsub_userdata requires pubsub_topic') | 453 'pubsub_userdata requires pubsub_topic') |
| 451 | 454 |
| 455 self.tags.append('priority:%s' % self.priority) |
| 456 self.tags.append('user:%s' % self.user) |
| 457 for key, value in self.properties.dimensions.iteritems(): |
| 458 self.tags.append('%s:%s' % (key, value)) |
| 459 self.tags = sorted(set(self.tags)) |
| 460 |
| 452 | 461 |
| 453 def _new_request_key(): | 462 def _new_request_key(): |
| 454 """Returns a valid ndb.Key for this entity. | 463 """Returns a valid ndb.Key for this entity. |
| 455 | 464 |
| 456 Task id is a 64 bits integer represented as a string to the user: | 465 Task id is a 64 bits integer represented as a string to the user: |
| 457 - 1 highest order bits set to 0 to keep value positive. | 466 - 1 highest order bits set to 0 to keep value positive. |
| 458 - 43 bits is time since _BEGINING_OF_THE_WORLD at 1ms resolution. | 467 - 43 bits is time since _BEGINING_OF_THE_WORLD at 1ms resolution. |
| 459 It is good for 2**43 / 365.3 / 24 / 60 / 60 / 1000 = 278 years or 2010+278 = | 468 It is good for 2**43 / 365.3 / 24 / 60 / 60 / 1000 = 278 years or 2010+278 = |
| 460 2288. The author will be dead at that time. | 469 2288. The author will be dead at that time. |
| 461 - 16 bits set to a random value or a server instance specific value. Assuming | 470 - 16 bits set to a random value or a server instance specific value. Assuming |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 _put_request(request) | 663 _put_request(request) |
| 655 return request | 664 return request |
| 656 | 665 |
| 657 | 666 |
| 658 def validate_priority(priority): | 667 def validate_priority(priority): |
| 659 """Throws ValueError if priority is not a valid value.""" | 668 """Throws ValueError if priority is not a valid value.""" |
| 660 if 0 > priority or MAXIMUM_PRIORITY < priority: | 669 if 0 > priority or MAXIMUM_PRIORITY < priority: |
| 661 raise datastore_errors.BadValueError( | 670 raise datastore_errors.BadValueError( |
| 662 'priority (%d) must be between 0 and %d (inclusive)' % | 671 'priority (%d) must be between 0 and %d (inclusive)' % |
| 663 (priority, MAXIMUM_PRIORITY)) | 672 (priority, MAXIMUM_PRIORITY)) |
| OLD | NEW |