@@ -24,8 +24,13 @@ if [ "$CORTEX_VERSION" != "$EXPECTED_CORTEX_VERSION" ]; then
24
24
exit 1
25
25
fi
26
26
27
- # configure log level for python scripts
28
- /opt/conda/envs/env/bin/python -c " from cortex_internal.lib import util; import os; util.expand_environment_vars_on_file(os.environ['CORTEX_LOG_CONFIG_FILE'])"
27
+ function substitute_env_vars() {
28
+ file_to_run_substitution=$1
29
+ /opt/conda/envs/env/bin/python -c " from cortex_internal.lib import util; import os; util.expand_environment_vars_on_file('$file_to_run_substitution ')"
30
+ }
31
+
32
+ # configure log level for python scripts§
33
+ substitute_env_vars $CORTEX_LOG_CONFIG_FILE
29
34
30
35
mkdir -p /mnt/workspace
31
36
mkdir -p /mnt/requests
@@ -111,55 +116,45 @@ else
111
116
rm $tempf
112
117
fi
113
118
114
- # good pages to read about s6-overlay used in create_s6_service and create_s6_task
115
- # https://wiki.gentoo.org/wiki/S6#Process_supervision
116
- # https://skarnet.org/software/s6/s6-svscanctl.html
117
- # http://skarnet.org/software/s6/s6-svc.html
118
- # http://skarnet.org/software/s6/servicedir.html
119
-
120
- # good pages to read about execline
121
- # http://www.troubleshooters.com/linux/execline.htm
122
- # https://danyspin97.org/blog/getting-started-with-execline-scripting/
123
-
124
119
# only terminate pod if this process exits with non-zero exit code
125
120
create_s6_service () {
126
- service_name =$1
127
- cmd =$2
121
+ export SERVICE_NAME =$1
122
+ export COMMAND_TO_RUN =$2
128
123
129
- dest_dir=" /etc/services.d/$service_name "
124
+ dest_dir=" /etc/services.d/$SERVICE_NAME "
130
125
mkdir $dest_dir
131
126
132
127
dest_script=" $dest_dir /run"
133
- echo " #!/usr/bin/with-contenv bash " > $dest_script
134
- echo $cmd >> $dest_script
128
+ cp /src/cortex/serve/init/templates/run $dest_script
129
+ substitute_env_vars $dest_script
135
130
chmod +x $dest_script
136
131
137
132
dest_script=" $dest_dir /finish"
138
- echo " #!/usr/bin/execlineb -S0" > $dest_script
139
- echo " ifelse { s6-test \$ {1} -ne 0 } { foreground { redirfd -w 1 /var/run/s6/env-stage3/S6_STAGE2_EXITED s6-echo -n -- \$ {1} } s6-svscanctl -t /var/run/s6/services }" >> $dest_script
140
- echo " s6-svc -O /var/run/s6/services/$service_name " >> $dest_script
133
+ cp /src/cortex/serve/init/templates/finish $dest_script
134
+ substitute_env_vars $dest_script
141
135
chmod +x $dest_script
136
+
137
+ unset SERVICE_NAME
138
+ unset COMMAND_TO_RUN
142
139
}
143
140
144
- # terminate pod if this process exits (zero or non-zero exit code)
145
- create_s6_task () {
146
- task_name =$1
147
- cmd =$2
141
+ # only terminate pod if this process exits with non-zero exit code
142
+ create_s6_service_from_file () {
143
+ export SERVICE_NAME =$1
144
+ runnable =$2
148
145
149
- dest_dir=" /etc/services.d/$task_name "
146
+ dest_dir=" /etc/services.d/$SERVICE_NAME "
150
147
mkdir $dest_dir
151
148
152
- dest_script=" $dest_dir /run"
153
- echo " #!/usr/bin/with-contenv bash" > $dest_script
154
- echo $cmd >> $dest_script
155
- chmod +x $dest_script
149
+ cp $runnable $dest_dir /run
150
+ chmod +x $dest_dir /run
156
151
157
152
dest_script=" $dest_dir /finish"
158
- echo " #!/usr/bin/execlineb -S0" > $dest_script
159
- echo " ifelse { s6-test \$ {1} -ne 0 } { foreground { redirfd -w 1 /var/run/s6/env-stage3/S6_STAGE2_EXITED s6-echo -n -- \$ {1} } s6-svscanctl -t /var/run/s6/services }" >> $dest_script
160
- echo " s6-svscanctl -t /var/run/s6/services" >> $dest_script
161
-
153
+ cp /src/cortex/serve/init/templates/finish $dest_script
154
+ substitute_env_vars $dest_script
162
155
chmod +x $dest_script
156
+
157
+ unset SERVICE_NAME
163
158
}
164
159
165
160
# prepare webserver
@@ -168,27 +163,19 @@ if [ "$CORTEX_KIND" = "RealtimeAPI" ]; then
168
163
# prepare uvicorn workers
169
164
mkdir /run/uvicorn
170
165
for i in $( seq 1 $CORTEX_PROCESSES_PER_REPLICA ) ; do
171
- create_s6_service " uvicorn-$(( i- 1 )) " " cd /mnt/project && $source_env_file_cmd && exec env PYTHONUNBUFFERED=TRUE env PYTHONPATH=$PYTHONPATH :$CORTEX_PYTHON_PATH /opt/conda/envs/env/bin/python /src/cortex/serve/start/server.py /run/uvicorn/proc-$(( i- 1 )) .sock"
166
+ create_s6_service " uvicorn-$(( i- 1 )) " " cd /mnt/project && $source_env_file_cmd && PYTHONUNBUFFERED=TRUE PYTHONPATH=$PYTHONPATH :$CORTEX_PYTHON_PATH exec /opt/conda/envs/env/bin/python /src/cortex/serve/start/server.py /run/uvicorn/proc-$(( i- 1 )) .sock"
172
167
done
173
168
174
- create_s6_service " nginx" " exec nginx -c /run/nginx.conf"
175
-
176
- # prepare api readiness checker
177
- dest_dir=" /etc/services.d/api_readiness"
178
- mkdir $dest_dir
179
- cp /src/cortex/serve/poll/readiness.sh $dest_dir /run
180
- chmod +x $dest_dir /run
181
-
182
169
# generate nginx conf
183
170
/opt/conda/envs/env/bin/python -c ' from cortex_internal.lib import util; import os; generated = util.render_jinja_template("/src/cortex/serve/nginx.conf.j2", os.environ); print(generated);' > /run/nginx.conf
184
171
185
- # create the python initialization service
186
- create_s6_service " py_init" " cd /mnt/project && /opt/conda/envs/env/bin/python /src/cortex/serve/init/script.py"
187
- elif [ " $CORTEX_KIND " = " BatchAPI" ]; then
188
- create_s6_task " batch" " cd /mnt/project && $source_env_file_cmd && PYTHONUNBUFFERED=TRUE PYTHONPATH=$PYTHONPATH :$CORTEX_PYTHON_PATH /opt/conda/envs/env/bin/python /src/cortex/serve/start/batch.py"
172
+ create_s6_service " py_init" " cd /mnt/project && exec /opt/conda/envs/env/bin/python /src/cortex/serve/init/script.py"
173
+ create_s6_service " nginx" " exec nginx -c /run/nginx.conf"
174
+ create_s6_service_from_file " api_readiness" " /src/cortex/serve/poll/readiness.sh"
189
175
190
- # create the python initialization service
191
- create_s6_service " py_init" " cd /mnt/project && /opt/conda/envs/env/bin/python /src/cortex/serve/init/script.py"
176
+ elif [ " $CORTEX_KIND " = " BatchAPI" ]; then
177
+ create_s6_service " py_init" " cd /mnt/project && exec /opt/conda/envs/env/bin/python /src/cortex/serve/init/script.py"
178
+ create_s6_service " batch" " cd /mnt/project && $source_env_file_cmd && PYTHONUNBUFFERED=TRUE PYTHONPATH=$PYTHONPATH :$CORTEX_PYTHON_PATH exec /opt/conda/envs/env/bin/python /src/cortex/serve/start/batch.py"
192
179
elif [ " $CORTEX_KIND " = " TaskAPI" ]; then
193
- create_s6_task " task" " cd /mnt/project && $source_env_file_cmd && PYTHONUNBUFFERED=TRUE PYTHONPATH=$PYTHONPATH :$CORTEX_PYTHON_PATH /opt/conda/envs/env/bin/python /src/cortex/serve/start/task.py"
180
+ create_s6_service " task" " cd /mnt/project && $source_env_file_cmd && PYTHONUNBUFFERED=TRUE PYTHONPATH=$PYTHONPATH :$CORTEX_PYTHON_PATH exec /opt/conda/envs/env/bin/python /src/cortex/serve/start/task.py"
194
181
fi
0 commit comments