def nimbus_push_experiment_to_kinto(collection, experiment_id): """ An invoked task that given a single experiment id, query it in the db, serialize it, and push its data to the configured collection. If it fails for any reason, log the error and reraise it so it will be forwarded to sentry. """ metrics.incr("push_experiment_to_kinto.started") try: experiment = NimbusExperiment.objects.get(id=experiment_id) logger.info(f"Pushing {experiment.slug} to Kinto") kinto_client = KintoClient(collection) data = NimbusExperimentSerializer(experiment).data kinto_client.create_record(data) experiment.publish_status = NimbusExperiment.PublishStatus.WAITING experiment.save() generate_nimbus_changelog( experiment, get_kinto_user(), message=NimbusChangeLog.Messages.PUSHED_TO_KINTO, ) logger.info(f"{experiment.slug} pushed to Kinto") metrics.incr("push_experiment_to_kinto.completed") except Exception as e: metrics.incr("push_experiment_to_kinto.failed") logger.info( f"Pushing experiment {experiment.slug} to Kinto failed: {e}") raise e
def test_create_record_creates_record_patches_collection( self, review, status): client = KintoClient(self.collection, review=review) client.create_record({"test": "data"}) self.mock_kinto_client_creator.assert_called_with( server_url=settings.KINTO_HOST, auth=(settings.KINTO_USER, settings.KINTO_PASS), ) self.mock_kinto_client.create_record.assert_called_with( data={"test": "data"}, collection=self.collection, bucket=settings.KINTO_BUCKET_WORKSPACE, if_not_exists=True, ) self.mock_kinto_client.patch_collection.assert_called_with( id=self.collection, data={"status": status}, bucket=settings.KINTO_BUCKET_WORKSPACE, )
def nimbus_synchronize_preview_experiments_in_kinto(): """ A scheduled task that pushes any experiments with status PREVIEW to the preview collection and removes any experiments not with status PREVIEW from the preview collection. """ metrics.incr("nimbus_synchronize_preview_experiments_in_kinto.started") kinto_client = KintoClient(settings.KINTO_COLLECTION_NIMBUS_PREVIEW, review=False) try: published_preview_slugs = kinto_client.get_main_records().keys() should_publish_experiments = NimbusExperiment.objects.filter( status=NimbusExperiment.Status.PREVIEW).exclude( slug__in=published_preview_slugs) for experiment in should_publish_experiments: data = NimbusExperimentSerializer(experiment).data kinto_client.create_record(data) logger.info(f"{experiment.slug} is being pushed to preview") should_unpublish_experiments = NimbusExperiment.objects.filter( slug__in=published_preview_slugs).exclude( status=NimbusExperiment.Status.PREVIEW) for experiment in should_unpublish_experiments: kinto_client.delete_record(experiment.slug) logger.info(f"{experiment.slug} is being removed from preview") metrics.incr( "nimbus_synchronize_preview_experiments_in_kinto.completed") except Exception as e: metrics.incr("nimbus_synchronize_preview_experiments_in_kinto.failed") logger.info(f"Synchronizing preview experiments failed: {e}") raise e