def sync_podcasts(self, request, queryset): for podcast in queryset: chain = Chain() chain.append(podcast_update, podcast.pk) chain.append(podcast_download, podcast.pk) chain.run() self.message_user(request, "Syncing podcast")
def podcast_sync_all(request): for podcast in Podcast.objects.all(): chain = Chain() chain.append(podcast_update, podcast.pk) chain.append(podcast_download, podcast.pk) chain.run() return HttpResponseRedirect(reverse('podcasts:index'))
def podcast_sync(request, slug): podcast = get_object_or_404(Podcast, slug=slug) chain = Chain() chain.append(podcast_update, podcast.pk) chain.append(podcast_download, podcast.pk) chain.run() return HttpResponseRedirect( reverse('podcasts:podcast-detail', args=(slug, )))
def schedule_cheques_creation(workspace_id: int, expense_group_ids: List[str]): """ Schedule cheque creation :param expense_group_ids: List of expense group ids :param workspace_id: workspace id :return: None """ if expense_group_ids: expense_groups = ExpenseGroup.objects.filter( Q(tasklog__id__isnull=True) | ~Q(tasklog__status__in=['IN_PROGRESS', 'COMPLETE']), workspace_id=workspace_id, id__in=expense_group_ids, cheque__id__isnull=True, exported_at__isnull=True).all() chain = Chain() for expense_group in expense_groups: task_log, _ = TaskLog.objects.get_or_create( workspace_id=expense_group.workspace_id, expense_group=expense_group, defaults={ 'status': 'ENQUEUED', 'type': 'CREATING_CHECK' }) if task_log.status not in ['IN_PROGRESS', 'ENQUEUED']: task_log.status = 'ENQUEUED' task_log.save() chain.append('apps.quickbooks_online.tasks.create_cheque', expense_group, task_log.id) if chain.length(): chain.run()
def testredisview(request): json_payload = {"message": "hello world!"} # async_task('async_test.func.sleep_and_print', 10) # for i in range(4): # async_task(sleep_and_print, i, group='xxx', hook='djangoq.hooks.print_result') chain = Chain(group='chain') chain.append(sleep_and_print, 5) chain.append(sleep_and_print, 4) chain.append(sleep_and_print, 3) chain.run() return JsonResponse(json_payload)
def post(self, request, *args, **kwargs): """ Trigger Auto Map employees """ try: workspace_id = kwargs['workspace_id'] general_settings = WorkspaceGeneralSettings.objects.get( workspace_id=workspace_id) chain = Chain() if not general_settings.auto_map_employees: return Response(data={ 'message': 'Employee mapping preference not found for this workspace' }, status=status.HTTP_400_BAD_REQUEST) chain.append('apps.mappings.tasks.async_auto_map_employees', workspace_id) chain.run() return Response(data={}, status=status.HTTP_200_OK) except GeneralMapping.DoesNotExist: return Response( { 'message': 'General mappings do not exist for this workspace' }, status=status.HTTP_400_BAD_REQUEST)
def handle(self, *args, **options): podcast_id = options['podcast_id'] download = options['download'] if podcast_id: podcast_set = Podcast.objects.filter(pk__in=podcast_id) else: podcast_set = Podcast.objects.all() for podcast in podcast_set: try: chain = Chain() chain.append(podcast_update, podcast.pk) if download: chain.append(podcast_download, podcast.pk) chain.run() # except Podcast.DoesNotExist: # raise CommandError(f"Podcast with id {podcast_id} does not exist") except ValueError as err: raise CommandError(err)
def post(self, request, *args, **kwargs): """ Trigger Auto Map Employees """ try: workspace_id = kwargs['workspace_id'] configuration = Configuration.objects.get( workspace_id=workspace_id) chain = Chain() if not configuration.auto_map_employees: return Response(data={ 'message': 'Employee mapping preference not found for this workspace' }, status=status.HTTP_400_BAD_REQUEST) chain.append('apps.mappings.tasks.async_auto_map_employees', workspace_id) general_mappings = GeneralMapping.objects.get( workspace_id=workspace_id) if general_mappings.default_charge_card_name: chain.append( 'apps.mappings.tasks.async_auto_map_charge_card_account', workspace_id) chain.run() return Response(data={}, status=status.HTTP_200_OK) except GeneralMapping.DoesNotExist: return Response( { 'message': 'General mappings do not exist for this workspace' }, status=status.HTTP_400_BAD_REQUEST)
def update_cache(self, sender, instance, *args, **kwags): from django_q.tasks import Chain name = '.'.join([instance._meta.app_label, instance._meta.object_name]) if name not in self.chains: self.chains[name] = Chain() start = getattr(instance, instance.time_fields()[0]) end = getattr(instance, instance.time_fields()[1]) if not isinstance(start, str): start = start.isoformat() if not isinstance(end, str): end = end.isoformat() start = arrow.get(start).replace(days=-1)\ .format('YYYY-MM-DDTHH:mm:00') + 'Z' end = arrow.get(end).replace(days=1)\ .format('YYYY-MM-DDTHH:mm:00') + 'Z' self.chains[name].append('time_series.apps.process_cache', instance._meta.app_label, instance._meta.object_name, instance.location.id, start, end)
def schedule_credit_card_charge_creation(workspace_id: int, expense_group_ids: List[str]): """ Schedule Credit Card Charge creation :param expense_group_ids: List of expense group ids :param workspace_id: workspace id :return: None """ if expense_group_ids: expense_groups = ExpenseGroup.objects.filter( Q(tasklog__id__isnull=True) | ~Q(tasklog__status__in=['IN_PROGRESS', 'COMPLETE']), workspace_id=workspace_id, id__in=expense_group_ids, creditcardcharge__id__isnull=True, exported_at__isnull=True).all() chain = Chain(cached=False) for expense_group in expense_groups: task_log, _ = TaskLog.objects.get_or_create( workspace_id=expense_group.workspace_id, expense_group=expense_group, defaults={ 'status': 'ENQUEUED', 'type': 'CREATING_CREDIT_CARD_CHARGE' }) if task_log.status not in ['IN_PROGRESS', 'ENQUEUED']: task_log.status = 'ENQUEUED' task_log.save() chain.append('apps.netsuite.tasks.create_credit_card_charge', expense_group, task_log.id) task_log.save() if chain.length(): chain.run()
def test_chain(broker): broker.purge_queue() broker.cache.clear() task_chain = Chain(sync=True) task_chain.append('math.floor', 1) task_chain.append('math.copysign', 1, -1) task_chain.append('math.floor', 2) assert task_chain.length() == 3 assert task_chain.current() is None task_chain.run() r = task_chain.result(wait=1000) assert task_chain.current() == task_chain.length() assert len(r) == task_chain.length() t = task_chain.fetch() assert len(t) == task_chain.length() task_chain.cached = True task_chain.append('math.floor', 3) assert task_chain.length() == 4 task_chain.run() r = task_chain.result(wait=1000) assert task_chain.current() == task_chain.length() assert len(r) == task_chain.length() t = task_chain.fetch() assert len(t) == task_chain.length() # test single rid = async_chain( ['django_q.tests.tasks.hello', 'django_q.tests.tasks.hello'], sync=True, cached=True) assert result_group(rid, cached=True) == ['hello', 'hello']
def test_chain(broker): broker.purge_queue() broker.cache.clear() task_chain = Chain(sync=True) task_chain.append('math.floor', 1) task_chain.append('math.copysign', 1, -1) task_chain.append('math.floor', 2) assert task_chain.length() == 3 assert task_chain.current() is None task_chain.run() r = task_chain.result(wait=1000) assert task_chain.current() == task_chain.length() assert len(r) == task_chain.length() t = task_chain.fetch() assert len(t) == task_chain.length() task_chain.cached = True task_chain.append('math.floor', 3) assert task_chain.length() == 4 task_chain.run() r = task_chain.result(wait=1000) assert task_chain.current() == task_chain.length() assert len(r) == task_chain.length() t = task_chain.fetch() assert len(t) == task_chain.length() # test single rid = async_chain(['django_q.tests.tasks.hello', 'django_q.tests.tasks.hello'], sync=True, cached=True) assert result_group(rid, cached=True) == ['hello', 'hello']