| 1 | package Project::Easy::Daemon; | ||
| 2 | |||
| 3 | use Class::Easy; | ||
| 4 | use IO::Easy; | ||
| 5 | |||
| 6 | use POSIX (); | ||
| 7 | |||
| 8 | use Project::Easy::Config; | ||
| 9 | |||
| 10 | has 'pid_file'; | ||
| 11 | has 'code'; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $class = shift; | ||
| 15 | my $core = shift; | ||
| 16 | my $code = shift; | ||
| 17 | my $config = shift; | ||
| 18 | |||
| 19 | my $root = $core->root; | ||
| 20 | my $id = $core->id; | ||
| 21 | |||
| 22 | if (defined $config->{pid_file}) { | ||
| 23 | $config->{pid_file} = file ($config->{pid_file}); | ||
| 24 | } else { | ||
| 25 | |||
| 26 | $config->{pid_file} = $root->file_io ('var', 'run', $id . '-' . $code . '.pid'); | ||
| 27 | } | ||
| 28 | |||
| 29 | if (defined $config->{log_file}) { | ||
| 30 | $config->{log_file} = file ($config->{log_file}); | ||
| 31 | } else { | ||
| 32 | $config->{log_file} = $root->file_io ('var', 'log', $id . '-' . $code . '_log'); | ||
| 33 | } | ||
| 34 | |||
| 35 | $config->{code} = $code; | ||
| 36 | |||
| 37 | bless $config, $class; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub startup_script { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | |||
| 44 | } | ||
| 45 | |||
| 46 | sub create_script_file { | ||
| 47 | my $self = shift; | ||
| 48 | my $root = shift; | ||
| 49 | |||
| 50 | my $script = $root->file_io ('bin', 'daemon_'.$self->code); | ||
| 51 | |||
| 52 | my $data_files = file->__data__files (); | ||
| 53 | |||
| 54 | my $script_text = Project::Easy::Config::string_from_template ( | ||
| 55 | $data_files->{script}, | ||
| 56 | { | ||
| 57 | daemon_code => $self->code | ||
| 58 | } | ||
| 59 | ); | ||
| 60 | |||
| 61 | $script->store ($script_text); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub launch { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | if ($self->{package}) { | ||
| 68 | |||
| 69 | if ($self->{user}) { | ||
| 70 | my $uid = $self->{user}; | ||
| 71 | $uid = (getpwnam ($self->{user})) [2] | ||
| 72 | if $self->{user} !~ /^\d+$/; | ||
| 73 | |||
| 74 | POSIX::setuid ($uid) | ||
| 75 | if defined $uid and $uid; | ||
| 76 | } | ||
| 77 | |||
| 78 | if ($self->{group}) { | ||
| 79 | my $gid = $self->{group}; | ||
| 80 | $gid = (getgrnam ($self->{group})) [2] | ||
| 81 | if $self->{group} !~ /^\d+$/; | ||
| 82 | |||
| 83 | POSIX::setgid ($gid) | ||
| 84 | if defined $gid and $gid; | ||
| 85 | } | ||
| 86 | |||
| 87 | my $ppid = fork(); | ||
| 88 | |||
| 89 | if (not defined $ppid) { | ||
| 90 | print "resources not avilable.\n"; | ||
| 91 | } elsif ($ppid == 0) { | ||
| 92 | # exit(0); | ||
| 93 | die "cannot detach from controlling terminal" | ||
| 94 | if POSIX::setsid() < 0; | ||
| 95 | |||
| 96 | my $log_fh; | ||
| 97 | |||
| 98 | my $pid_file = $self->{pid_file}; | ||
| 99 | $pid_file->store ($$); | ||
| 100 | |||
| 101 | die 'cannot open log file to append' | ||
| 102 | unless open $log_fh, '>>', $self->{log_file}->path; | ||
| 103 | |||
| 104 | # DIRTY | ||
| 105 | $SIG{__WARN__} = sub { | ||
| 106 | print $log_fh @_; | ||
| 107 | }; | ||
| 108 | $SIG{__DIE__} = sub { | ||
| 109 | print $log_fh @_; | ||
| 110 | }; | ||
| 111 | |||
| 112 | my $previous_default = select ($log_fh); | ||
| 113 | $|++; | ||
| 114 | select ($previous_default); | ||
| 115 | |||
| 116 | close STDOUT; | ||
| 117 | close STDERR; | ||
| 118 | close STDIN; | ||
| 119 | |||
| 120 | if ($self->can ('_launched')) { | ||
| 121 | $self->_launched; | ||
| 122 | } | ||
| 123 | |||
| 124 | } else { | ||
| 125 | exit (0); | ||
| 126 | } | ||
| 127 | |||
| 128 | } elsif ($self->{bin}) { | ||
| 129 | # DEPRECATED | ||
| 130 | my $conf_file = $self->{conf_file}; | ||
| 131 | my $httpd_bin = $self->{bin}; | ||
| 132 | print "starting daemon by: $httpd_bin -f $conf_file\n"; | ||
| 133 | `$httpd_bin -f $conf_file`; | ||
| 134 | |||
| 135 | } else { | ||
| 136 | die "you must define daemon package or bin file"; | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | sleep 1; | ||
| 141 | |||
| 142 | warn "not running" | ||
| 143 | unless $self->running; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub running { | ||
| 147 | my $self = shift; | ||
| 148 | |||
| 149 | my $pid = $self->pid; | ||
| 150 | |||
| 151 | return 0 unless defined $pid; | ||
| 152 | |||
| 153 | if ($^O eq 'darwin') { | ||
| 154 | my $ps = `ps -x -p $pid -o rss=,command=`; | ||
| 155 | if ($ps =~ /^\s+(\d+)\s+(.*)$/m) { | ||
| 156 | return 1; | ||
| 157 | } | ||
| 158 | } else { | ||
| 159 | return kill 0, $pid; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | sub pid { | ||
| 164 | my $self = shift; | ||
| 165 | |||
| 166 | my $pid; | ||
| 167 | if (-f $self->pid_file) { | ||
| 168 | $pid = $self->pid_file->contents; | ||
| 169 | } | ||
| 170 | |||
| 171 | return unless defined $pid; | ||
| 172 | |||
| 173 | return unless $pid =~ /(\d+)/; | ||
| 174 | |||
| 175 | return $1; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub shutdown { | ||
| 179 | my $self = shift; | ||
| 180 | |||
| 181 | my $pid = $self->pid; | ||
| 182 | |||
| 183 | return 1 unless defined $pid; | ||
| 184 | |||
| 185 | kill 15, $pid; | ||
| 186 | |||
| 187 | my $count = 10; | ||
| 188 | |||
| 189 | for (1..$count) { | ||
| 190 | print ($count + 1 - $_ ."… "); | ||
| 191 | sleep 1; | ||
| 192 | # wait one more second | ||
| 193 | unless ($self->running) { | ||
| 194 | unlink $self->pid_file; | ||
| 195 | return 1; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | print "kill with force\n"; | ||
| 200 | |||
| 201 | kill 9, $pid; | ||
| 202 | |||
| 203 | for (1..$count) { | ||
| 204 | print ($count + 1 - $_ ."… "); | ||
| 205 | sleep 1; | ||
| 206 | # wait one more second | ||
| 207 | unless ($self->running) { | ||
| 208 | unlink $self->pid_file; | ||
| 209 | return 1; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | return 0; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub process_command { | ||
| 217 | my $self = shift; | ||
| 218 | my $comm = shift; | ||
| 219 | |||
| 220 | if ($comm eq 'stop') { | ||
| 221 | if (! $self->running) { | ||
| 222 | print "no process is running\n"; | ||
| 223 | exit; | ||
| 224 | } | ||
| 225 | |||
| 226 | print "awaiting process to kill… "; | ||
| 227 | $self->shutdown; | ||
| 228 | |||
| 229 | print "\n"; | ||
| 230 | |||
| 231 | } elsif ($comm eq 'start') { | ||
| 232 | if ($self->running) { | ||
| 233 | print "no way, process is running\n"; | ||
| 234 | exit; | ||
| 235 | } | ||
| 236 | |||
| 237 | $self->launch; | ||
| 238 | |||
| 239 | } elsif ($comm eq 'restart') { | ||
| 240 | if ($self->running) { | ||
| 241 | my $finished = $self->shutdown; | ||
| 242 | |||
| 243 | unless ($finished) { | ||
| 244 | print "pid is running after kill, please kill manually\n"; | ||
| 245 | exit; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | $self->launch; | ||
| 250 | } | ||
| 251 | |||
| 252 | } | ||
| 253 | |||
| 254 | sub TO_JSON { | ||
| 255 | my $self = shift; | ||
| 256 | |||
| 257 | my $result = {map {$_ => $self->{$_}} keys %$self}; | ||
| 258 | $result->{pid_file} = $result->{pid_file}->path; | ||
| 259 | $result->{log_file} = $result->{log_file}->path; | ||
| 260 | |||
| 261 | $result; | ||
| 262 | } | ||
| 263 | |||
| 264 | 1; | ||
| 265 | |||
| 266 | __DATA__ |