Skip to content

Commit

Permalink
pulling build in queue
Browse files Browse the repository at this point in the history
  • Loading branch information
melezhik committed Jan 10, 2024
1 parent cb62b4e commit ff7ddbc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
40 changes: 38 additions & 2 deletions bin/sparky-web.raku
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,15 @@ sub create-cro-app ($pool) {
$dbh.dispose;

if $state.defined {
#content 'text/plain', "$state"
if $state == -1 or $state == 1 {
say "ws: done - job has finsihed - state: [$state]";
$done = True;
last();
}
} else {
say "ws: done - job not found";
$done = True;
last();
#not-found();
}
}

Expand Down Expand Up @@ -518,6 +517,41 @@ sub create-cro-app ($pool) {
}
}

get -> 'livestatus', $project, $key {

web-socket -> $incoming {
supply {
whenever $incoming -> $message {
my $done = False;
while True {
if trigger-exists($root,$project,$key) {
emit "[{DateTime.now}] - build in queue";
sleep(1);
} else {
my $dbh = $pool ?? $pool.get-connection() !! get-dbh();
my $sth = $dbh.prepare("SELECT state, id FROM builds where project = '{$project}' and job_id = '{$key}'");
$sth.execute();
my @r = $sth.allrows(:array-of-hash);
my $build_id = @r[0]<id>;
$sth.finish;
$dbh.dispose;
if $build_id {
say "ws - build has started, build_id: {$build_id}";
emit "[{DateTime.now}] - <a href=\"{sparky-http-root()}/report/{$project}/{$build_id}\">build_id: {$build_id} has started</a>";
$done = True;
last();
}
}
}
if $done {
# emit "[{DateTime.now}] ---";
done
}
}
}
}
}

get -> 'report', 'raw', $project, $key {

if trigger-exists($root,$project,$key) {
Expand Down Expand Up @@ -641,6 +675,8 @@ sub create-cro-app ($pool) {

template 'templates/build.crotmp', {
http-root => sparky-http-root(),
sparky-host => "10.7.98.245", # fix me
sparky-tcp-port => sparky-tcp-port(),
css =>css(),
navbar => navbar(),
project => $project,
Expand Down
48 changes: 43 additions & 5 deletions templates/build.crotmp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
<&HTML-AND-JAVASCRIPT(.navbar)>
<div class="panel is-primary">
<div class="panel-heading">
<h3 class="panel-title" id="notification">
<h3 class="panel-title">
Build: <.project>
</h3>
</div>
<div class="panel-block">
<form method="post">
<div class="field" id="notification">
</div>
<div class="field">
<@vars: $i>
<div class="control">
Expand Down Expand Up @@ -58,19 +60,49 @@
</div>
<script>

var log = document.getElementById('notification');

function connect(job_id) {
var ws = new WebSocket(`ws://<.sparky-host>:<.sparky-tcp-port>/livestatus/<.project>/${job_id}`);
ws.onopen = function() {
console.log('ws - socket is open');
// subscribe to some channels
ws.send("Меня зовут Джон");
};

ws.onmessage = function(e) {
console.log('ws - ', e.data);
log.innerHTML = e.data;
// log.innerHTML += "<br>";
};

ws.onclose = function(e) {
console.log('ws - socket is closed', e.reason);
//setTimeout(function() {
// connect();
//}, 1000);
};

ws.onerror = function(err) {
console.error('ws - socket encountered error: ', err.message, 'Closing socket');
ws.close();
};
}


function handleSubmit(event) {
event.preventDefault();
const data = new FormData(event.target);
const form_json = Object.fromEntries(data.entries());
// alert(form_json)
var url = "<.http-root>/build-with-tags/project/<.project>";
const tags = []
const tags = [];
for (const t in form_json) {
const v = form_json[t]
console.log(`${t}: ${v}`);
tags.push(`${t}=${v}`)
}
const tags_s = tags.join(",");
var tags_s = tags.join(",");
// https://developer.mozilla.org/en-US/docs/Web/API/fetch
fetch(
url,
Expand All @@ -88,11 +120,17 @@
.then(function(response) {
console.log(response.status); // Will show you the status
if (!response.ok) {
document.getElementById('notification').innerHTML = `Build <.project> error: ${response.status} ${response.statusText}`;
// document.getElementById('notification').innerHTML += "<br>";
document.getElementById('notification').innerHTML = `Build error: ${response.status} ${response.statusText}`;
} else {
document.getElementById('notification').innerHTML = 'Build <.project>: queued';
return response.text();
}
})
.then(function(job_id) {
// document.getElementById('notification').innerHTML += "<br>";
document.getElementById('notification').innerHTML = `Build queued: ${job_id}`;
connect(job_id);
})
}

const form = document.querySelector('form');
Expand Down

0 comments on commit ff7ddbc

Please sign in to comment.