8000 fix: cleanup additional temporary directories · symfony-cli/symfony-cli@d820fed · GitHub
[go: up one dir, main page]

Skip to content

Commit d820fed

Browse files
committed
fix: cleanup additional temporary directories
1 parent a2f79df commit d820fed

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

commands/local_server_start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ var localServerStartCmd = &console.Command{
421421
if p.PHPServer != nil {
422422
<-p.PHPServer.StoppedChan
423423
}
424+
pidFile.CleanupDirectories()
424425
ui.Success("Stopped all processes successfully")
425426
}
426427
return nil

local/php/fpm.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,5 @@ env['LC_ALL'] = C
114114
}
115115

116116
func (p *Server) fpmConfigFile() string {
117-
path := filepath.Join(p.homeDir, fmt.Sprintf("php/%s/fpm-%s.ini", name(p.projectDir), p.Version.Version))
118-
if _, err := os.Stat(filepath.Dir(path)); os.IsNotExist(err) {
119-
_ = os.MkdirAll(filepath.Dir(path), 0755)
120-
}
121-
return path
117+
return filepath.Join(p.tempDir, fmt.Sprintf("fpm-%s.ini", p.Version.Version))
122118
}

local/php/php_builtin_server.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package php
2121

2222
import (
2323
"fmt"
24-
"os"
2524
"path/filepath"
2625
)
2726

@@ -61,9 +60,5 @@ require $script;
6160
`)
6261

6362
func (p *Server) phpRouterFile() string {
64-
path := filepath.Join(p.homeDir, fmt.Sprintf("php/%s-router.php", name(p.projectDir)))
65-
if _, err := os.Stat(filepath.Dir(path)); os.IsNotExist(err) {
66-
_ = os.MkdirAll(filepath.Dir(path), 0755)
67-
}
68-
return path
63+
return filepath.Join(p.tempDir, fmt.Sprintf("%s-router.php", p.Version.Version))
6964
}

local/php/php_server.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ type Server struct {
5050
logger zerolog.Logger
5151
StoppedChan chan bool
5252
appVersion string
53-
homeDir string
5453
projectDir string
5554
documentRoot string
5655
passthru string
5756
addr string
5857
proxy *httputil.ReverseProxy
58+
59+
tempDir string
5960
}
6061

6162
var addslashes = strings.NewReplacer("\\", "\\\\", "'", "\\'")
@@ -76,7 +77,6 @@ func NewServer(homeDir, projectDir, documentRoot, passthru, appVersion string, l
7677
Version: version,
7778
logger: logger.With().Str("source", "PHP").Str("php", version.Version).Str("path", version.ServerPath()).Logger(),
7879
appVersion: appVersion,
79-
homeDir: homeDir,
8080
projectDir: projectDir,
8181
documentRoot: documentRoot,
8282
passthru: passthru,
@@ -86,7 +86,13 @@ func NewServer(homeDir, projectDir, documentRoot, passthru, appVersion string, l
8686

8787
// Start starts a PHP server
8888
func (p *Server) Start(ctx context.Context, pidFile *pid.PidFile) (*pid.PidFile, func() error, error) {
89-
var pathsToRemove []string
89+
p.tempDir = pidFile.TempDirectory()
90+
if _, err := os.Stat(p.tempDir); os.IsNotExist(err) {
91+
if err = os.MkdirAll(p.tempDir, 0755); err != nil {
92+
return nil, nil, err
93+
}
94+
}
95+
9096
port, err := process.FindAvailablePort()
9197
if err != nil {
9298
p.logger.Debug().Err(err).Msg("unable to find an available port")
@@ -125,7 +131,6 @@ func (p *Server) Start(ctx context.Context, pidFile *pid.PidFile) (*pid.PidFile,
125131
return nil, nil, errors.WithStack(err)
126132
}
127133
p.proxy.Transport = &cgiTransport{}
128-
pathsToRemove = append(pathsToRemove, fpmConfigFile)
129134
binName = "php-fpm"
130135
workerName = "PHP-FPM"
131136
args = []string{p.Version.ServerPath(), "--nodaemonize", "--fpm-config", fpmConfigFile}
@@ -151,7 +156,6 @@ func (p *Server) Start(ctx context.Context, pidFile *pid.PidFile) (*pid.PidFile,
151156
if err := 1E80 os.WriteFile(routerPath, phprouter, 0644); err != nil {
152157
return nil, nil, errors.WithStack(err)
153158
}
154-
pathsToRemove = append(pathsToRemove, routerPath)
155159
binName = "php"
156 F438 160
workerName = "PHP"
157161
args = []string{p.Version.ServerPath(), "-S", "127.0.0.1:" + strconv.Itoa(port), "-d", "variables_order=EGPCS", routerPath}
@@ -194,9 +198,6 @@ func (p *Server) Start(ctx context.Context, pidFile *pid.PidFile) (*pid.PidFile,
194198

195199
return phpPidFile, func() error {
196200
defer func() {
197-
for _, path := range pathsToRemove {
198-
os.RemoveAll(path)
199-
}
200201
e.CleanupTemporaryDirectories()
201202
p.StoppedChan <- true
202203
}()

local/pid/pidfile.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,19 @@ func (p *PidFile) WorkerPidDir() string {
232232
return filepath.Join(util.GetHomeDir(), "var", name(p.Dir))
233233
}
234234

235+
func (p *PidFile) TempDirectory() string {
236+
return filepath.Join(util.GetHomeDir(), "php", name(p.Dir))
237+
}
238+
239+
func (p *PidFile) CleanupDirectories() {
240+
os.RemoveAll(p.TempDirectory())
241+
// We don't want to force removal of log and pid files, we only want to
242+
// clean up empty directories. To do so we use `os.Remove` instead of
243+
// `os.RemoveAll`
244+
os.Remove(p.WorkerLogDir())
245+
os.Remove(p.WorkerPidDir())
246+
}
247+
235248
func (p *PidFile) LogReader() (io.ReadCloser, error) {
236249
logFile := p.LogFile()
237250
if err := os.MkdirAll(filepath.Dir(logFile), 0755); err != nil {

0 commit comments

Comments
 (0)
0