mirror of
https://github.com/datarhei/core.git
synced 2025-09-26 20:11:29 +08:00
Add process id and reference glob pattern matching
For the API endpoint /v3/process two new query parameter are introduced in order to list only processes that match a pattern for the id and the reference: idpattern and refpattern. The pattern is a glob pattern. If patterns for both are given, the results will be intersected. If you use other query parameters such as id or reference, they will be applied after the result of the pattern matching.
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/datarhei/core/v16/ffmpeg"
|
||||
"github.com/datarhei/core/v16/ffmpeg/parse"
|
||||
"github.com/datarhei/core/v16/ffmpeg/skills"
|
||||
"github.com/datarhei/core/v16/glob"
|
||||
"github.com/datarhei/core/v16/io/fs"
|
||||
"github.com/datarhei/core/v16/log"
|
||||
"github.com/datarhei/core/v16/net"
|
||||
@@ -33,7 +34,7 @@ type Restreamer interface {
|
||||
Start() // start all processes that have a "start" order
|
||||
Stop() // stop all running process but keep their "start" order
|
||||
AddProcess(config *app.Config) error // add a new process
|
||||
GetProcessIDs() []string // get a list of all process IDs
|
||||
GetProcessIDs(idpattern, refpattern string) []string // get a list of process IDs based on patterns for ID and reference
|
||||
DeleteProcess(id string) error // delete a process
|
||||
UpdateProcess(id string, config *app.Config) error // update a process
|
||||
StartProcess(id string) error // start a process
|
||||
@@ -808,14 +809,64 @@ func (r *restream) UpdateProcess(id string, config *app.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *restream) GetProcessIDs() []string {
|
||||
func (r *restream) GetProcessIDs(idpattern, refpattern string) []string {
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
|
||||
ids := make([]string, len(r.tasks))
|
||||
if len(idpattern) == 0 && len(refpattern) == 0 {
|
||||
ids := make([]string, len(r.tasks))
|
||||
i := 0
|
||||
|
||||
for id := range r.tasks {
|
||||
ids[i] = id
|
||||
i++
|
||||
}
|
||||
|
||||
return ids
|
||||
}
|
||||
|
||||
idmap := map[string]int{}
|
||||
|
||||
if len(idpattern) != 0 {
|
||||
for id := range r.tasks {
|
||||
match, err := glob.Match(idpattern, id)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !match {
|
||||
continue
|
||||
}
|
||||
|
||||
idmap[id] = 1
|
||||
}
|
||||
}
|
||||
|
||||
if len(refpattern) != 0 {
|
||||
for _, t := range r.tasks {
|
||||
match, err := glob.Match(refpattern, t.reference)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !match {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := idmap[t.id]; ok {
|
||||
idmap[t.id]++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ids := make([]string, len(idmap))
|
||||
i := 0
|
||||
|
||||
for id := range r.tasks {
|
||||
for id, count := range idmap {
|
||||
if count == 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
ids[i] = id
|
||||
i++
|
||||
}
|
||||
|
@@ -206,7 +206,7 @@ func TestGetProcess(t *testing.T) {
|
||||
_, err = rs.GetProcess(process.ID)
|
||||
require.Equal(t, nil, err, "Process not found (%s)", process.ID)
|
||||
|
||||
list := rs.GetProcessIDs()
|
||||
list := rs.GetProcessIDs("", "")
|
||||
require.Len(t, list, 1, "expected 1 process")
|
||||
require.Equal(t, process.ID, list[0], "expected same process ID")
|
||||
}
|
||||
|
Reference in New Issue
Block a user