def get_task_signature(cls, package, serialized_package): tenant = package.tenant serialized_tenant = core_utils.serialize_instance(tenant) service_settings = package.service_settings serialized_service_settings = core_utils.serialize_instance(service_settings) create_tenant = openstack_executors.TenantCreateExecutor.get_task_signature(tenant, serialized_tenant) set_tenant_ok = openstack_executors.TenantCreateExecutor.get_success_signature(tenant, serialized_tenant) populate_service_settings = tasks.OpenStackPackageSettingsPopulationTask().si(serialized_package) create_service_settings = structure_executors.ServiceSettingsCreateExecutor.get_task_signature( service_settings, serialized_service_settings) return create_tenant | set_tenant_ok | populate_service_settings | create_service_settings
def send_issue_updated_notification(sender, instance, created=False, **kwargs): if created or set(instance.tracker.changed()) == { models.Issue.assignee.field.attname, 'modified' }: return serialized_issue = core_utils.serialize_instance(instance) transaction.on_commit( lambda: tasks.send_issue_updated_notification.delay(serialized_issue))
def send_comment_added_notification(sender, instance, created=False, **kwargs): comment = instance if not created or not comment.is_public: return serialized_comment = core_utils.serialize_instance(comment) transaction.on_commit(lambda: tasks.send_comment_added_notification.delay( serialized_comment))
def test_task_does_not_set_coordinates_if_response_is_not_ok( self, mock_request_get): ip_address = "127.0.0.1" instance = factories.TestNewInstanceFactory() mock_request_get.return_value.ok = False tasks.detect_vm_coordinates(utils.serialize_instance(instance)) instance.refresh_from_db() self.assertIsNone(instance.latitude) self.assertIsNone(instance.longitude)
def detect_vm_coordinates(sender, instance, name, source, target, **kwargs): # Check if geolocation is enabled if not settings.NODECONDUCTOR.get('ENABLE_GEOIP', True): return # VM already has coordinates if instance.latitude is not None and instance.longitude is not None: return if target == StateMixin.States.OK: send_task('structure', 'detect_vm_coordinates')(utils.serialize_instance(instance))
def test_if_limit_is_reached_provisioning_is_delayed(self, params): link = factories.TestServiceProjectLinkFactory() factories.TestNewInstanceFactory.create_batch( size=params['size'], state=models.TestNewInstance.States.CREATING, service_project_link=link) vm = factories.TestNewInstanceFactory( state=models.TestNewInstance.States.CREATION_SCHEDULED, service_project_link=link) serialized_vm = utils.serialize_instance(vm) mocked_retry = Mock() tasks.ThrottleProvisionTask.retry = mocked_retry tasks.ThrottleProvisionTask().si( serialized_vm, 'create', state_transition='begin_starting').apply() self.assertEqual(mocked_retry.called, params['retried'])
def test_task_sets_coordinates(self, mock_request_get): ip_address = "127.0.0.1" expected_latitude = 20 expected_longitude = 20 instance = factories.TestNewInstanceFactory() mock_request_get.return_value.ok = True response = { "ip": ip_address, "latitude": expected_latitude, "longitude": expected_longitude } mock_request_get.return_value.json.return_value = response tasks.detect_vm_coordinates(utils.serialize_instance(instance)) instance.refresh_from_db() self.assertEqual(instance.latitude, expected_latitude) self.assertEqual(instance.longitude, expected_longitude)
def run(self, serialized_instance, *args, **kwargs): """ Deserialize input data and start backend operation execution """ try: instance = utils.deserialize_instance(serialized_instance) except ObjectDoesNotExist: message = ( 'Cannot restore instance from serialized object %s. Probably it was deleted.' % serialized_instance) six.reraise(ObjectDoesNotExist, message) self.args = args self.kwargs = kwargs self.pre_execute(instance) result = self.execute(instance, *self.args, **self.kwargs) self.post_execute(instance) if result and isinstance(result, django_models.Model): result = utils.serialize_instance(result) return result
def get_success_signature(cls, package, serialized_package, **kwargs): """ Get Celery signature of task that should be applied on successful execution. """ service_settings = package.service_settings serialized_service_settings = core_utils.serialize_instance(service_settings) return core_tasks.StateTransitionTask().si(serialized_service_settings, state_transition='set_ok')
def _get_tag_cache_key(self): return 'tags:%s' % core_utils.serialize_instance(self)
def run(self): for instance in self.get_pulled_objects(): serialized = core_utils.serialize_instance(instance) self.pull_task().delay(serialized)
pass @classmethod def post_apply(cls, instance, **kwargs): """ Perform synchronous actions after signature apply """ pass @classmethod def apply_signature(cls, instance, async=True, countdown=None, is_heavy_task=False, **kwargs): """ Serialize input data and apply signature """ serialized_instance = utils.serialize_instance(instance) signature = cls.get_task_signature(instance, serialized_instance, **kwargs) link = cls.get_success_signature(instance, serialized_instance, **kwargs) link_error = cls.get_failure_signature(instance, serialized_instance, **kwargs) if async: return signature.apply_async(link=link, link_error=link_error, countdown=countdown, queue=is_heavy_task and 'heavy' or None) else:
def pull_sla(): """ Copy OpenStack instance SLA and events as CRM events """ for crm in CRM.objects.filter(state=CRM.States.ONLINE): BackendMethodTask().delay(core_utils.serialize_instance(crm), 'pull_crm_sla')