Script Development / Pre-run Script
Added in version 2.6.18
Important Note
The pre-run script directory was changed from init-scripts
to pre-run-scripts
starting from version 2.6.22.
At the same time, the new version will automatically copy all files from init-scripts
to pre-run-scripts
, and users do not need to perform additional actions.
Although DataFlux Func already provides PIP tool for installing third-party Python packages, they may not function properly due to missing dependency libraries.
For example, when using OpenCV in DataFlux Func, apart from installing opencv-python
, it is also necessary to install dependency libraries through apt or other means.
Otherwise, the following issues might occur:
1. Solving Dependencies with Pre-run Scripts
To resolve this issue, you can provide a pre-run script to DataFlux Func. A pre-run script will execute before DataFlux Func starts, allowing installation of necessary dependencies.
The specific steps are as follows:
1.1 Prepare the Script
The DataFlux Func image is based on Ubuntu:22.04, and the APT source uses Tsinghua University TUNA mirror (http://mirrors.tuna.tsinghua.edu.cn/ubuntu
).
Taking the previously mentioned OpenCV dependency issue as an example, prepare the following Bash script.
Bash | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Save it as prepare-for-opencv.sh
.
The filename can be arbitrary but must end with .sh
. To avoid unnecessary issues, avoid using Chinese characters or other unconventional symbols in the filename.
Distinguishing Execution Environments
DataFlux Func consists of Server and Worker sides.
- The Server side primarily functions as an HTTP server, providing Web pages and HTTP APIs, and does not participate in Python code execution.
- The Worker side handles actual Python code execution.
Therefore, in most cases, pre-run scripts only need to be executed on the Worker side.
When needing to distinguish the environment, read $1
to determine whether its value is server
or worker
.
Beat service, MySQL service, Redis service will not execute pre-run scripts
Sample Bash code for distinguishing environments is as follows:
Bash | |
---|---|
1 2 3 4 |
|
Bash | |
---|---|
1 2 3 4 |
|
1.2 Upload the Script
The location for storing pre-run scripts is as follows:
Environment | Location |
---|---|
Inside container | /data/resources/pre-run-scripts/ |
On host machine | {Installation Directory}/data/resources/pre-run-scripts/ |
Users can place pre-run scripts into this directory on the host machine.
Alternatively, within DataFlux Func's File Management, navigate inside and upload your custom-prepared pre-run script:
1.3 Restart DataFlux Func and Verify
After completing all preparations, restart DataFlux Func
Return to the previous script and execute it again; you should now see that the opencv-python
library can be imported correctly:
2. Details of Pre-run Script Execution
Each time DataFlux Func starts, it first checks if there are any pre-run scripts present.
When pre-run scripts exist, DataFlux Func executes them sequentially according to their filenames sorted in alphabetical order. DataFlux Func will only start normally after all scripts have executed successfully.
If you want to observe the execution process of the pre-run scripts, you can track it using the following command:
Bash | |
---|---|
1 |
|
Taking the aforementioned pre-run script as an example, you would see output similar to the following:
Text Only | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
You can see that the pre-run script executed correctly and installed the required dependency packages.