Skip to content

Commit 1da63e6

Browse files
NO-REF: Refactoring main.py (#497)
1 parent 5838e74 commit 1da63e6

File tree

2 files changed

+32
-216
lines changed

2 files changed

+32
-216
lines changed

main.py

+32-46
Original file line numberDiff line numberDiff line change
@@ -7,88 +7,74 @@
77
from load_env import load_env_file
88
from logger import create_log
99

10-
#NEW_RELIC_LICENSE_KEY = Put license key here
11-
#ENVIRONMENT = Put environment here
1210

1311
if os.environ.get('NEW_RELIC_LICENSE_KEY', None):
1412
newrelic.agent.initialize(
1513
config_file='newrelic.ini',
1614
environment=os.environ.get('ENVIRONMENT', 'local')
17-
)
15+
)
16+
1817

1918
def main(args):
2019
logger = create_log(__name__)
2120

2221
environment = args.environment
2322
process = args.process
24-
procType = args.ingestType
25-
customFile = args.inputFile
26-
startDate = args.startDate
27-
singleRecord = args.singleRecord
23+
process_type = args.ingestType
24+
custom_file = args.inputFile
25+
start_date = args.startDate
26+
single_record = args.singleRecord
2827
limit = args.limit
2928
offset = args.offset
3029
options = args.options
3130

32-
logger.info('Starting process {} in {}'.format(process, environment))
33-
logger.debug('Process Args Type: {}, Limit: {}, Offset: {}, Date: {}, File: {}, Record: {}'.format(
34-
procType, limit, offset, startDate, customFile, singleRecord
35-
))
31+
logger.info(f'Starting process {process} in {environment}')
3632

37-
availableProcesses = registerProcesses()
33+
available_processes = register_processes()
3834

3935
try:
40-
procClass = availableProcesses[process]
41-
processInstance = procClass(
42-
procType, customFile, startDate, singleRecord, limit, offset, options
43-
)
36+
process_class = available_processes[process]
37+
38+
process_instance = process_class(process_type, custom_file, start_date, single_record, limit, offset, options)
4439
except:
4540
logger.exception(f'Failed to initialize process {process} in {environment}')
4641
return
4742

48-
if process in (
49-
"APIProcess", # Covered by newrelic's automatic Flask integration
50-
"DevelopmentSetupProcess",
51-
"MigrationProcess",
52-
):
53-
processInstance.runProcess()
43+
if process in ('APIProcess', 'DevelopmentSetupProcess', 'MigrationProcess'):
44+
process_instance.runProcess()
5445
else:
5546
app = newrelic.agent.register_application(timeout=10.0)
56-
with newrelic.agent.BackgroundTask(app, name=process):
57-
logger.info(f'Running process {process} in {environment}')
58-
processInstance.runProcess()
47+
48+
with newrelic.agent.BackgroundTask(app, name=process_instance):
49+
process_instance.runProcess()
5950

6051

61-
def registerProcesses():
52+
def register_processes():
6253
import processes
63-
procs = inspect.getmembers(processes, inspect.isclass)
64-
return dict(procs)
54+
55+
process_classes = inspect.getmembers(processes, inspect.isclass)
56+
57+
return dict(process_classes)
6558

6659

67-
def createArgParser():
60+
def create_arg_parser():
6861
parser = argparse.ArgumentParser(description='Run DCDW Data Ingest Jobs')
69-
parser.add_argument('-p', '--process', required=True,
70-
help='The name of the process job to be run')
71-
parser.add_argument('-e', '--environment', required=True,
72-
help='Environment for deployment, sets env file to load')
73-
parser.add_argument('-i', '--ingestType',
74-
help='The interval to run the ingest over. Generally daily/complete/custom')
75-
parser.add_argument('-f', '--inputFile',
76-
help='Name of file to ingest. Ignored if -i custom is not set')
77-
parser.add_argument('-s', '--startDate',
78-
help='Start point for coverage period to query/process')
79-
parser.add_argument('-l', '--limit',
80-
help='Set overall limit for number of records imported in this process')
81-
parser.add_argument('-o', '--offset',
82-
help='Set start offset for current processed (for batched import process)')
83-
parser.add_argument('-r', '--singleRecord',
84-
help='Single record ID for ingesting an individual record')
62+
63+
parser.add_argument('-p', '--process', required=True, help='The name of the process job to be run')
64+
parser.add_argument('-e', '--environment', required=True, help='Environment for deployment, sets env file to load')
65+
parser.add_argument('-i', '--ingestType', help='The interval to run the ingest over. Generally daily/complete/custom')
66+
parser.add_argument('-f', '--inputFile', help='Name of file to ingest. Ignored if -i custom is not set')
67+
parser.add_argument('-s', '--startDate', help='Start point for coverage period to query/process')
68+
parser.add_argument('-l', '--limit', help='Set overall limit for number of records imported in this process')
69+
parser.add_argument('-o', '--offset', help='Set start offset for current processed (for batched import process)')
70+
parser.add_argument('-r', '--singleRecord', help='Single record ID for ingesting an individual record')
8571
parser.add_argument('options', nargs='*', help='Additional arguments')
8672

8773
return parser
8874

8975

9076
if __name__ == '__main__':
91-
parser = createArgParser()
77+
parser = create_arg_parser()
9278
args = parser.parse_args()
9379

9480
load_env_file(args.environment, './config/{}.yaml')

tests/unit/test_main_process.py

-170
This file was deleted.

0 commit comments

Comments
 (0)