Fixed issue with updates after builds and restarting process

This commit is contained in:
Brett Williams
2025-03-06 00:16:13 -06:00
parent cdbee30109
commit f06d418e29
2 changed files with 43 additions and 27 deletions

View File

@@ -16,9 +16,11 @@ import platform
from zeroconf_server import ZeroconfServer
from version import APP_VERSION, APP_NAME
build_agent_version = "0.1.23"
build_agent_version = "0.1.32"
app = Flask(__name__)
launch_time = datetime.datetime.now()
LAUNCH_DIR = os.curdir
SCRIPT_PATH = os.path.basename(__file__)
LOCAL_DIR = os.path.dirname(__file__)
BUILD_DIR = "pybuild-data"
@@ -69,7 +71,6 @@ def update_files():
check=True)
print("Update complete")
return jsonify({'updated_files': updated_files, 'error_files': error_files}), 200 if not error_files else 500
@app.get("/restart")
@@ -100,6 +101,7 @@ def restart():
try:
return jsonify({"message": "=== Restarting ==="}), 200
finally:
time.sleep(0.1)
os.kill(os.getpid(), signal.SIGTERM)
@app.get("/shutdown")
@@ -109,7 +111,7 @@ def shutdown():
system_status['status'] = "shutting_down"
return jsonify({"message": "Shutting down"}), 200
finally:
time.sleep(1)
time.sleep(0.1)
os.kill(os.getpid(), signal.SIGTERM)
@app.get("/")
@@ -117,7 +119,7 @@ def status_page():
version = platform.mac_ver()[0] if platform.mac_ver() else platform.version()
hostname = socket.gethostname()
return (f"{APP_NAME} - Build Agent {build_agent_version} - \n"
f"{platform.system()} | {cpu_arch()} | {version} | {hostname} | {ZeroconfServer.get_local_ip()}")
f"{system_os()} | {cpu_arch()} | {version} | {hostname} | {ZeroconfServer.get_local_ip()}")
@app.get("/status")
def status():
@@ -137,9 +139,17 @@ def status():
size_in_bytes /= 1024
hostname = socket.gethostname()
return jsonify({"status": system_status['status'], "agent_version": build_agent_version, "os": platform.system(), "cpu": cpu_arch(),
"python_version": platform.python_version(), "hostname": hostname, "ip": ZeroconfServer.get_local_ip(),
"running_job": system_status['running_job'], "job_cache": len(job_cache()), "job_cache_size": format_size(get_directory_size(TMP_DIR))})
return jsonify({"status": system_status['status'],
"agent_version": build_agent_version,
"os": system_os(),
"cpu": cpu_arch(),
"python": platform.python_version(),
"hostname": hostname,
"ip": ZeroconfServer.get_local_ip(),
"job_id": system_status['running_job'],
"cache_size": format_size(get_directory_size(TMP_DIR)),
"uptime": str(datetime.datetime.now() - launch_time)
})
@app.route('/upload', methods=['POST'])
@@ -220,7 +230,6 @@ def install_and_build(project_path, job_id, start_time):
# Compile with PyInstaller
print(f"\n========== Compiling spec file {index+1} of {len(spec_files)} - {spec_file} ==========")
simple_name = os.path.splitext(os.path.basename(spec_file))[0]
os.chdir(project_path)
dist_path = os.path.join(project_path, "dist")
work_path = os.path.join(project_path, "build")
log_file_path = os.path.join(project_path, f"build-{simple_name}.log")
@@ -231,12 +240,10 @@ def install_and_build(project_path, job_id, start_time):
[py_exec, "-m", "PyInstaller", spec_file, "--distpath", dist_path, "--workpath", work_path],
text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
for line in process.stdout:
print(line, end="") # Print to console
log_file.write(line) # Save to log file
log_file.flush() # Ensure real-time writing
process.wait() # Wait for the process to complete
print(f"\n========== Compilation of spec file {spec_file} complete ==========\n")
except Exception as e:
@@ -256,7 +263,7 @@ def install_and_build(project_path, job_id, start_time):
"output_folder": dist_path,
"duration": str(datetime.datetime.now() - start_time),
"cpu": cpu_arch(),
"os": platform.system(),
"os": system_os(),
"hostname": socket.gethostname()
}), 200
@@ -273,6 +280,9 @@ def cpu_arch():
arch = arch.replace(x, y)
return arch
def system_os():
return platform.system().replace("Darwin", "macOS")
@app.route('/download/<job_id>', methods=['GET'])
def download_binaries(job_id):
"""Handles downloading the compiled PyInstaller binaries for a given job."""
@@ -369,13 +379,11 @@ if __name__ == "__main__":
print("Another instance is running. Waiting until it exits.")
time.sleep(1)
os.system('cls' if os.name == 'nt' else 'clear')
print(f"===== {APP_NAME} {APP_VERSION} Build Agent (v{build_agent_version}) =====")
ZeroconfServer.configure("_crosspybuilder._tcp.local.", socket.gethostname(), 9001)
try:
ZeroconfServer.start()
app.run(host="0.0.0.0", port=9001, threaded=True)
except KeyboardInterrupt:
pass
finally: