mirror of
https://github.com/ANL-CEEESA/UnitCommitment.jl.git
synced 2025-12-09 17:48:52 -06:00
web: backend: Implement job queue
This commit is contained in:
@@ -12,73 +12,26 @@ import Backend
|
||||
import JuliaFormatter
|
||||
using HiGHS
|
||||
|
||||
basedir = dirname(@__FILE__)
|
||||
port = 32617
|
||||
BASEDIR = dirname(@__FILE__)
|
||||
|
||||
include("jobs_test.jl")
|
||||
include("server_test.jl")
|
||||
|
||||
function fixture(path::String)::String
|
||||
return "$basedir/../fixtures/$path"
|
||||
end
|
||||
|
||||
function with_server(f)
|
||||
logger = Test.TestLogger()
|
||||
# server = Base.CoreLogging.with_logger(logger) do
|
||||
# Backend.start_server(port)
|
||||
# end
|
||||
server = Backend.start_server(port; optimizer=HiGHS.Optimizer)
|
||||
try
|
||||
f()
|
||||
finally
|
||||
close(server)
|
||||
end
|
||||
return filter!(x -> x.group == :access, logger.logs)
|
||||
end
|
||||
|
||||
function test_usage()
|
||||
with_server() do
|
||||
# Read the compressed fixture file
|
||||
compressed_data = read(fixture("case14.json.gz"))
|
||||
|
||||
# Submit test case
|
||||
response = HTTP.post(
|
||||
"http://localhost:$port/submit",
|
||||
["Content-Type" => "application/gzip"],
|
||||
compressed_data
|
||||
)
|
||||
@test response.status == 200
|
||||
|
||||
# Check response
|
||||
response_data = JSON.parse(String(response.body))
|
||||
@test haskey(response_data, "job_id")
|
||||
job_id = response_data["job_id"]
|
||||
@test length(job_id) == 16
|
||||
@test all(c -> c in ['a':'z'; '0':'9'], collect(job_id))
|
||||
|
||||
# Verify the compressed file was saved correctly
|
||||
job_dir = joinpath(Backend.basedir, "jobs", job_id)
|
||||
saved_path = joinpath(job_dir, "input.json.gz")
|
||||
@test isfile(saved_path)
|
||||
saved_data = read(saved_path)
|
||||
@test saved_data == compressed_data
|
||||
|
||||
response = HTTP.get("http://localhost:$port/jobs/123/view")
|
||||
@test response.status == 200
|
||||
@test String(response.body) == "OK"
|
||||
|
||||
# Clean up: remove the job directory
|
||||
# rm(job_dir, recursive=true)
|
||||
end
|
||||
return "$BASEDIR/../fixtures/$path"
|
||||
end
|
||||
|
||||
function runtests()
|
||||
@testset "UCJL Backend" begin
|
||||
test_usage()
|
||||
server_test_usage()
|
||||
# jobs_test_usage()
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
function format()
|
||||
JuliaFormatter.format(basedir, verbose = true)
|
||||
JuliaFormatter.format("$basedir/../../src", verbose = true)
|
||||
JuliaFormatter.format(BASEDIR, verbose = true)
|
||||
JuliaFormatter.format("$BASEDIR/../../src", verbose = true)
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
43
web/backend/test/src/jobs_test.jl
Normal file
43
web/backend/test/src/jobs_test.jl
Normal file
@@ -0,0 +1,43 @@
|
||||
# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment
|
||||
# Copyright (C) 2025, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
using Backend
|
||||
using Test
|
||||
using HiGHS
|
||||
|
||||
function jobs_test_usage()
|
||||
@testset "JobProcessor" begin
|
||||
# Setup job directory
|
||||
job_id = "qwe123"
|
||||
job_dir = joinpath(Backend.basedir, "jobs", job_id)
|
||||
mkpath(job_dir)
|
||||
cp(fixture("case14.json.gz"), joinpath(job_dir, "input.json.gz"))
|
||||
|
||||
try
|
||||
# Create processor with HiGHS optimizer
|
||||
processor = JobProcessor(optimizer = HiGHS.Optimizer)
|
||||
|
||||
# Start the worker
|
||||
start(processor)
|
||||
|
||||
# Push job to queue
|
||||
put!(processor, job_id)
|
||||
|
||||
# Wait until all jobs are processed
|
||||
while isbusy(processor)
|
||||
sleep(0.1)
|
||||
end
|
||||
|
||||
# Check that solution file exists
|
||||
output_path = joinpath(job_dir, "output.json")
|
||||
@test isfile(output_path)
|
||||
|
||||
# Stop the worker
|
||||
stop(processor)
|
||||
finally
|
||||
# Cleanup
|
||||
rm(job_dir, recursive = true, force = true)
|
||||
end
|
||||
end
|
||||
end
|
||||
44
web/backend/test/src/server_test.jl
Normal file
44
web/backend/test/src/server_test.jl
Normal file
@@ -0,0 +1,44 @@
|
||||
# UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment
|
||||
# Copyright (C) 2025, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
const PORT = 32617
|
||||
|
||||
function server_test_usage()
|
||||
server = Backend.start_server(PORT; optimizer = HiGHS.Optimizer)
|
||||
|
||||
# Read the compressed fixture file
|
||||
compressed_data = read(fixture("case14.json.gz"))
|
||||
|
||||
# Submit test case
|
||||
response = HTTP.post(
|
||||
"http://localhost:$PORT/submit",
|
||||
["Content-Type" => "application/gzip"],
|
||||
compressed_data,
|
||||
)
|
||||
@test response.status == 200
|
||||
|
||||
# Check response
|
||||
response_data = JSON.parse(String(response.body))
|
||||
@test haskey(response_data, "job_id")
|
||||
job_id = response_data["job_id"]
|
||||
@test length(job_id) == 16
|
||||
|
||||
# Wait for jobs to finish and stop server
|
||||
sleep(0.1)
|
||||
stop(server)
|
||||
|
||||
# Verify the compressed file was saved correctly
|
||||
job_dir = joinpath(Backend.basedir, "jobs", job_id)
|
||||
saved_input_path = joinpath(job_dir, "input.json.gz")
|
||||
saved_log_path = joinpath(job_dir, "output.log")
|
||||
saved_output_path = joinpath(job_dir, "output.json")
|
||||
@test isfile(saved_input_path)
|
||||
@test isfile(saved_log_path)
|
||||
@test isfile(saved_output_path)
|
||||
saved_data = read(saved_input_path)
|
||||
@test saved_data == compressed_data
|
||||
|
||||
# Clean up: remove the job directory
|
||||
# rm(job_dir, recursive=true)
|
||||
end
|
||||
Reference in New Issue
Block a user