Skip to content

Commit

Permalink
#610 Add zold node --nohup-log-rotate option
Browse files Browse the repository at this point in the history
  • Loading branch information
v-kolesnikov committed May 8, 2019
1 parent 7e0f693 commit b56aaea
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/zold/commands/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def run(args = [])
o.string '--nohup-log',
'The file to log output into (default: zold.log)',
default: 'zold.log'
o.string '--nohup-log-rotate',
'Frequency of the log file rotation (daily, weekly or monthly)'
o.integer '--nohup-log-truncate',
'The maximum amount of bytes to keep in the file, and truncate it in half if it grows bigger',
default: 1024 * 1024
Expand Down Expand Up @@ -393,12 +395,17 @@ def exec(cmd, nohup_log)

def nohup(opts)
pid = fork do
nohup_log = NohupLog.new(opts['nohup-log'], opts['nohup-log-truncate'])
nohup_log =
if opts['nohup-log-rotate']
NohupLogRotate.new(opts['nohup-log'], opts['nohup-log-rotate'])
else
NohupLog.new(opts['nohup-log'], opts['nohup-log-truncate'])
end
Signal.trap('HUP') do
nohup_log.print("Received HUP, ignoring...\n")
Thread.new { nohup_log.print("Received HUP, ignoring...\n") }.join
end
Signal.trap('TERM') do
nohup_log.print("Received TERM, terminating...\n")
Thread.new { nohup_log.print("Received TERM, terminating...\n") }.join
exit(-1)
end
myself = File.expand_path($PROGRAM_NAME)
Expand Down Expand Up @@ -485,6 +492,17 @@ def oom_limit
512
end

# Log facility for nohup
class NohupLogRotate
def initialize(file, rotation_age = 'daily')
@logger = Logger.new(file, rotation_age)
end

def print(data)
@logger << data
end
end

# Log facility for nohup
class NohupLog
def initialize(file, max)
Expand Down

0 comments on commit b56aaea

Please sign in to comment.