def refresh_org_reporters(org_id=None): start = time.time() r = get_redis_connection() key = 'refresh_reporters' lock_timeout = 900 if org_id: key = 'refresh_reporters:%d' % org_id lock_timeout = 30 if not r.get(key): with r.lock(key, timeout=lock_timeout): active_orgs = Org.objects.filter(is_active=True) if org_id: active_orgs = Org.objects.filter(pk=org_id) for org in active_orgs: fetch_reporter_group(org) fetch_old_sites_count() print "Task: refresh_org_reporters took %ss" % (time.time() - start)
def test_reporter_group(self): self.clear_cache() with patch("ureport.utils.datetime_to_ms") as mock_datetime_ms: mock_datetime_ms.return_value = 500 with patch('dash.orgs.models.TembaClient.get_groups') as mock: group_dict = dict(uuid="group-uuid", name="reporters", size=25) mock.return_value = Group.deserialize_list([group_dict]) with patch('django.core.cache.cache.set') as cache_set_mock: cache_set_mock.return_value = "Set" with patch('django.core.cache.cache.delete') as cache_delete_mock: cache_delete_mock.return_value = "Deleted" fetch_reporter_group(self.org) self.assertFalse(mock.called) self.assertFalse(cache_set_mock.called) self.assertEqual(self.org.get_reporter_group(), dict()) self.org.set_config("reporter_group", "reporters") fetch_reporter_group(self.org) mock.assert_called_with(name='reporters') key = CACHE_ORG_REPORTER_GROUP_KEY % (self.org.pk, "reporters") cache_set_mock.assert_called_with(key, {'time': 500, 'results': group_dict}, UREPORT_ASYNC_FETCHED_DATA_CACHE_TIME) cache_delete_mock.assert_called_with(GLOBAL_COUNT_CACHE_KEY) with patch('django.core.cache.cache.get') as cache_get_mock: cache_get_mock.return_value = {'time': 500, 'results': group_dict} self.assertEqual(get_reporter_group(self.org), group_dict)