@@ -6,20 +6,12 @@ def self.start
6
6
new . start
7
7
end
8
8
9
- def start
9
+ def initialize
10
10
@server = Server . new ( self )
11
- @pid = fork do
12
- monitor_server
13
- exit_hook
14
- # Using IO.popen(command, 'r+') will avoid watch_command read from $stdin.
15
- # If we use system(*command) instead, IRB and Debug can't read from $stdin
16
- # correctly bacause some keystrokes will be taken by watch_command.
17
- IO . popen ( Commands . watch_command , 'r+' ) do |io |
18
- IO . copy_stream ( io , $stdout)
19
- end
20
- end
21
- Process . detach pid
11
+ end
22
12
13
+ def start
14
+ @pid = existing_process || start_process
23
15
server . monitor_process
24
16
server . exit_hook
25
17
end
@@ -41,6 +33,37 @@ def dead?
41
33
42
34
private
43
35
36
+ def existing_process
37
+ if ( pid = Pidfile . pid )
38
+ begin
39
+ Process . kill 0 , pid
40
+ pid
41
+ rescue Errno ::ESRCH
42
+ # Process does not exist
43
+ rescue Errno ::EPERM
44
+ # Ignore process owned by another user
45
+ end
46
+ end
47
+ end
48
+
49
+ def start_process
50
+ pid = fork do
51
+ Pidfile . write
52
+ monitor_server
53
+ exit_hook
54
+ # Using IO.popen(command, 'r+') will avoid watch_command read from $stdin.
55
+ # If we use system(*command) instead, IRB and Debug can't read from $stdin
56
+ # correctly bacause some keystrokes will be taken by watch_command.
57
+ IO . popen ( Commands . watch_command , 'r+' ) do |io |
58
+ IO . copy_stream ( io , $stdout)
59
+ end
60
+ ensure
61
+ Pidfile . delete
62
+ end
63
+ Process . detach pid
64
+ pid
65
+ end
66
+
44
67
def monitor_server
45
68
Thread . new do
46
69
loop do
@@ -60,6 +83,33 @@ def exit_hook
60
83
end
61
84
end
62
85
86
+ module Pidfile
87
+ def self . path
88
+ Rails . root . join ( "tmp" , "pids" , "tailwindcss.txt" )
89
+ end
90
+
91
+ def self . read
92
+ File . read ( path , mode : "rb:UTF-8" )
93
+ rescue Errno ::ENOENT
94
+ # File does not exist
95
+ end
96
+
97
+ def self . write
98
+ File . write ( path , Process . pid , mode : "wb:UTF-8" )
99
+ end
100
+
101
+ def self . delete
102
+ File . exist? ( path ) && File . delete ( path )
103
+ end
104
+
105
+ def self . pid
106
+ Integer ( read )
107
+ rescue ArgumentError , TypeError
108
+ # Invalid content
109
+ delete
110
+ end
111
+ end
112
+
63
113
class Server
64
114
attr_reader :process , :pid
65
115
0 commit comments