findInstances finds running instances of commands using cached ps output
(commandStrings []string)
| 1700 | |
| 1701 | // findInstances finds running instances of commands using cached ps output |
| 1702 | func findInstances(commandStrings []string) []JobInstance { |
| 1703 | if len(commandStrings) == 0 { |
| 1704 | return []JobInstance{} |
| 1705 | } |
| 1706 | |
| 1707 | lines := getCachedProcessList() |
| 1708 | instances := make([]JobInstance, 0) |
| 1709 | seenPGIDs := make(map[string]bool) // To avoid duplicate entries |
| 1710 | |
| 1711 | for _, line := range lines { |
| 1712 | fields := strings.Fields(line) |
| 1713 | if len(fields) < 8 { |
| 1714 | continue |
| 1715 | } |
| 1716 | |
| 1717 | pgid := fields[0] // Process group ID |
| 1718 | // The start time is fields[1:6] (day month date time year) |
| 1719 | started := strings.Join(fields[1:6], " ") |
| 1720 | args := strings.Join(fields[6:], " ") |
| 1721 | |
| 1722 | // Skip if this is a cronitor exec command |
| 1723 | if strings.Contains(args, "cronitor exec") { |
| 1724 | continue |
| 1725 | } |
| 1726 | |
| 1727 | // Check if any of the command strings match |
| 1728 | for _, cmdStr := range commandStrings { |
| 1729 | if strings.Contains(args, cmdStr) && !seenPGIDs[pgid] { |
| 1730 | instances = append(instances, JobInstance{ |
| 1731 | PID: pgid, |
| 1732 | Started: started, |
| 1733 | }) |
| 1734 | seenPGIDs[pgid] = true |
| 1735 | break |
| 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | return instances |
| 1741 | } |
| 1742 | |
| 1743 | // handleRunJob handles POST requests to run a job |
| 1744 | func handleRunJob(w http.ResponseWriter, r *http.Request) { |
no test coverage detected