Skip to content

Commit

Permalink
process lock to check if the pid is running in the background
Browse files Browse the repository at this point in the history
Signed-off-by: daveaugustus <[email protected]>
  • Loading branch information
daveaugustus committed Dec 19, 2024
1 parent eb0ccb4 commit 65cafb9
Showing 1 changed file with 54 additions and 19 deletions.
73 changes: 54 additions & 19 deletions components/automate-cli/cmd/chef-automate/reindexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -78,6 +79,8 @@ var infoReindexing = `
Reindexing of Elasticsearch/OpenSearch indices if needed.
`

const lockFile = "/tmp/reindex.lock"

var (
statusFile = "/hab/status.json"
taskMutex sync.Mutex
Expand All @@ -99,7 +102,7 @@ var reindexCmd = &cobra.Command{
}

var statusCmd = &cobra.Command{
Use: "indexing-status",
Use: "index-status",
Short: "Check the reindexing status",
RunE: checkStatus,
}
Expand Down Expand Up @@ -160,19 +163,51 @@ func readStatus() (Status, error) {
}

func updateStatus(status *Status) error {
// taskMutex.Lock()
// defer taskMutex.Unlock()

status.LastUpdatedAt = time.Now().Format(time.RFC3339)

file, err := json.MarshalIndent(status, "", " ")
if err != nil {
return err
}
fmt.Printf("file: %v\n", string(file))

return os.WriteFile(statusFile, file, 0644)
}

func isProcessRunning() bool {
data, err := os.ReadFile(lockFile)
if err != nil {
if os.IsNotExist(err) {
return false // No lock file means no process is running
}
fmt.Printf("Error reading lock file: %v\n", err)
return false
}

pid := strings.TrimSpace(string(data))
pidInt, err := strconv.Atoi(pid)
if err != nil {
fmt.Printf("Error converting PID to integer: %v\n", err)
return false
}
process, err := os.FindProcess(pidInt)
if err != nil {
return false // Process not found
}

// Check if the process is still alive
err = process.Signal(syscall.Signal(0))
return err == nil
}

func createLockFile() error {
pid := fmt.Sprintf("%d\n", os.Getpid())
return os.WriteFile(lockFile, []byte(pid), 0644)
}

func removeLockFile() {
os.Remove(lockFile) // Ignore errors during cleanup
}

func runInBackground() {
cmd := exec.Command(os.Args[0], append(os.Args[1:], "--background")...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} // Detach process group
Expand All @@ -191,20 +226,20 @@ func runReindex(cmd *cobra.Command, args []string) error {
backgroundFlag, _ := cmd.Flags().GetBool("background")

if !backgroundFlag {
// Check if reindexing is already running
if reindexStatus.Status == "Running" {
fmt.Println("Reindexing process is already running. Run `chef-automate indexing-status` to get the status.")
if isProcessRunning() {
fmt.Println("Reindexing process is already running. Run `chef-automate index-status` to get the status.")
return nil
}

runInBackground()
}

fmt.Println("Starting reindexing process...")

if reindexStatus.Status == "Running" {
fmt.Println("Reindexing process is already running. Run `chef-automate indexing-status` to get the status.")
return nil
if err := createLockFile(); err != nil {
return fmt.Errorf("failed to create lock file: %v", err)
}
defer removeLockFile() // Ensure cleanup after reindexing completes

fmt.Println("Starting reindexing process...")

osInfo, err := getOpenSearchInfo()
if err != nil {
Expand All @@ -213,19 +248,19 @@ func runReindex(cmd *cobra.Command, args []string) error {

reindexStatus.OpenSearchVersion = osInfo.Version.Number
reindexStatus.Status = "Running"
fmt.Println("Fetching indices from Elasticsearch/OpenSearch.")

indices, err := fetchIndices()
if err != nil {
fmt.Printf("Error fetching indices: %v\n", err)
return err
}

// Add indices to the queue for reindexing
OuterLoop:
for _, index := range indices {
for prefix := range skipIndices {
if strings.HasPrefix(index.Index, prefix) {
fmt.Printf("Skipping index %s\n", index.Index)
continue
continue OuterLoop
}
}

Expand All @@ -238,12 +273,12 @@ func runReindex(cmd *cobra.Command, args []string) error {
reindexStatus.CompletedIndex = append(reindexStatus.CompletedIndex, index.Index)
}

fmt.Printf("reindexStatus: %v\n", reindexStatus)
}

reindexStatus.Status = "Completed"
if err := updateStatus(reindexStatus); err != nil {
fmt.Printf("Error updating status: %v\n", err)
}
updateStatus(reindexStatus)
fmt.Println("Reindexing process completed.")
return nil
}

Expand Down

0 comments on commit 65cafb9

Please sign in to comment.