Skip to content

Commit

Permalink
21475 Dissolutions job - Update stage 1 & stage 2 processes (#2751)
Browse files Browse the repository at this point in the history
* 21475 - dissolutions job - update stage 1 & stage 2 processes

Signed-off-by: Hongjing Chen <[email protected]>

* update delay configs with default values

Signed-off-by: Hongjing Chen <[email protected]>

---------

Signed-off-by: Hongjing Chen <[email protected]>
  • Loading branch information
chenhongjing authored Jun 17, 2024
1 parent f521f9d commit 45f224c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
4 changes: 2 additions & 2 deletions jobs/involuntary-dissolutions/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class _Config(object): # pylint: disable=too-few-public-methods

SUMMARY_STORAGE_PATH = os.getenv('SUMMARY_STORAGE_PATH', '')
SECOND_NOTICE_DELAY = os.getenv('SECOND_NOTICE_DELAY', None)
STAGE_1_DELAY = os.getenv('STAGE_1_DELAY', None)
STAGE_2_DELAY = os.getenv('STAGE_2_DELAY', None)
STAGE_1_DELAY = int(os.getenv('STAGE_1_DELAY', '42'))
STAGE_2_DELAY = int(os.getenv('STAGE_2_DELAY', '30'))


class DevConfig(_Config): # pylint: disable=too-few-public-methods
Expand Down
34 changes: 13 additions & 21 deletions jobs/involuntary-dissolutions/involuntary_dissolutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from legal_api.services.flags import Flags
from legal_api.services.involuntary_dissolution import InvoluntaryDissolutionService
from sentry_sdk.integrations.logging import LoggingIntegration
from sqlalchemy import Date, cast, func, text
from sqlalchemy import Date, cast, func

import config # pylint: disable=import-error
from utils.logging import setup_logging # pylint: disable=import-error
Expand Down Expand Up @@ -77,8 +77,7 @@ def initiate_dissolution_process(app: Flask): # pylint: disable=redefined-outer
"""Initiate dissolution process for new businesses that meet dissolution criteria."""
try:
# check if batch has already run today
tz = pytz.timezone('US/Pacific')
today_date = tz.localize(datetime.today()).date()
today_date = datetime.utcnow().date()

batch_today = (
db.session.query(Batch)
Expand All @@ -98,11 +97,15 @@ def initiate_dissolution_process(app: Flask): # pylint: disable=redefined-outer
app.logger.debug('Skipping job run since there are no businesses eligible for dissolution.')
return

# get stage_1 & stage_2 delay configs
stage_1_delay = timedelta(days=app.config.get('STAGE_1_DELAY'))
stage_2_delay = timedelta(days=app.config.get('STAGE_2_DELAY'))

# create new entry in batches table
batch = Batch(batch_type=Batch.BatchType.INVOLUNTARY_DISSOLUTION,
status=Batch.BatchStatus.PROCESSING,
size=len(businesses_eligible),
start_date=datetime.now())
start_date=datetime.utcnow())
batch.save()

# create batch processing entries for each business being dissolved
Expand All @@ -111,20 +114,11 @@ def initiate_dissolution_process(app: Flask): # pylint: disable=redefined-outer
batch_processing = BatchProcessing(business_identifier=business.identifier,
step=BatchProcessing.BatchProcessingStep.WARNING_LEVEL_1,
status=BatchProcessing.BatchProcessingStatus.PROCESSING,
created_date=datetime.now(),
created_date=datetime.utcnow(),
trigger_date=datetime.utcnow()+stage_1_delay,
batch_id=batch.id,
business_id=business.id)

if (stage_1_delay_config := app.config.get('STAGE_1_DELAY', 0)):
stage_1_delay = timedelta(days=int(stage_1_delay_config))
else:
stage_1_delay = timedelta(days=0)

if (stage_2_delay_config := app.config.get('STAGE_2_DELAY', 0)):
stage_2_delay = timedelta(days=int(stage_2_delay_config))
else:
stage_2_delay = timedelta(days=0)

target_dissolution_date = batch_processing.created_date + stage_1_delay + stage_2_delay

batch_processing.meta_data = {
Expand All @@ -140,10 +134,6 @@ def initiate_dissolution_process(app: Flask): # pylint: disable=redefined-outer

def stage_2_process(app: Flask):
"""Run dissolution stage 2 process for businesses meet moving criteria."""
if not (stage_1_delay := app.config.get('STAGE_1_DELAY', None)):
app.logger.debug('Skipping stage 2 run since config STAGE_1_DELAY is missing.')
return

batch_processings = (
db.session.query(BatchProcessing)
.filter(BatchProcessing.batch_id == Batch.id)
Expand All @@ -156,20 +146,22 @@ def stage_2_process(app: Flask):
BatchProcessing.step == BatchProcessing.BatchProcessingStep.WARNING_LEVEL_1
)
.filter(
BatchProcessing.created_date + text(f"""INTERVAL '{stage_1_delay} DAYS'""")
<= func.timezone('UTC', func.now())
BatchProcessing.trigger_date <= func.timezone('UTC', func.now())
)
.all()
)

# TODO: add check if warnings have been sent out & set batch_processing.status to error if not

stage_2_delay = timedelta(days=app.config.get('STAGE_2_DELAY'))

for batch_processing in batch_processings:
eligible, _ = InvoluntaryDissolutionService.check_business_eligibility(
batch_processing.business_identifier, exclude_in_dissolution=False
)
if eligible:
batch_processing.step = BatchProcessing.BatchProcessingStep.WARNING_LEVEL_2
batch_processing.trigger_date = datetime.utcnow() + stage_2_delay
else:
batch_processing.status = BatchProcessing.BatchProcessingStatus.WITHDRAWN
batch_processing.notes = 'Moved back into good standing'
Expand Down
3 changes: 3 additions & 0 deletions jobs/involuntary-dissolutions/tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""The Test-Suite used to ensure that the Involuntary Dissolutions Job is working correctly."""

import datetime
from datedelta import datedelta

from legal_api.models import Batch, BatchProcessing, Business

Expand Down Expand Up @@ -66,6 +67,7 @@ def factory_batch_processing(batch_id,
step=BatchProcessing.BatchProcessingStep.WARNING_LEVEL_1,
status=BatchProcessing.BatchProcessingStatus.PROCESSING,
created_date=datetime.datetime.utcnow(),
trigger_date=datetime.datetime.utcnow()+datedelta(days=42),
last_modified=datetime.datetime.utcnow(),
notes=''):
"""Create a batch processing entry."""
Expand All @@ -76,6 +78,7 @@ def factory_batch_processing(batch_id,
step=step,
status=status,
created_date=created_date,
trigger_date=trigger_date,
last_modified=last_modified,
notes=notes
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@


CREATED_DATE = (datetime.utcnow() + datedelta(days=-60)).replace(tzinfo=pytz.UTC)
TRIGGER_DATE = CREATED_DATE + datedelta(days=42)


def test_check_run_schedule():
Expand Down Expand Up @@ -103,6 +104,7 @@ def test_initiate_dissolution_process(app, session):
assert batch_processing.step == BatchProcessing.BatchProcessingStep.WARNING_LEVEL_1
assert batch_processing.status == BatchProcessing.BatchProcessingStatus.PROCESSING
assert batch_processing.created_date.date() == datetime.now().date()
assert batch_processing.trigger_date.date() == datetime.now().date() + datedelta(days=42)
assert batch_processing.meta_data


Expand Down Expand Up @@ -164,6 +166,7 @@ def test_stage_2_process_find_entry(app, session, test_name, batch_status, statu
status=status,
step=step,
created_date=created_date,
trigger_date=created_date+datedelta(days=42),
last_modified=last_modified
)

Expand Down Expand Up @@ -197,7 +200,8 @@ def test_stage_2_process_update_business(app, session, test_name, status, step):
batch_id=batch.id,
business_id=business.id,
identifier=business.identifier,
created_date=CREATED_DATE
created_date=CREATED_DATE,
trigger_date=TRIGGER_DATE
)

if test_name == 'MOVE_BACK_2_GOOD_STANDING':
Expand All @@ -208,3 +212,8 @@ def test_stage_2_process_update_business(app, session, test_name, status, step):

assert batch_processing.status == status
assert batch_processing.step == step

if test_name == 'MOVE_2_STAGE_2':
assert batch_processing.trigger_date.date() == datetime.utcnow().date() + datedelta(days=30)
else:
assert batch_processing.trigger_date == TRIGGER_DATE

0 comments on commit 45f224c

Please sign in to comment.