Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
26a998f7
Commit
26a998f7
authored
May 08, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer/plugin: Managed clients for automatic cleanup
parent
c8079a42
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
3 deletions
+32
-3
packer.go
packer.go
+1
-2
packer/plugin/client.go
packer/plugin/client.go
+30
-0
packer/plugin/command.go
packer/plugin/command.go
+1
-1
No files found.
packer.go
View file @
26a998f7
...
...
@@ -8,7 +8,6 @@ import (
"log"
"os"
"os/exec"
"time"
)
func
main
()
{
...
...
@@ -41,6 +40,6 @@ func main() {
}
exitCode
,
_
:=
env
.
Cli
(
os
.
Args
[
1
:
])
time
.
Sleep
(
1
*
time
.
Second
)
plugin
.
CleanupClients
(
)
os
.
Exit
(
exitCode
)
}
packer/plugin/client.go
View file @
26a998f7
...
...
@@ -8,15 +8,39 @@ import (
"log"
"os/exec"
"strings"
"sync"
"time"
)
// This is a slice of the "managed" clients which are cleaned up when
// calling Cleanup
var
managedClients
=
make
([]
*
client
,
0
,
5
)
type
client
struct
{
cmd
*
exec
.
Cmd
exited
bool
doneLogging
bool
}
// This makes sure all the managed subprocesses are killed and properly
// logged. This should be called before the parent process running the
// plugins exits.
func
CleanupClients
()
{
// Kill all the managed clients in parallel and use a WaitGroup
// to wait for them all to finish up.
var
wg
sync
.
WaitGroup
for
_
,
client
:=
range
managedClients
{
wg
.
Add
(
1
)
go
func
()
{
client
.
Kill
()
wg
.
Done
()
}()
}
wg
.
Wait
()
}
func
NewClient
(
cmd
*
exec
.
Cmd
)
*
client
{
return
&
client
{
cmd
,
...
...
@@ -25,6 +49,12 @@ func NewClient(cmd *exec.Cmd) *client {
}
}
func
NewManagedClient
(
cmd
*
exec
.
Cmd
)
(
result
*
client
)
{
result
=
NewClient
(
cmd
)
managedClients
=
append
(
managedClients
,
result
)
return
}
func
(
c
*
client
)
Exited
()
bool
{
return
c
.
exited
}
...
...
packer/plugin/command.go
View file @
26a998f7
...
...
@@ -52,7 +52,7 @@ func (c *cmdCommand) checkExit(p interface{}, cb func()) {
//
// This function guarantees the subprocess will end in a timely manner.
func
Command
(
cmd
*
exec
.
Cmd
)
(
result
packer
.
Command
,
err
error
)
{
cmdClient
:=
NewClient
(
cmd
)
cmdClient
:=
New
Managed
Client
(
cmd
)
address
,
err
:=
cmdClient
.
Start
()
if
err
!=
nil
{
return
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment