Skip to content

Commit

Permalink
web background cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iignatevich committed Aug 20, 2024
1 parent fd59ec2 commit 80a3f47
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 36 deletions.
7 changes: 4 additions & 3 deletions client/src/utils/app-urls-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const isProductionMode = import.meta.env.MODE === 'production'
const addDefaultPort = () => ':433'
const addDefaultPort = (protocol: string) =>
protocol === 'https:' ? ':443' : ':80'

export function getApiUrl() {
let url = import.meta.env.VITE_API_URL
Expand All @@ -9,7 +10,7 @@ export function getApiUrl() {
url = location.origin

if (!location.port) {
url += addDefaultPort()
url += addDefaultPort(location.protocol)
}

url += '/api'
Expand All @@ -26,7 +27,7 @@ export function getWebSocketUrl() {
url = location.protocol === 'https:' ? 'wss://' : 'ws://'
url += location.host
if (!location.port) {
url += addDefaultPort()
url += addDefaultPort(location.protocol)
}

url += '/ws'
Expand Down
14 changes: 0 additions & 14 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6221,20 +6221,6 @@ __metadata:
languageName: node
linkType: hard

"dotenv-expand@npm:5.1.0":
version: 5.1.0
resolution: "dotenv-expand@npm:5.1.0"
checksum: 10c0/24ac633de853ef474d0421cc639328b7134109c8dc2baaa5e3afb7495af5e9237136d7e6971e55668e4dce915487eb140967cdd2b3e99aa439e0f6bf8b56faeb
languageName: node
linkType: hard

"dotenv@npm:8.2.0":
version: 8.2.0
resolution: "dotenv@npm:8.2.0"
checksum: 10c0/b6a07a2c400b13ad4e59c34e4682256e6bb846469781a3963b36861ee608ed312e6125c4a7635a9edcf957bb294a6966e218f0e26b82ff0bda9184211d4bc141
languageName: node
linkType: hard

"dotenv@npm:^16.0.3":
version: 16.4.5
resolution: "dotenv@npm:16.4.5"
Expand Down
2 changes: 1 addition & 1 deletion plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ web stop`,
}

if ok, url := isWebRunning(webPidFile, webRunFlags.RunInfoDir); ok {
return fmt.Errorf("another server is already running by URL: %s. please stop the existing server before initiating a new one", url)
return fmt.Errorf("another server is already running at the URL: %s. please stop the existing server before starting a new one", url)
}

if foreground {
Expand Down
6 changes: 3 additions & 3 deletions scripts/prebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func main() {
}

release := os.Args[1]
folderPath := os.Args[2]
dirPath := os.Args[2]

archivePath := filepath.Clean(filepath.Join(folderPath, "dist.tar.gz"))
resultPath := filepath.Clean(filepath.Join(folderPath, "."))
archivePath := filepath.Clean(filepath.Join(dirPath, "dist.tar.gz"))
resultPath := filepath.Clean(filepath.Join(dirPath, "."))

fmt.Println("Trying to download dist archive...")

Expand Down
4 changes: 0 additions & 4 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func (l *launchrServer) GetCustomisationConfig(w http.ResponseWriter, _ *http.Re
var launchrConfig *launchrWebConfig
err := l.cfg.Get("web", &launchrConfig)
if err != nil {
log.Debug(err.Error())
sendError(w, http.StatusInternalServerError, "error getting config")
return
}
Expand All @@ -72,7 +71,6 @@ func (l *launchrServer) GetCustomisationConfig(w http.ResponseWriter, _ *http.Re

gvFile, err := parseVarsFile(launchrConfig.VarsFile)
if err != nil {
log.Debug(err.Error())
sendError(w, http.StatusInternalServerError, "error getting group vars file")
return
}
Expand Down Expand Up @@ -117,7 +115,6 @@ func (l *launchrServer) GetRunningActionStreams(w http.ResponseWriter, _ *http.R
}
sd, err := fStreams.GetStreamData(params)
if err != nil {
log.Debug(err.Error())
sendError(w, http.StatusInternalServerError, "Error reading streams")
}

Expand Down Expand Up @@ -231,7 +228,6 @@ func (l *launchrServer) RunAction(w http.ResponseWriter, r *http.Request, id str
// Can we fetch directly json?
streams, err := createFileStreams(runID)
if err != nil {
log.Debug(err.Error())
sendError(w, http.StatusInternalServerError, "Error preparing streams")
}

Expand Down
10 changes: 5 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func Run(ctx context.Context, app launchr.App, opts *RunOptions) error {
cancel()
_, err = w.Write([]byte("Server is shutting down..."))
if err != nil {
log.Debug(err.Error())
log.Warn(err.Error())
}
})

Expand Down Expand Up @@ -179,7 +179,7 @@ func Run(ctx context.Context, app launchr.App, opts *RunOptions) error {
func onShutdown(dir string) {
err := os.RemoveAll(dir)
if err != nil {
log.Debug(err.Error())
log.Warn(err.Error())
}
}

Expand All @@ -194,13 +194,13 @@ func GetRunInfo(storePath string) (*RunInfo, error) {

data, err := os.ReadFile(filepath.Clean(riPath))
if err != nil {
return nil, fmt.Errorf("error reading plugin storage path: %v", err)
return nil, fmt.Errorf("error reading plugin storage path: %w", err)
}

var ri RunInfo
err = yaml.Unmarshal(data, &ri)
if err != nil {
return nil, fmt.Errorf("error unmarshalling yaml: %v", err)
return nil, fmt.Errorf("error unmarshalling yaml: %w", err)
}

return &ri, nil
Expand Down Expand Up @@ -245,7 +245,7 @@ func popInBrowser(url string) bool {
for {
req, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
log.Debug(err.Error())
log.Warn(err.Error())
return false
}
resp, err := client.Do(req)
Expand Down
12 changes: 6 additions & 6 deletions web-runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func runWeb(ctx context.Context, p *Plugin, webOpts *webFlags) error {
var err error

port := webOpts.Port
if !isPortFree(port) {
if !isAvailablePort(port) {
log.Info("The port %d you are trying to use for the web server is not available.", port)
port, err = getAvailablePort(port)
if err != nil {
Expand Down Expand Up @@ -83,7 +83,7 @@ func runBackgroundWeb(cmd *cobra.Command, p *Plugin, flags *webFlags, pidFile st
// Kill existing process
_ = killProcess(pid)

// Cleanup temp folder
// Cleanup temp dir
err = os.RemoveAll(flags.RunInfoDir)
if err != nil {
log.Debug(err.Error())
Expand Down Expand Up @@ -241,27 +241,27 @@ func stopWeb(pidFile, runInfoDir string) error {

func getAvailablePort(port int) (int, error) {
// Quick check if port available and return if yes.
if isPortFree(port) {
if isAvailablePort(port) {
return port, nil
}

maxPort := 65535
newPort := 49152

// Check available port from pool.
for !isPortFree(newPort) && newPort < maxPort {
for !isAvailablePort(newPort) && newPort < maxPort {
log.Debug("port %d is not available", newPort)
newPort++
}

if newPort >= maxPort && !isPortFree(newPort) {
if newPort >= maxPort && !isAvailablePort(newPort) {
panic("port limit exceeded")
}

return newPort, nil
}

func isPortFree(port int) bool {
func isAvailablePort(port int) bool {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return false
Expand Down

0 comments on commit 80a3f47

Please sign in to comment.