|
| 1 | +Traditionally, batch processes/applications are commonly built as top-down scripts. |
| 2 | + |
| 3 | +It is not uncommon to see even long-running batch processes implemented as long top-down Shell/Python scripts or a series of individual scripts to be executed in specific order. |
| 4 | + |
| 5 | +In the event of a failure, processes may need to be resumed/restarted from the specific point of failure, rather than from the start. To do this, state management must either be implemented or a larger orchestration tool may be used. |
| 6 | + |
| 7 | +# Sample Python Script |
| 8 | + |
| 9 | +Let's take a very simple (and very much contrived) example: |
| 10 | + |
| 11 | +```python |
| 12 | +import urllib.request |
| 13 | +import pandas as pd |
| 14 | +from datetime import datetime |
| 15 | + |
| 16 | +def emit_slack_message(message): |
| 17 | + # Assume implementation |
| 18 | + # ... |
| 19 | + |
| 20 | +def load_to_mysql(df): |
| 21 | + # Assume implementation |
| 22 | + # ... |
| 23 | + |
| 24 | +def write_as_parquet(df, path): |
| 25 | + # Assume implementation |
| 26 | + # ... |
| 27 | + |
| 28 | +# Broadcast job start notification |
| 29 | +print('Starting example batch process') |
| 30 | +run_date_str = datetime.now().strftime('%Y-%m-%d') |
| 31 | +emit_slack_message('Starting daily data download for {}'.format(run_date_str)) |
| 32 | + |
| 33 | +# Download data file 1 |
| 34 | +print('Initiating File Download 1') |
| 35 | +url1 = 'https://some_file_url_here_1' |
| 36 | +urllib.request.urlretrieve(url1, '/landing/data1.csv') |
| 37 | + |
| 38 | +# Download data file 2 |
| 39 | +print('Initiating File Download 2') |
| 40 | +url2 = 'https://some_file_url_here_2' |
| 41 | +urllib.request.urlretrieve(url2, '/landing/data2.csv') |
| 42 | + |
| 43 | +# Load file 1 to MYSQL |
| 44 | +print('Loading MYSQL table') |
| 45 | +df1 = pd.read_csv('/landing/data1.csv') |
| 46 | +load_to_mysql(df1) |
| 47 | + |
| 48 | +# Write combined file as parquet |
| 49 | +print('Writing Parquet file') |
| 50 | +df2 = pd.read_csv('/landing/data2.csv') |
| 51 | +write_as_parquet(df2, '/data/extract_{}.parquet'.format(run_date_str)) |
| 52 | + |
| 53 | +# Broadcast job end notification |
| 54 | +print('Completed example batch process') |
| 55 | +emit_slack_message('Successfully completed daily data download for {}'.format(run_date_str)) |
| 56 | +``` |
| 57 | + |
| 58 | +As with any script, all steps execute in serial fashion and upon failure, a restart will run all steps again. |
| 59 | + |
| 60 | +# Worker Classes |
| 61 | + |
| 62 | +We can instead convert this into a PyRunner application with minor changes by separating each logical step into Worker classes: |
| 63 | + |
| 64 | +```python |
| 65 | +# <app_root_dir>/python/workers.py |
| 66 | + |
| 67 | +import urllib.request |
| 68 | +import pandas as pd |
| 69 | +from datetime import datetime |
| 70 | +from pyrunner import Worker |
| 71 | + |
| 72 | +def emit_slack_message(message): |
| 73 | + # Assume implementation |
| 74 | + # ... |
| 75 | + |
| 76 | +def load_to_mysql(df): |
| 77 | + # Assume implementation |
| 78 | + # ... |
| 79 | + |
| 80 | +def write_as_parquet(df, path): |
| 81 | + # Assume implementation |
| 82 | + # ... |
| 83 | + |
| 84 | +# Broadcast job start notification |
| 85 | +class Start(Worker): |
| 86 | + def run(self): |
| 87 | + # Print function also works, but we can take advantage of |
| 88 | + # advanced features with the provided logger. |
| 89 | + self.logger.info('Starting example batch process') |
| 90 | + |
| 91 | + run_date_str = datetime.now().strftime('%Y-%m-%d') |
| 92 | + emit_slack_message('Starting daily data download for {}'.format(run_date_str)) |
| 93 | + |
| 94 | + # The self.context is a special thread-safe shared dictionary, |
| 95 | + # which can be read or modified from any Worker. |
| 96 | + self.context['run_date_str'] = run_date_str |
| 97 | + |
| 98 | +# Download data file 1 |
| 99 | +class DownloadFile1(Worker): |
| 100 | + def run(self): |
| 101 | + self.logger.info('Initiating File Download 1') |
| 102 | + url = 'https://some_file_url_here_1' |
| 103 | + urllib.request.urlretrieve(url, '/landing/data1.csv') |
| 104 | + |
| 105 | +# Download data file 2 |
| 106 | +class DownloadFile2(Worker): |
| 107 | + def run(self): |
| 108 | + self.logger.info('Initiating File Download 2') |
| 109 | + url = 'https://some_file_url_here_2' |
| 110 | + urllib.request.urlretrieve(url, '/landing/data2.csv') |
| 111 | + |
| 112 | +# Load file 1 to MYSQL |
| 113 | +class LoadMySQL(Worker): |
| 114 | + def run(self): |
| 115 | + self.logger.info('Loading MySQL table') |
| 116 | + df = pd.read_csv('/landing/data1.csv') |
| 117 | + load_to_mysql(df) |
| 118 | + |
| 119 | +# Write file 2 as parquet |
| 120 | +class WriteParquet(Worker): |
| 121 | + def run(self): |
| 122 | + self.logger.info('Writing Parquet file') |
| 123 | + |
| 124 | + # Once again, we are accessing the shared dictionary, this time |
| 125 | + # to read the value originally set by the Start Worker. |
| 126 | + run_date_str = self.context['run_date_str'] |
| 127 | + df = pd.read_csv('/landing/data2.csv') |
| 128 | + write_as_parquet(df, '/data/extract_{}.parquet'.format(run_date_str)) |
| 129 | + |
| 130 | +# Broadcast job end notification |
| 131 | +class End(Worker): |
| 132 | + def run(self): |
| 133 | + self.logger.info('Completed example batch process') |
| 134 | + run_date_str = self.context['run_date_str'] |
| 135 | + emit_slack_message('Successfully completed daily data download for {}'.format(run_date_str)) |
| 136 | +``` |
| 137 | + |
| 138 | +# Task List File |
| 139 | + |
| 140 | +To specify the order of execution of Workers above, we need to express in a `.lst` (to be placed in `<app_root_dir>/config/my_sample_app.lst`) file like: |
| 141 | + |
| 142 | +```bash |
| 143 | +#PYTHON |
| 144 | +#ID|PARENT_IDS|MAX_ATTEMPTS|RETRY_WAIT_TIME|PROCESS_NAME |MODULE_NAME|WORKER_NAME |ARGUMENTS|LOGFILE |
| 145 | +1 |-1 |1 |0 |Start Job Notification|workers |Start | |$ENV{APP_LOG_DIR}/start.log |
| 146 | +2 |1 |1 |0 |Download File 1 |workers |DownloadFile1| |$ENV{APP_LOG_DIR}/download_1.log |
| 147 | +3 |1 |1 |0 |Download File 2 |workers |DownloadFile2| |$ENV{APP_LOG_DIR}/download_2.log |
| 148 | +4 |2 |1 |0 |Load MySQL Table |workers |LoadMySQL | |$ENV{APP_LOG_DIR}/load_mysql.log |
| 149 | +5 |3 |1 |0 |Write Parquet File |workers |WriteParquet | |$ENV{APP_LOG_DIR}/write_parquet.log |
| 150 | +6 |4,5 |1 |0 |End Job Notification |workers |End | |$ENV{APP_LOG_DIR}/end.log |
| 151 | +``` |
| 152 | + |
| 153 | +A visualization of the above `.lst` file: |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +With the above two pieces, we now have the necessary code to run the original script as a PyRunner application. |
0 commit comments