socket: Explicitly communicate success
This commit updates the socket process to explicitly communicate the process (and any potential error) on the server using a binary message.
This commit is contained in:
parent
53f63d4efd
commit
746ebcd9e3
12 changed files with 111 additions and 31 deletions
2
go.mod
2
go.mod
|
|
@ -9,6 +9,7 @@ require (
|
|||
github.com/go-sql-driver/mysql v1.7.0
|
||||
github.com/gorilla/csrf v1.7.1
|
||||
github.com/gorilla/sessions v1.2.1
|
||||
github.com/gorilla/websocket v1.5.0
|
||||
github.com/julienschmidt/httprouter v1.3.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/pquerna/otp v1.4.0
|
||||
|
|
@ -32,7 +33,6 @@ require (
|
|||
github.com/boombuler/barcode v1.0.1 // indirect
|
||||
github.com/feiin/sqlstring v0.3.0 // indirect
|
||||
github.com/gorilla/securecookie v1.1.1 // indirect
|
||||
github.com/gorilla/websocket v1.5.0 // indirect
|
||||
github.com/gosuri/uilive v0.0.4 // indirect
|
||||
github.com/jessevdk/go-flags v1.5.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package cli
|
||||
|
||||
// ===========================================================================================================
|
||||
// This file was generated automatically at 27-02-2023 10:11:35 using gogenlicense.
|
||||
// This file was generated automatically at 28-02-2023 00:03:02 using gogenlicense.
|
||||
// Do not edit manually, as changes may be overwritten.
|
||||
// ===========================================================================================================
|
||||
|
||||
|
|
@ -2452,7 +2452,7 @@ package cli
|
|||
// # Generation
|
||||
//
|
||||
// This variable and the associated documentation have been automatically generated using the 'gogenlicense' tool.
|
||||
// It was last updated at 27-02-2023 10:11:35.
|
||||
// It was last updated at 28-02-2023 00:03:02.
|
||||
var LegalNotices string
|
||||
|
||||
func init() {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@ package socket
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
|
|
@ -12,6 +13,7 @@ import (
|
|||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances/purger"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/provision"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/tkw1536/goprogram/status"
|
||||
"github.com/tkw1536/pkglib/httpx"
|
||||
)
|
||||
|
|
@ -51,20 +53,52 @@ func (socket *Sockets) Serve(conn httpx.WebSocketConnection) {
|
|||
|
||||
var instanceParamsTimeout = time.Second
|
||||
|
||||
func (socket *Sockets) Handle(conn httpx.WebSocketConnection, action SocketAction) {
|
||||
type actionResult struct {
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (*Sockets) reportErrorToClient(conn httpx.WebSocketConnection, err error) {
|
||||
// create an action result
|
||||
var result actionResult
|
||||
if err == nil {
|
||||
result.Success = true
|
||||
} else {
|
||||
result.Success = false
|
||||
result.Error = err.Error()
|
||||
}
|
||||
|
||||
// marshal the result, ignoring any error silently
|
||||
data, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// and send it as a binary message to the client
|
||||
<-conn.Write(httpx.WebSocketMessage{Type: websocket.BinaryMessage, Bytes: data})
|
||||
}
|
||||
|
||||
var errInsufficientParams = errors.New("insufficient parameters")
|
||||
var errParameterTimeout = errors.New("timed out reading parameters")
|
||||
|
||||
func (socket *Sockets) Handle(conn httpx.WebSocketConnection, action SocketAction) (err error) {
|
||||
// report the error to the client
|
||||
defer func() {
|
||||
// NOTE: the closure is needed here!
|
||||
socket.reportErrorToClient(conn, err)
|
||||
}()
|
||||
|
||||
// read the parameters
|
||||
params := make([]string, action.NumParams)
|
||||
for i := range params {
|
||||
select {
|
||||
case message, ok := <-conn.Read():
|
||||
if !ok {
|
||||
<-conn.WriteText("Insufficient parameters")
|
||||
return
|
||||
return errInsufficientParams
|
||||
}
|
||||
params[i] = string(message.Bytes)
|
||||
case <-time.After(instanceParamsTimeout):
|
||||
<-conn.WriteText("Timed out reading parameters")
|
||||
return
|
||||
return errParameterTimeout
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -78,14 +112,7 @@ func (socket *Sockets) Handle(conn httpx.WebSocketConnection, action SocketActio
|
|||
defer writer.Close()
|
||||
|
||||
// handle the interactive action
|
||||
if action.HandleInteractive != nil {
|
||||
err := action.HandleInteractive(conn.Context(), socket, writer, params...)
|
||||
if err != nil {
|
||||
fmt.Fprintln(writer, err)
|
||||
return
|
||||
}
|
||||
fmt.Fprintln(writer, "done")
|
||||
}
|
||||
return action.HandleInteractive(conn.Context(), socket, writer, params...)
|
||||
}
|
||||
|
||||
// IAction is like SocketAction, but takes the slug of an instance (runnning or not) as the first parameter
|
||||
|
|
|
|||
|
|
@ -317,6 +317,32 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|||
|
||||
-----
|
||||
|
||||
The following software may be included in this product: async-mutex. A copy of the source code may be downloaded from https://github.com/DirtyHairy/async-mutex. This software contains the following license and notice below:
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Christian Speckner <cnspeckn@googlemail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-----
|
||||
|
||||
The following software may be included in this product: base-x. A copy of the source code may be downloaded from https://github.com/cryptocoinjs/base-x.git. This software contains the following license and notice below:
|
||||
|
||||
The MIT License (MIT)
|
||||
|
|
|
|||
|
|
@ -24,12 +24,12 @@ var AssetsUser = Assets{
|
|||
|
||||
// AssetsAdmin contains assets for the 'Admin' entrypoint.
|
||||
var AssetsAdmin = Assets{
|
||||
Scripts: `<script nomodule="" defer src="/this-is-fine/User.b2f9a57c.js"></script><script type="module" src="/this-is-fine/User.e0367d79.js"></script><script type="module" src="/this-is-fine/Default.38d394c2.js"></script><script src="/this-is-fine/Default.38d394c2.js" nomodule="" defer></script><script type="module" src="/this-is-fine/Admin.620228db.js"></script><script src="/this-is-fine/Admin.939dcd95.js" nomodule="" defer></script>`,
|
||||
Scripts: `<script nomodule="" defer src="/this-is-fine/User.b2f9a57c.js"></script><script type="module" src="/this-is-fine/User.e0367d79.js"></script><script type="module" src="/this-is-fine/Default.38d394c2.js"></script><script src="/this-is-fine/Default.38d394c2.js" nomodule="" defer></script><script type="module" src="/this-is-fine/Admin.15462d3d.js"></script><script src="/this-is-fine/Admin.2889b18a.js" nomodule="" defer></script>`,
|
||||
Styles: `<link rel="stylesheet" href="/this-is-fine/Default.938b4407.css"><link rel="stylesheet" href="/this-is-fine/Admin.a1e05c23.css"><link rel="stylesheet" href="/this-is-fine/User.840de3b4.css"><link rel="stylesheet" href="/this-is-fine/User.68febbf8.css"><link rel="stylesheet" href="/this-is-fine/Admin.6d2ae968.css">`,
|
||||
}
|
||||
|
||||
// AssetsAdminProvision contains assets for the 'AdminProvision' entrypoint.
|
||||
var AssetsAdminProvision = Assets{
|
||||
Scripts: `<script nomodule="" defer src="/this-is-fine/User.b2f9a57c.js"></script><script nomodule="" defer src="/this-is-fine/Admin.939dcd95.js"></script><script type="module" src="/this-is-fine/User.e0367d79.js"></script><script type="module" src="/this-is-fine/Admin.620228db.js"></script><script type="module" src="/this-is-fine/Default.38d394c2.js"></script><script src="/this-is-fine/Default.38d394c2.js" nomodule="" defer></script><script type="module" src="/this-is-fine/AdminProvision.3cf9e19e.js"></script><script src="/this-is-fine/AdminProvision.d195fd59.js" nomodule="" defer></script>`,
|
||||
Scripts: `<script nomodule="" defer src="/this-is-fine/User.b2f9a57c.js"></script><script nomodule="" defer src="/this-is-fine/Admin.2889b18a.js"></script><script type="module" src="/this-is-fine/User.e0367d79.js"></script><script type="module" src="/this-is-fine/Admin.15462d3d.js"></script><script type="module" src="/this-is-fine/Default.38d394c2.js"></script><script src="/this-is-fine/Default.38d394c2.js" nomodule="" defer></script><script type="module" src="/this-is-fine/AdminProvision.3cf9e19e.js"></script><script src="/this-is-fine/AdminProvision.d195fd59.js" nomodule="" defer></script>`,
|
||||
Styles: `<link rel="stylesheet" href="/this-is-fine/Default.938b4407.css"><link rel="stylesheet" href="/this-is-fine/Admin.a1e05c23.css"><link rel="stylesheet" href="/this-is-fine/User.840de3b4.css"><link rel="stylesheet" href="/this-is-fine/User.68febbf8.css"><link rel="stylesheet" href="/this-is-fine/Admin.6d2ae968.css"><link rel="stylesheet" href="/this-is-fine/AdminProvision.38d394c2.css">`,
|
||||
}
|
||||
|
|
|
|||
1
internal/dis/component/server/assets/dist/Admin.15462d3d.js
vendored
Normal file
1
internal/dis/component/server/assets/dist/Admin.15462d3d.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
internal/dis/component/server/assets/dist/Admin.2889b18a.js
vendored
Normal file
1
internal/dis/component/server/assets/dist/Admin.2889b18a.js
vendored
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -4,6 +4,7 @@
|
|||
"license": "AGPL-3.0-only",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"async-mutex": "^0.4.0",
|
||||
"dayjs": "^1.11.5",
|
||||
"highlight.js": "^11.7.0",
|
||||
"latex.css": "^1.8.0",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import "./index.css"
|
||||
import connectSocket from './socket';
|
||||
import connectSocket from './socket'
|
||||
import { Mutex } from 'async-mutex'
|
||||
|
||||
type Println = ((line: string, flush?: boolean) => void) & {
|
||||
paintedFrames: number;
|
||||
|
|
@ -122,14 +123,14 @@ export function createModal(action: string, params: string[], opts: Partial<Moda
|
|||
const button = document.createElement("button")
|
||||
button.className = "pure-button pure-button-success"
|
||||
button.append(typeof opts?.onClose === 'function' ? "Close & Finish" : "Close")
|
||||
let success = false;
|
||||
let result = {success: false, error: "unknown error"};
|
||||
button.addEventListener('click', function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
if (typeof opts?.onClose === 'function') {
|
||||
button.setAttribute('disabled', 'disabled')
|
||||
target.innerHTML = 'Finishing up ...'
|
||||
opts.onClose(success)
|
||||
opts.onClose(result.success)
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -154,20 +155,38 @@ export function createModal(action: string, params: string[], opts: Partial<Moda
|
|||
|
||||
println("Connecting ...", true)
|
||||
|
||||
const mutex = new Mutex();
|
||||
|
||||
// connect to the socket and send the action
|
||||
connectSocket((socket) => {
|
||||
println("Connected", true)
|
||||
socket.send(action)
|
||||
params.forEach(p => socket.send(p))
|
||||
}, (data) => {
|
||||
mutex.runExclusive(async () => {
|
||||
if (data instanceof Blob) {
|
||||
result = JSON.parse(await data.text());
|
||||
return
|
||||
}
|
||||
|
||||
println(data);
|
||||
})
|
||||
}).then(() => {
|
||||
success = true
|
||||
mutex.runExclusive(async () => {
|
||||
if(result.success) {
|
||||
println("Process finished successfully. ")
|
||||
} else {
|
||||
println("Process failed: " + result.error)
|
||||
}
|
||||
println("Connection closed. ", true)
|
||||
close();
|
||||
})
|
||||
}).catch(() => {
|
||||
success = false
|
||||
mutex.runExclusive(async () => {
|
||||
println("Connection errored. ", true)
|
||||
result = { success: false, error: "connection errored" }
|
||||
|
||||
close();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
|
@ -751,6 +751,13 @@ ansi-styles@^4.1.0:
|
|||
dependencies:
|
||||
color-convert "^2.0.1"
|
||||
|
||||
async-mutex@^0.4.0:
|
||||
version "0.4.0"
|
||||
resolved "https://registry.yarnpkg.com/async-mutex/-/async-mutex-0.4.0.tgz#ae8048cd4d04ace94347507504b3cf15e631c25f"
|
||||
integrity sha512-eJFZ1YhRR8UN8eBLoNzcDPcy/jqjsg6I1AP+KvWQX80BqOSW1oJPJXDylPUEeMr2ZQvHgnQ//Lp6f3RQ1zI7HA==
|
||||
dependencies:
|
||||
tslib "^2.4.0"
|
||||
|
||||
base-x@^3.0.8:
|
||||
version "3.0.9"
|
||||
resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.9.tgz#6349aaabb58526332de9f60995e548a53fe21320"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue