Skip to content

Commit

Permalink
Export "isProcessRunning" (#1126)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi authored Feb 13, 2024
1 parent d52f433 commit 4bf1fe8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
7 changes: 4 additions & 3 deletions utils/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/jfrog/jfrog-cli-core/v2/utils/osutils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
Expand Down Expand Up @@ -129,9 +130,9 @@ func (lock *Lock) removeOtherLockOrWait(otherLock Lock, filesList *[]string) err
log.Debug("Lock hasn't been acquired.")

// Check if the process is running.
// There are two implementation of the 'isProcessRunning'.
// There are two implementation of the 'IsProcessRunning'.
// One for Windows and one for Unix based systems.
running, err := isProcessRunning(otherLock.pid)
running, err := osutils.IsProcessRunning(otherLock.pid)
if err != nil {
return err
}
Expand Down Expand Up @@ -241,7 +242,7 @@ func GetLastLockTimestamp(lockDirPath string) (int64, error) {
lastLock := locks[len(locks)-1]

// If the lock isn't acquired by a running process, an unexpected error was occurred.
running, err := isProcessRunning(lastLock.pid)
running, err := osutils.IsProcessRunning(lastLock.pid)
if err != nil {
return 0, err
}
Expand Down
7 changes: 4 additions & 3 deletions utils/lock/utils_unix.go → utils/osutils/utils_unix.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
//go:build linux || darwin || freebsd
// +build linux darwin freebsd

package lock
package osutils

import (
"github.com/jfrog/jfrog-client-go/utils/log"
"os"
"syscall"

"github.com/jfrog/jfrog-client-go/utils/log"
)

// This file will be compiled only on unix systems.
// Checks if the process is running.
// If error occurs, check if the error is part of the OS permission errors. This means the process is running.
// Else means the process is not running.
func isProcessRunning(pid int) (bool, error) {
func IsProcessRunning(pid int) (bool, error) {
process, err := os.FindProcess(pid)
if err != nil {
return false, err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package lock
package osutils

import (
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"syscall"

"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

// This file will be compiled on Windows.
// Checks if the process can be reached.
// If an error occurs, check if the error is part of the invalid parameter. This means the process is not running.
// Else find the exit code. If the exit code 259 means the process is running.
func isProcessRunning(pid int) (bool, error) {
func IsProcessRunning(pid int) (bool, error) {
process, err := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, true, uint32(pid))
if err != nil {
// Check if err is of type of syscall.Errno, which is a Windows error number.
Expand Down

0 comments on commit 4bf1fe8

Please sign in to comment.