Skip to content

Commit

Permalink
refactor background tasks (#260)
Browse files Browse the repository at this point in the history
Co-authored-by: Ashutosh619-sudo <[email protected]>
  • Loading branch information
Ashutosh619-sudo and Ashutosh619-sudo authored Sep 21, 2023
1 parent 8af8cad commit 078f8bd
Show file tree
Hide file tree
Showing 17 changed files with 243 additions and 229 deletions.
91 changes: 91 additions & 0 deletions apps/mappings/queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from django_q.models import Schedule
from datetime import datetime, timedelta
from fyle_accounting_mappings.models import MappingSetting


def schedule_auto_map_employees(employee_mapping_preference: str, workspace_id: str):
if employee_mapping_preference:
Schedule.objects.update_or_create(
func='apps.mappings.tasks.async_auto_map_employees',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': 24 * 60,
'next_run': datetime.now()
}
)
else:
schedule: Schedule = Schedule.objects.filter(
func='apps.mappings.tasks.async_auto_map_employees',
args='{}'.format(workspace_id)
).first()

if schedule:
schedule.delete()


def schedule_cost_centers_creation(import_to_fyle, workspace_id: int):
if import_to_fyle:
schedule, _ = Schedule.objects.update_or_create(
func='apps.mappings.tasks.auto_create_cost_center_mappings',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': 24 * 60,
'next_run': datetime.now()
}
)
else:
schedule: Schedule = Schedule.objects.filter(
func='apps.mappings.tasks.auto_create_cost_center_mappings',
args='{}'.format(workspace_id)
).first()

if schedule:
schedule.delete()


def schedule_tax_groups_creation(import_tax_codes, workspace_id):
if import_tax_codes:
schedule, _ = Schedule.objects.update_or_create(
func='apps.mappings.tasks.auto_create_tax_codes_mappings',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': 24 * 60,
'next_run': datetime.now()
}
)
else:
schedule: Schedule = Schedule.objects.filter(
func='apps.mappings.tasks.auto_create_tax_codes_mappings',
args='{}'.format(workspace_id),
).first()

if schedule:
schedule.delete()


def schedule_fyle_attributes_creation(workspace_id: int):
mapping_settings = MappingSetting.objects.filter(
is_custom=True, import_to_fyle=True, workspace_id=workspace_id
).all()

if mapping_settings:
schedule, _ = Schedule.objects.get_or_create(
func='apps.mappings.tasks.async_auto_create_custom_field_mappings',
args='{0}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': 24 * 60,
'next_run': datetime.now() + timedelta(hours=24)
}
)
else:
schedule: Schedule = Schedule.objects.filter(
func='apps.mappings.tasks.async_auto_create_custom_field_mappings',
args=workspace_id
).first()

if schedule:
schedule.delete()
4 changes: 2 additions & 2 deletions apps/mappings/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

from fyle_accounting_mappings.models import MappingSetting, ExpenseAttribute, Mapping
from apps.tasks.models import Error
from apps.mappings.tasks import schedule_cost_centers_creation, schedule_fyle_attributes_creation,\
upload_attributes_to_fyle
from apps.mappings.queue import schedule_cost_centers_creation, schedule_fyle_attributes_creation
from apps.mappings.tasks import upload_attributes_to_fyle
from apps.workspaces.models import WorkspaceGeneralSettings

from django.db.models.signals import post_save
Expand Down
88 changes: 0 additions & 88 deletions apps/mappings/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,6 @@ def async_auto_map_employees(workspace_id: int):
resolve_expense_attribute_errors(source_attribute_type='EMPLOYEE', workspace_id=workspace_id)


def schedule_auto_map_employees(employee_mapping_preference: str, workspace_id: str):
if employee_mapping_preference:
Schedule.objects.update_or_create(
func='apps.mappings.tasks.async_auto_map_employees',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': 24 * 60,
'next_run': datetime.now()
}
)
else:
schedule: Schedule = Schedule.objects.filter(
func='apps.mappings.tasks.async_auto_map_employees',
args='{}'.format(workspace_id)
).first()

if schedule:
schedule.delete()


def sync_xero_attributes(xero_attribute_type: str, workspace_id: int):
xero_credentials: XeroCredentials = XeroCredentials.get_active_xero_credentials(workspace_id)
xero_connection = XeroConnector(
Expand Down Expand Up @@ -327,27 +306,6 @@ def auto_create_cost_center_mappings(workspace_id: int):
post_cost_centers_in_batches(platform, workspace_id, mapping_setting.destination_field)


def schedule_cost_centers_creation(import_to_fyle, workspace_id: int):
if import_to_fyle:
schedule, _ = Schedule.objects.update_or_create(
func='apps.mappings.tasks.auto_create_cost_center_mappings',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': 24 * 60,
'next_run': datetime.now()
}
)
else:
schedule: Schedule = Schedule.objects.filter(
func='apps.mappings.tasks.auto_create_cost_center_mappings',
args='{}'.format(workspace_id)
).first()

if schedule:
schedule.delete()


def create_fyle_projects_payload(projects: List[DestinationAttribute], existing_project_names: list,
updated_projects: List[ExpenseAttribute] = None):
"""
Expand Down Expand Up @@ -595,31 +553,6 @@ def async_auto_create_custom_field_mappings(workspace_id: str):
)


def schedule_fyle_attributes_creation(workspace_id: int):
mapping_settings = MappingSetting.objects.filter(
is_custom=True, import_to_fyle=True, workspace_id=workspace_id
).all()

if mapping_settings:
schedule, _ = Schedule.objects.get_or_create(
func='apps.mappings.tasks.async_auto_create_custom_field_mappings',
args='{0}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': 24 * 60,
'next_run': datetime.now() + timedelta(hours=24)
}
)
else:
schedule: Schedule = Schedule.objects.filter(
func='apps.mappings.tasks.async_auto_create_custom_field_mappings',
args=workspace_id
).first()

if schedule:
schedule.delete()


def upload_tax_groups_to_fyle(platform_connection: PlatformConnector, workspace_id: int):
existing_tax_codes_name = ExpenseAttribute.objects.filter(
attribute_type='TAX_GROUP', workspace_id=workspace_id).values_list('value', flat=True)
Expand Down Expand Up @@ -679,27 +612,6 @@ def auto_create_tax_codes_mappings(workspace_id: int):
upload_tax_groups_to_fyle(platform, workspace_id)


def schedule_tax_groups_creation(import_tax_codes, workspace_id):
if import_tax_codes:
schedule, _ = Schedule.objects.update_or_create(
func='apps.mappings.tasks.auto_create_tax_codes_mappings',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': 24 * 60,
'next_run': datetime.now()
}
)
else:
schedule: Schedule = Schedule.objects.filter(
func='apps.mappings.tasks.auto_create_tax_codes_mappings',
args='{}'.format(workspace_id),
).first()

if schedule:
schedule.delete()


def auto_create_suppliers_as_merchants(workspace_id):
fyle_credentials: FyleCredential = FyleCredential.objects.get(workspace_id=workspace_id)
fyle_connection = PlatformConnector(fyle_credentials)
Expand Down
2 changes: 1 addition & 1 deletion apps/mappings/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fyle_xero_api.utils import assert_valid

from .models import TenantMapping, GeneralMapping
from ..xero.tasks import schedule_payment_creation
from ..xero.queue import schedule_payment_creation


class MappingUtils:
Expand Down
2 changes: 1 addition & 1 deletion apps/workspaces/apis/advanced_settings/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rest_framework import serializers

from apps.workspaces.models import Workspace, WorkspaceGeneralSettings, WorkspaceSchedule
from apps.workspaces.tasks import schedule_sync
from apps.workspaces.queue import schedule_sync
from apps.mappings.models import GeneralMapping
from .triggers import AdvancedSettingsTriggers

Expand Down
2 changes: 1 addition & 1 deletion apps/workspaces/apis/advanced_settings/triggers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from apps.xero.tasks import schedule_payment_creation, schedule_xero_objects_status_sync, schedule_reimbursements_sync
from apps.xero.queue import schedule_payment_creation, schedule_xero_objects_status_sync, schedule_reimbursements_sync
from apps.workspaces.models import WorkspaceGeneralSettings


Expand Down
2 changes: 1 addition & 1 deletion apps/workspaces/apis/export_settings/triggers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from apps.mappings.tasks import schedule_auto_map_employees
from apps.mappings.queue import schedule_auto_map_employees
from apps.workspaces.models import WorkspaceGeneralSettings
from fyle_accounting_mappings.models import MappingSetting
from apps.workspaces.utils import delete_cards_mapping_settings, schedule_or_delete_import_supplier_schedule
Expand Down
2 changes: 1 addition & 1 deletion apps/workspaces/apis/import_settings/triggers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict, List
from django.db.models import Q

from apps.mappings.tasks import schedule_cost_centers_creation, schedule_tax_groups_creation,\
from apps.mappings.queue import schedule_cost_centers_creation, schedule_tax_groups_creation,\
schedule_fyle_attributes_creation
from apps.mappings.helpers import schedule_or_delete_fyle_import_tasks
from apps.workspaces.models import WorkspaceGeneralSettings
Expand Down
66 changes: 66 additions & 0 deletions apps/workspaces/queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from datetime import datetime, timedelta
from typing import List
from django_q.models import Schedule
from apps.workspaces.models import WorkspaceSchedule



def schedule_email_notification(workspace_id: int, schedule_enabled: bool, hours: int):
if schedule_enabled:
schedule, _ = Schedule.objects.update_or_create(
func='apps.workspaces.tasks.run_email_notification',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': hours * 60,
'next_run': datetime.now() + timedelta(minutes=10)
}
)
else:
schedule: Schedule = Schedule.objects.filter(
func='apps.workspaces.tasks.run_email_notification',
args='{}'.format(workspace_id)
).first()

if schedule:
schedule.delete()


def schedule_sync(workspace_id: int, schedule_enabled: bool, hours: int, email_added: List, emails_selected: List):
ws_schedule, _ = WorkspaceSchedule.objects.get_or_create(
workspace_id=workspace_id
)

schedule_email_notification(workspace_id=workspace_id, schedule_enabled=schedule_enabled, hours=hours)

if schedule_enabled:
ws_schedule.enabled = schedule_enabled
ws_schedule.start_datetime = datetime.now()
ws_schedule.interval_hours = hours
ws_schedule.emails_selected = emails_selected

if email_added:
ws_schedule.additional_email_options.append(email_added)

schedule, _ = Schedule.objects.update_or_create(
func='apps.workspaces.tasks.run_sync_schedule',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': hours * 60,
'next_run': datetime.now()
}
)

ws_schedule.schedule = schedule

ws_schedule.save()

elif not schedule_enabled and ws_schedule.schedule:
schedule = ws_schedule.schedule
ws_schedule.enabled = schedule_enabled
ws_schedule.schedule = None
ws_schedule.save()
schedule.delete()

return ws_schedule
61 changes: 0 additions & 61 deletions apps/workspaces/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,6 @@
logger.level = logging.INFO


def schedule_email_notification(workspace_id: int, schedule_enabled: bool, hours: int):
if schedule_enabled:
schedule, _ = Schedule.objects.update_or_create(
func='apps.workspaces.tasks.run_email_notification',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': hours * 60,
'next_run': datetime.now() + timedelta(minutes=10)
}
)
else:
schedule: Schedule = Schedule.objects.filter(
func='apps.workspaces.tasks.run_email_notification',
args='{}'.format(workspace_id)
).first()

if schedule:
schedule.delete()


def schedule_sync(workspace_id: int, schedule_enabled: bool, hours: int, email_added: List, emails_selected: List):
ws_schedule, _ = WorkspaceSchedule.objects.get_or_create(
workspace_id=workspace_id
)

schedule_email_notification(workspace_id=workspace_id, schedule_enabled=schedule_enabled, hours=hours)

if schedule_enabled:
ws_schedule.enabled = schedule_enabled
ws_schedule.start_datetime = datetime.now()
ws_schedule.interval_hours = hours
ws_schedule.emails_selected = emails_selected

if email_added:
ws_schedule.additional_email_options.append(email_added)

schedule, _ = Schedule.objects.update_or_create(
func='apps.workspaces.tasks.run_sync_schedule',
args='{}'.format(workspace_id),
defaults={
'schedule_type': Schedule.MINUTES,
'minutes': hours * 60,
'next_run': datetime.now()
}
)

ws_schedule.schedule = schedule

ws_schedule.save()

elif not schedule_enabled and ws_schedule.schedule:
schedule = ws_schedule.schedule
ws_schedule.enabled = schedule_enabled
ws_schedule.schedule = None
ws_schedule.save()
schedule.delete()

return ws_schedule


def run_sync_schedule(workspace_id):
"""
Run schedule
Expand Down
Loading

0 comments on commit 078f8bd

Please sign in to comment.