Script Development / Pre-execution Scripts
Added in version 2.6.18
Important Notice
The pre-execution script directory was changed from init-scripts
to pre-run-scripts
in version 2.6.22.
Meanwhile, the new version will automatically copy all files from init-scripts
to pre-run-scripts
, so users do not need to perform any additional operations.
Although DataFlux Func already provides PIP tool installation for third-party Python packages, it may still be unusable due to missing dependency libraries.
For example, if a user wants to use OpenCV in DataFlux Func, besides installing opencv-python
, they also need to install dependency libraries via apt or similar methods.
Otherwise, the following issues might occur:
1. Resolve Dependencies Using Pre-execution Scripts
To solve this problem, you can provide pre-execution scripts to DataFlux Func. These scripts will execute before DataFlux Func starts, allowing for the 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 APT uses TUNA at Tsinghua University (http://mirrors.tuna.tsinghua.edu.cn/ubuntu
).
As an example to resolve the OpenCV dependency issue mentioned earlier, you can prepare the following Bash script.
Bash | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Save it as the file prepare-for-opencv.sh
.
The filename can be arbitrary but must end with .sh
. To avoid unnecessary trouble, do not use Chinese characters or other unconventional symbols in the filename.
Differentiate Running Environments
DataFlux Func includes both Server and Worker sides.
- The Server side is mainly the HTTP server, providing Web pages and HTTP APIs, and does not participate in executing Python code.
- The Worker side is the actual Python code execution service.
Therefore, in most cases, the pre-execution script only needs to run on the Worker side.
To differentiate environments, you can read $1
and check whether its value is server
or worker
.
Beat services, MySQL services, and Redis services will not execute pre-execution scripts.
Example Bash code for differentiation:
Bash | |
---|---|
1 2 3 4 |
|
Bash | |
---|---|
1 2 3 4 |
|
1.2 Upload the Script
The pre-execution script storage directory 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 the pre-execution script inside the host machine's directory.
Alternatively, you can upload your own pre-execution script by navigating through the file manager in DataFlux Func:
1.3 Restart DataFlux Func and Verify
After completing all preparations, restart DataFlux Func.
Returning to the previous script and running it again, you'll see that the opencv-python
library can now be correctly imported:
2. Pre-execution Script Execution Details
Each time DataFlux Func starts, it first checks whether there are any pre-execution scripts.
When pre-execution scripts exist, DataFlux Func executes them in order based on their filenames, and DataFlux Func will only start normally after all scripts have been successfully executed.
If you need to observe the execution process of the pre-execution script, you can track it using the following command:
Bash | |
---|---|
1 |
|
Using the pre-execution script from the example above, you can see output like 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 |
|
It can be seen that the pre-execution script executed correctly and installed the required dependency packages.