Skip to content
Snippets Groups Projects

North api

Compare and
9 files
+ 396
382
Compare changes
  • Side-by-side
  • Inline

Files

+ 25
67
@@ -36,8 +36,8 @@ export function useNorthTool() {
@@ -36,8 +36,8 @@ export function useNorthTool() {
}
}
const launchButtonLabels = {
const launchButtonLabels = {
'idle': 'Launch',
'stopped': 'Launch',
'launching': 'Launching...',
'starting': 'Launching...',
'running': 'Open',
'running': 'Open',
'stopping': 'Launch'
'stopping': 'Launch'
}
}
@@ -55,7 +55,7 @@ export const NorthToolButtons = React.memo(function NorthToolButton() {
@@ -55,7 +55,7 @@ export const NorthToolButtons = React.memo(function NorthToolButton() {
const {name, launch, stop, state} = useNorthTool()
const {name, launch, stop, state} = useNorthTool()
return (
return (
<Box display="flex" flexDirection="row">
<Box display="flex" flexDirection="row">
<LaunchButton fullWidth name={name} onClick={launch} disabled={state === 'stopping' || state === 'launching' || !state}>
<LaunchButton fullWidth name={name} onClick={launch} disabled={state === 'stopping' || state === 'starting' || !state}>
{launchButtonLabels[state] || 'not available'}
{launchButtonLabels[state] || 'not available'}
</LaunchButton>
</LaunchButton>
{(state === 'running' || state === 'stopping') && (
{(state === 'running' || state === 'stopping') && (
@@ -90,55 +90,19 @@ const useStyles = makeStyles(theme => ({
@@ -90,55 +90,19 @@ const useStyles = makeStyles(theme => ({
}))
}))
const NorthTool = React.memo(function NorthTool({tool, uploadId, path, children}) {
const NorthTool = React.memo(function NorthTool({tool, uploadId, path, children}) {
const {name, title, version, description, short_description, path_prefix, icon} = tool
const {name, title, version, description, short_description, icon} = tool
const styles = useStyles()
const styles = useStyles()
const {northApi, user} = useApi()
const {api} = useApi()
const {raiseError} = useErrors()
const {raiseError} = useErrors()
const [state, setState] = useState()
const [state, setState] = useState('stopped')
const toolUrl = useMemo(() => {
if (!user) {
return null
}
let toolPath = ''
if (path_prefix) {
toolPath += `/${path_prefix}`
}
if (uploadId) {
toolPath += `/uploads/${uploadId}`
}
if (path) {
toolPath += `/${path}`
}
const toolUrl = `${northBase}/user/${user.preferred_username}/${name}${toolPath}`
return toolUrl
}, [user, name, path_prefix, uploadId, path])
const getToolStatus = useCallback(() => {
const getToolStatus = useCallback(() => {
if (northApi === null) {
return api.get(`north/${name}`)
return
.then(response => {
}
return response.data.state
return northApi.get(`servers/${name}/progress`)
}).catch(raiseError)
.then((response) => {
}, [api, raiseError, name])
const data = JSON.parse(response.data.substr(6))
if (data.ready) {
return 'running'
} else {
return 'launching'
}
})
.catch(error => {
if (error?.response?.status === 404 || error?.response?.status === 400) {
return 'idle'
} else if (error.code === 'ERR_NETWORK') {
// north is unavailable
return undefined
} else {
raiseError(error)
}
})
}, [northApi, raiseError, name])
useEffect(() => {
useEffect(() => {
const toolStatus = getToolStatus()
const toolStatus = getToolStatus()
@@ -151,34 +115,28 @@ const NorthTool = React.memo(function NorthTool({tool, uploadId, path, children}
@@ -151,34 +115,28 @@ const NorthTool = React.memo(function NorthTool({tool, uploadId, path, children}
const launch = useCallback(() => {
const launch = useCallback(() => {
// We get the current actual tools status and do not use the one used to display the status!
// We get the current actual tools status and do not use the one used to display the status!
getToolStatus().then((toolStatus) => {
setState('starting')
if (toolStatus === 'running') {
api.post(`north/${name}`)
 
.then((response) => {
 
const toolUrl = `${northBase}/${response.data.upload_urls[uploadId]}/${path}`
window.open(toolUrl, name)
window.open(toolUrl, name)
setState(toolStatus)
setState(response.data.state)
} else {
})
setState('launching')
.catch(errors => {
northApi.post(`servers/${name}`)
raiseError(errors)
.then((response) => {
setState('stopped')
window.open(toolUrl, name)
})
setState('running')
}, [setState, api, raiseError, uploadId, path, name])
})
.catch(errors => {
raiseError(errors)
setState('idle')
})
}
})
}, [setState, northApi, raiseError, name, toolUrl, getToolStatus])
const stop = useCallback(() => {
const stop = useCallback(() => {
setState('stopping')
setState('stopping')
northApi.delete(`servers/${name}`)
api.delete(`north/${name}`)
.then((response) => {
.then((response) => {
console.log(response)
console.log(response)
setState('idle')
setState('stopped')
})
})
.catch(raiseError)
.catch(raiseError)
}, [northApi, raiseError, setState, name])
}, [api, raiseError, setState, name])
const value = useMemo(() => ({
const value = useMemo(() => ({
state: state,
state: state,
Loading