Move to new db setup

This commit is contained in:
Ryan
2026-09-08 20:31:06 -04:00
parent 78358e0d7b
commit ea46d418bd
18 changed files with 468 additions and 2043 deletions
+27
View File
@@ -0,0 +1,27 @@
FROM ubuntu:22.04
# Install Perl and system dependencies
RUN apt update && apt install -y \
dnsutils \
libdancer2-perl \
libdancer2-plugin-database-perl \
libdancer-plugin-database-core-perl \
libdbd-mysql-perl \
libhttp-lite-perl \
libjson-perl \
libspreadsheet-parsexlsx-perl \
libsql-splitstatement-perl \
libyaml-perl \
perl \
libdbi-perl \
build-essential \
ffmpeg
EXPOSE 3000
RUN mkdir -p /app
WORKDIR /app
COPY . /app
# Simple placeholder command
CMD ["perl", "app.pl"]
+157
View File
@@ -0,0 +1,157 @@
#!/usr/bin/perl
use strict;
use Dancer2;
use Dancer2::Plugin::Database;
use Data::Dumper;
use SQL::SplitStatement;
use HTTP::Lite;
$SIG{'INT'} = sub { exit; };
hook before => sub {
my $origin = request_header('Origin') || 'http://localhost:8890';
my $method = request_header('Access-Control-Request-Method') || '';
my $headers = request_header('Access-Control-Request-Headers') || '';
response_header 'Access-Control-Allow-Origin' => $origin;
response_header 'Access-Control-Allow-Methods' => $method;
response_header 'Access-Control-Allow-Headers' => $headers;
};
options qr{/api/v1/.+} => sub {
return {};
};
get '/api/v1/hello' => sub {
my $user = session('user') || 'world';
return {'hello' => $user };
};
get '/api/v1/videos' => sub {
my $sth = database->prepare("SELECT id, file_path FROM videos ORDER BY id");
$sth->execute();
my $ref = $sth->fetchall_hashref('id');
$sth->finish();
return $ref;
};
get '/api/v1/video' => sub {
my $path = query_parameters->get("path");
my $sth = database->prepare("SELECT * FROM videos WHERE file_path=?");
$sth->execute($path);
my $ref = $sth->fetchrow_hashref();
$sth->finish();
unless(ref $ref) {
send_error("No video", 404);
}
return $ref;
}
get '/api/v1/video/:video' => sub {
my $video = route_parameters->get("video");
my $sth = database->prepare("SELECT * FROM videos WHERE id=?");
$sth->execute($video);
my $ref = $sth->fetchrow_hashref();
$sth->finish();
unless(ref $ref) {
send_error("No video", 404);
}
return $ref;
};
post '/api/v1/video' => sub {
my $file_path = body_parameters->get("file_path");
my $file_hash = body_parameters->get("file_hash") || undef;
my $resolution_w = body_parameters->get("resolution_w");
my $resolution_h = body_parameters->get("resolution_h");
my $codec = body_parameters->get("codec");
my $sth = database->prepare("INSERT INTO videos (file_path, file_hash, resolution_w, resolution_h, codec) VALUES (?, ?, ?, ?, ?)");
$sth->execute($file_path, $file_hash, $resolution_w, $resolution_h, $codec);
my $video_id = database->last_insert_id(undef, undef, 'videos', undef);
$sth->finish();
return { id => $video_id };
};
get '/api/v1/tasks' => sub {
my $status = query_parameters->get("status") || 'PENDING';
my $sth = database->prepare("SELECT id, video_id, status FROM tasks WHERE status=? ORDER BY id");
$sth->execute($status);
my $ref = $sth->fetchall_hashref('id');
$sth->finish();
return $ref;
};
get '/api/v1/task/:task' => sub {
my $task = route_parameters->get("task");
my $sth = database->prepare("SELECT * FROM tasks WHERE id=?");
$sth->execute($task);
my $ref = $sth->fetchrow_hashref();
$sth->finish();
unless(ref $ref) {
send_error("No task", 404);
}
return $ref;
};
get '/api/v1/nexttask/:type' => sub {
my $type = route_parameters->get("type");
my $sth = database->prepare("SELECT * FROM tasks WHERE task_type=? AND status='PENDING' ORDER BY id LIMIT 1");
$sth->execute($type);
my $ref = $sth->fetchrow_hashref();
$sth->finish();
unless(ref $ref) {
send_error("No task", 404);
}
# Mark the task as in progress and assign it to a worker
my $assign_key = "worker_" . int(rand(1000)); # Example assign key, you can customize this
my $update_sth = database->prepare("UPDATE tasks SET status='IN_PROGRESS', assign_key=?, assigned_at=NOW() WHERE id=? AND status='PENDING'");
$update_sth->execute($assign_key, $ref->{id});
if($update_sth->rows == 0) {
send_error("Failed to claim task", 409);
}
$update_sth->finish();
return { task => $ref, assign_key => $assign_key };
};
post '/api/v1/task/:task/complete' => sub {
my $task = route_parameters->get("task");
my $assign_key = body_parameters->get("assign_key");
my $results = body_parameters->get("results");
# Verify the task is assigned to the worker
my $sth = database->prepare("SELECT * FROM tasks WHERE id=? AND assign_key=? AND status='IN_PROGRESS'");
$sth->execute($task, $assign_key);
my $ref = $sth->fetchrow_hashref();
$sth->finish();
unless(ref $ref) {
send_error("Task not assigned to this worker or not in progress", 403);
}
# Update the task as completed
my $update_sth = database->prepare("UPDATE tasks SET status='COMPLETED', results=?, updated_at=NOW() WHERE id=?");
$update_sth->execute($results, $task);
if($update_sth->rows == 0) {
send_error("Failed to complete task", 500);
}
$update_sth->finish();
return { message => "Task completed successfully" };
};
start();
+39
View File
@@ -0,0 +1,39 @@
serializer: JSON
logger: console
engines:
serializer:
JSON:
allow_nonref: 1
allow_blessed: 1
pretty: 1
plugins:
Database:
driver: 'mysql'
database: 'videodetect'
host: 'mariadb'
port: 3306
username: 'videodetect'
password: 'videodetect123'
connection_check_threshold: 10
dbi_params:
RaiseError: 1
AutoCommit: 1
on_connect_do: ["SET NAMES 'utf8'", "SET CHARACTER SET 'utf8'" ]
log_queries: 1
server:
port: 5000
workers: 4
bind: 0.0.0.0
logging:
level: info
logdir: /var/log/videodetect/api
logfile: videodetect-api.log
format: "[%d] %l [%P] %m"
paths:
scratch: /scratch
input: /data/input
output: /data/output
models: /models