def push_translations(self): # If gengosystem is disabled, we just return immediately. We # can backfill later. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return if self.gengo_watch_balance: balance = gengo_api.get_balance() threshold = settings.GENGO_ACCOUNT_BALANCE_THRESHOLD # statsd the balance so we can track it with graphite. statsd.gauge('translation.gengo.balance', balance) if not self.balance_good_to_continue(balance, threshold): # If we don't have enough balance, stop. return # Create language buckets for the jobs for this translator. # We bucket by language because this makes it easier for a # single Gengo translator to translate all the jobs in an # order. jobs = GengoJob.objects.filter( tier=self.gengo_tier, status=STATUS_CREATED) lang_buckets = {} for job in jobs: lang_buckets.setdefault(job.src_lang, []).append(job) # For each bucket, assemble an order and post it. for lang, jobs in lang_buckets.items(): batch = [] for job in jobs: batch.append({ 'id': job.id, 'lc_src': job.src_lang, 'lc_dst': job.dst_lang, 'tier': self.gengo_tier, 'text': getattr(job.content_object, job.src_field), 'unique_id': job.unique_id }) try: resp = gengo_api.translate_bulk(batch) except GengoError as exc: self.log_error( instance=None, action='push-translations', msg=unicode(exc), metadata={ 'batch': batch }) continue # We should have an `order_id` at this point, so we create a # GengoOrder with it. order = GengoOrder(order_id=resp['order_id']) order.save() order.log('created', metadata={'response': resp}) # Update all the jobs in the order. for job in jobs: job.assign_to_order(order) if self.gengo_watch_balance: # Update the balance and see if we're below the threshold. balance = balance - float(resp['credits_used']) if not self.balance_good_to_continue(balance, threshold): # If we don't have enough balance, stop. return
def push_translations(self): # If gengosystem is disabled, we just return immediately. We # can backfill later. if not waffle.switch_is_active('gengosystem'): return gengo_api = FjordGengo() if not gengo_api.is_configured(): # If Gengo isn't configured, then we drop out here rather # than raise a GengoConfig error. return if self.gengo_watch_balance: balance = gengo_api.get_balance() threshold = settings.GENGO_ACCOUNT_BALANCE_THRESHOLD # statsd the balance so we can track it with graphite. statsd.gauge('translation.gengo.balance', balance) if not self.balance_good_to_continue(balance, threshold): # If we don't have enough balance, stop. return # Create language buckets for the jobs for this translator. # We bucket by language because this makes it easier for a # single Gengo translator to translate all the jobs in an # order. jobs = GengoJob.objects.filter(tier=self.gengo_tier, status=STATUS_CREATED) lang_buckets = {} for job in jobs: lang_buckets.setdefault(job.src_lang, []).append(job) # For each bucket, assemble an order and post it. for lang, jobs in lang_buckets.items(): batch = [] for job in jobs: batch.append({ 'id': job.id, 'lc_src': job.src_lang, 'lc_dst': job.dst_lang, 'tier': self.gengo_tier, 'text': getattr(job.content_object, job.src_field), 'unique_id': job.unique_id }) try: resp = gengo_api.translate_bulk(batch) except GengoError as exc: self.log_error(instance=None, action='push-translations', msg=unicode(exc), metadata={'batch': batch}) continue # We should have an `order_id` at this point, so we create a # GengoOrder with it. order = GengoOrder(order_id=resp['order_id']) order.save() order.log('created', metadata={'response': resp}) # Update all the jobs in the order. for job in jobs: job.assign_to_order(order) if self.gengo_watch_balance: # Update the balance and see if we're below the threshold. balance = balance - float(resp['credits_used']) if not self.balance_good_to_continue(balance, threshold): # If we don't have enough balance, stop. return