clean up status handling

This commit is contained in:
andreas 2023-10-06 18:43:18 +02:00
parent 35eb5417af
commit 1da9fea9a4
1 changed files with 102 additions and 97 deletions

View File

@ -1,6 +1,41 @@
import { addEl, setButtons,fillValues, setValue, buildUrl, fetchJson, setVisible, enableEl, setValues, getParam, fillSelect, forEachEl, readFile } from "./helper.js"; import { addEl, setButtons,fillValues, setValue, buildUrl, fetchJson, setVisible, enableEl, setValues, getParam, fillSelect, forEachEl, readFile } from "./helper.js";
import {load as yamlLoad} from "https://cdn.skypack.dev/js-yaml@4.1.0"; import {load as yamlLoad} from "https://cdn.skypack.dev/js-yaml@4.1.0";
import fileDownload from "https://cdn.skypack.dev/js-file-download@0.4.12" import fileDownload from "https://cdn.skypack.dev/js-file-download@0.4.12"
class PipelineInfo{
constructor(id){
this.STFIELDS=['status','error','status_url'];
this.reset(id);
}
update(state){
if (state.pipeline_id !== undefined && state.pipeline_id !== this.id){
return false;
}
this.STFIELDS.forEach((i)=>{
let v=state[i];
if (v !== undefined)this[i]=v;
});
}
reset(id,opt_state){
this.id=id;
this.STFIELDS.forEach((i)=>this[i]=undefined);
this.downloadUrl=undefined;
if (opt_state) {
this.update(opt_state);
}
else{
if (id !== undefined) this.status='fetching';
}
}
valid(){
return this.id !== undefined;
}
isRunning(){
if (! this.valid()) return false;
if (this.status === undefined) return false;
return ['error','success','canceled'].indexOf(this.status) < 0;
}
}
(function(){ (function(){
const STATUS_INTERVAL=2000; const STATUS_INTERVAL=2000;
const CURRENT_PIPELINE='pipeline'; const CURRENT_PIPELINE='pipeline';
@ -8,16 +43,13 @@ import fileDownload from "https://cdn.skypack.dev/js-file-download@0.4.12"
const GITAPI="install.php"; const GITAPI="install.php";
const GITUSER="wellenvogel"; const GITUSER="wellenvogel";
const GITREPO="esp32-nmea2000"; const GITREPO="esp32-nmea2000";
let currentPipeline=undefined; let currentPipeline=new PipelineInfo();
let downloadUrl=undefined;
let timer=undefined; let timer=undefined;
let structure=undefined; let structure=undefined;
let config={}; //values as read and stored let config={}; //values as read and stored
let configStruct={}; //complete struct merged of config and struct let configStruct={}; //complete struct merged of config and struct
let displayMode='last'; let displayMode='last';
let delayedSearch=undefined; let delayedSearch=undefined;
let running=false;
let pipelineState=undefined;
let gitSha=undefined; let gitSha=undefined;
const modeStrings={ const modeStrings={
last: 'Last Build', last: 'Last Build',
@ -34,37 +66,22 @@ import fileDownload from "https://cdn.skypack.dev/js-file-download@0.4.12"
setValue('resultTitle',ms); setValue('resultTitle',ms);
return mode !== old; return mode !== old;
} }
const showError=(text)=>{ const updateStatus=()=>{
if (text === undefined){ setValues(currentPipeline,{
setVisible('buildError',false,true); id: 'pipeline'
return; });
} setVisible('download',currentPipeline.valid() && currentPipeline.downloadUrl!==undefined,true);
setValue('buildError',text); setVisible('status_url',currentPipeline.valid() && currentPipeline.status_url!==undefined,true);
setVisible('buildError',true,true); setVisible('error',currentPipeline.error!==undefined,true);
}
const hideResults = () => {
downloadUrl = undefined;
currentPipeline = undefined;
setValue('pipeline', currentPipeline);
setValue('status','');
showError();
setVisible('download', false, true);
setVisible('status_url', false, true);
}
const updateStart=()=>{
if (running) {
enableEl('start',false);
return;
}
let e=document.getElementById('configError'); let e=document.getElementById('configError');
if (e.textContent) { if (e.textContent) {
enableEl('start',false); enableEl('start',false);
return; return;
} }
if (displayMode != 'existing'){ if (displayMode != 'existing'){
if (currentPipeline !== undefined){ if (currentPipeline.valid()){
//check pipeline state //check pipeline state
if (['error','success'].indexOf(pipelineState) >= 0){ if (['error','success','canceled'].indexOf(currentPipeline.status) >= 0){
enableEl('start',true); enableEl('start',true);
return; return;
} }
@ -72,71 +89,51 @@ import fileDownload from "https://cdn.skypack.dev/js-file-download@0.4.12"
return; return;
} }
enableEl('start',true); enableEl('start',true);
return;
} }
//display mnode existing //display node existing
enableEl('start',currentPipeline === undefined); enableEl('start',!currentPipeline.valid());
}
const setRunning=(active)=>{
running=active;
if (active){
showError();
downloadUrl=undefined;
setVisible('download', false, true);
setVisible('status_url', false, true);
}
updateStart();
} }
const isRunning=()=>{ const isRunning=()=>{
return running; return currentPipeline.isRunning();
} }
const fetchStatus=(initial)=>{ const fetchStatus=(initial)=>{
if (currentPipeline === undefined) { if (! currentPipeline.valid()){
setVisible('status_url',false,true); updateStatus();
setVisible('error',false);
setVisible('download',false,true);
setValue('status','---');
updateStart();
return; return;
}; }
let queryPipeline=currentPipeline; let queryPipeline=currentPipeline.id;
fetchJson(API,{api:'status',pipeline:currentPipeline}) fetchJson(API,{api:'status',pipeline:currentPipeline.id})
.then((st)=>{ .then((st)=>{
if (queryPipeline !== currentPipeline) return; if (queryPipeline !== currentPipeline.id) return;
if (currentPipeline.id !== st.pipeline_id) return;
if (st.status === undefined) st.status=st.state; if (st.status === undefined) st.status=st.state;
pipelineState=st.status; currentPipeline.update(st);
setValues(st); updateStatus();
setVisible('status_url',st.status_url !== undefined,true); if (st.status === 'error' || st.status === 'errored' || st.status === 'canceled'){
setVisible('error',st.error !== undefined,true);
if (st.status === 'error' || st.status === 'errored'){
setRunning(false);
setVisible('download',false,true);
updateStart();
return; return;
} }
if (st.status === 'success'){ if (st.status === 'success'){
setRunning(false); fetchJson(API,{api:'artifacts',pipeline:currentPipeline.id})
updateStart();
fetchJson(API,{api:'artifacts',pipeline:currentPipeline})
.then((ar)=>{ .then((ar)=>{
if (! ar.items || ar.items.length < 1){ if (! ar.items || ar.items.length < 1){
throw new Error("no download link"); throw new Error("no download link");
} }
downloadUrl=buildUrl(API,{ currentPipeline.downloadUrl=buildUrl(API,{
download: currentPipeline download: currentPipeline.id
}); });
setVisible('download',true,true); updateStatus();
}) })
.catch((err)=>{ .catch((err)=>{
showError("Unable to get build result: "+err); currentPipeline.update({
setVisible('download',false,true); status:'error',
error:"Unable to get build result: "+err
});
updateStatus();
}); });
return; return;
} }
else{
setVisible('download',false,true);
}
updateStart();
timer=window.setTimeout(fetchStatus,STATUS_INTERVAL) timer=window.setTimeout(fetchStatus,STATUS_INTERVAL)
}) })
.catch((e)=>{ .catch((e)=>{
@ -144,51 +141,54 @@ import fileDownload from "https://cdn.skypack.dev/js-file-download@0.4.12"
}) })
} }
const setCurrentPipeline=(pipeline,doStore)=>{ const setCurrentPipeline=(pipeline,doStore)=>{
currentPipeline=pipeline; currentPipeline.reset(pipeline);
setValue('pipeline',currentPipeline);
if (doStore) window.localStorage.setItem(CURRENT_PIPELINE,pipeline); if (doStore) window.localStorage.setItem(CURRENT_PIPELINE,pipeline);
}; };
const startBuild=()=>{ const startBuild=()=>{
let param={}; let param={};
currentPipeline=undefined; currentPipeline.reset(undefined,{status:'requested'});
if (timer) window.clearTimeout(timer); if (timer) window.clearTimeout(timer);
timer=undefined; timer=undefined;
fillValues(param,['environment','buildflags']); fillValues(param,['environment','buildflags']);
setValue('status','requested'); setDisplayMode('current');
setValue('pipeline',''); updateStatus();
setRunning(true);
updateStart();
if (gitSha !== undefined) param.tag=gitSha; if (gitSha !== undefined) param.tag=gitSha;
param.config=JSON.stringify(config); param.config=JSON.stringify(config);
fetchJson(API,Object.assign({ fetchJson(API,Object.assign({
api:'start'},param)) api:'start'},param))
.then((json)=>{ .then((json)=>{
if (json.status === 'error'){ let status=json.status || 'error';
if (status === 'error'){
currentPipeline.update({status:status,error:json.error})
updateStatus();
throw new Error("unable to create job "+(json.error||'')); throw new Error("unable to create job "+(json.error||''));
} }
if (!json.id) throw new Error("unable to create job, no id"); if (!json.id) {
let error="unable to create job, no id"
currentPipeline.update({status:'error',error:error});
updateStatus();
throw new Error(error);
}
setCurrentPipeline(json.id,true); setCurrentPipeline(json.id,true);
setValue('status',json.status); updateStatus();
setDisplayMode('current');
timer=window.setTimeout(fetchStatus,STATUS_INTERVAL); timer=window.setTimeout(fetchStatus,STATUS_INTERVAL);
}) })
.catch((err)=>{ .catch((err)=>{
setRunning(false); currentPipeline.update({status:'error',error:err});
setValue('status','error'); updateStatus();
showError(err);
}); });
} }
const runDownload=()=>{ const runDownload=()=>{
if (! downloadUrl) return; if (! currentPipeline.downloadUrl) return;
let df=document.getElementById('dlframe'); let df=document.getElementById('dlframe');
if (df){ if (df){
df.setAttribute('src',null); df.setAttribute('src',null);
df.setAttribute('src',downloadUrl); df.setAttribute('src',currentPipeline.downloadUrl);
} }
} }
const webInstall=()=>{ const webInstall=()=>{
if (! downloadUrl) return; if (! currentPipeline.downloadUrl) return;
let url=buildUrl("install.html",{custom:downloadUrl}); let url=buildUrl("install.html",{custom:currentPipeline.downloadUrl});
window.location.href=url; window.location.href=url;
} }
const uploadConfig=()=>{ const uploadConfig=()=>{
@ -387,7 +387,7 @@ import fileDownload from "https://cdn.skypack.dev/js-file-download@0.4.12"
setVisible('configError',false,true); setVisible('configError',false,true);
} }
if (! initial) findPipeline(); if (! initial) findPipeline();
updateStart(); updateStatus();
} }
let findIdx=0; let findIdx=0;
const findPipeline=()=>{ const findPipeline=()=>{
@ -408,23 +408,28 @@ import fileDownload from "https://cdn.skypack.dev/js-file-download@0.4.12"
.then((res)=>{ .then((res)=>{
if (queryIdx != findIdx) return; if (queryIdx != findIdx) return;
setCurrentPipeline(res.pipeline); setCurrentPipeline(res.pipeline);
updateStart(); if (res.pipeline) currentPipeline.status="found";
setDisplayMode('existing'); setDisplayMode('existing');
updateStatus();
fetchStatus(true); fetchStatus(true);
}) })
.catch((e)=>console.log("findPipeline error ",e)); .catch((e)=>{
console.log("findPipeline error ",e)
if (displayMode == 'existing'){
setCurrentPipeline();
updateStatus();
}
});
} }
window.onload=async ()=>{ window.onload=async ()=>{
setButtons(btConfig); setButtons(btConfig);
forEachEl('#environment',(el)=>el.addEventListener('change',hideResults));
forEachEl('#buildflags',(el)=>el.addEventListener('change',hideResults));
let pipeline=window.localStorage.getItem(CURRENT_PIPELINE); let pipeline=window.localStorage.getItem(CURRENT_PIPELINE);
setDisplayMode('last'); setDisplayMode('last');
if (pipeline){ if (pipeline){
setCurrentPipeline(pipeline); setCurrentPipeline(pipeline);
updateStatus();
fetchStatus(true); fetchStatus(true);
setRunning(true);
} }
let gitParam={user:GITUSER,repo:GITREPO}; let gitParam={user:GITUSER,repo:GITREPO};
let branch=getParam('branch'); let branch=getParam('branch');
@ -491,7 +496,7 @@ import fileDownload from "https://cdn.skypack.dev/js-file-download@0.4.12"
structure=await loadConfig("build.yaml"); structure=await loadConfig("build.yaml");
} }
buildSelectors(ROOT_PATH,structure.config.children,true); buildSelectors(ROOT_PATH,structure.config.children,true);
if (! running) findPipeline(); if (! isRunning()) findPipeline();
updateStart(); updateStatus();
} }
})(); })();