<commit_before># Copyright (C) 2008-2010, <NAME>.

package Mojo::Server::Daemon;

use strict;
use warnings;

use base 'Mojo::Server';
use bytes;

use Carp 'croak';
use Fcntl ':flock';
use File::Spec;
use IO::File;
use Mojo::IOLoop;
use Mojo::Transaction::Pipeline;
use Scalar::Util qw/isweak weaken/;
use Sys::Hostname 'hostname';

__PACKAGE__->attr([qw/group listen listen_queue_size user/]);
__PACKAGE__->attr(ioloop => sub { Mojo::IOLoop->singleton });
__PACKAGE__->attr(keep_alive_timeout => 15);
__PACKAGE__->attr(
    lock_file => sub {
        return File::Spec->catfile(File::Spec->splitdir(File::Spec->tmpdir),
            'mojo_daemon.lock');
    }
);
__PACKAGE__->attr(max_clients             => 1000);
__PACKAGE__->attr(max_keep_alive_requests => 100);
__PACKAGE__->attr(
    pid_file => sub {
        return File::Spec->catfile(File::Spec->splitdir(File::Spec->tmpdir),
            'mojo_daemon.pid');
    }
);

__PACKAGE__->attr(_connections => sub { {} });
__PACKAGE__->attr('_lock');

sub accept_lock {
    my ($self, $blocking) = @_;

    # Lock
    my $flags = $blocking ? LOCK_EX : LOCK_EX | LOCK_NB;
    my $lock = flock($self->_lock, $flags);

    return $lock;
}

sub accept_unlock { flock(shift->_lock, LOCK_UN) }

sub prepare_ioloop {
    my $self = shift;

    # Lock callback
    $self->ioloop->lock_cb(sub { $self->accept_lock($_[1]) });

    # Unlock callback
    $self->ioloop->unlock_cb(sub { $self->accept_unlock });

    # Listen
    my $listen = $self->listen || 'http:*:3000';
    $self->_listen($_) for split ',', $listen;

    # Max clients
    $self->ioloop->max_connections($self->max_clients);

    # Stop ioloop on HUP signal
    $SIG{HUP} = sub { $self->ioloop->stop };
}

sub prepare_lock_file {
    my $self = shift;

    my $file = $self->lock_file;

    # Create lock file
    my $fh = IO::File->new("> $file")
      or croak qq/Can't open lock file "$file"/;
    $self->_lock($fh);
}

sub prepare_pid_file {
    my $self = shift;

    my $file = $self->pid_file;

    # PID file
    my $fh;
    if (-e $file) {
        $fh = IO::File->new("< $file")
          or croak qq/Can't open PID file "$file": $!/;
        my $pid = <$fh>;<commit_msg>fixed daemon default port<commit_after># Copyright (C) 2008-2010, <NAME>.

package Mojo::Server::Daemon;

use strict;
use warnings;

use base 'Mojo::Server';
use bytes;

use Carp 'croak';
use Fcntl ':flock';
use File::Spec;
use IO::File;
use Mojo::IOLoop;
use Mojo::Transaction::Pipeline;
use Scalar::Util qw/isweak weaken/;
use Sys::Hostname 'hostname';

__PACKAGE__->attr([qw/group listen listen_queue_size user/]);
__PACKAGE__->attr(ioloop => sub { Mojo::IOLoop->singleton });
__PACKAGE__->attr(keep_alive_timeout => 15);
__PACKAGE__->attr(
    lock_file => sub {
        return File::Spec->catfile(File::Spec->splitdir(File::Spec->tmpdir),
            'mojo_daemon.lock');
    }
);
__PACKAGE__->attr(max_clients             => 1000);
__PACKAGE__->attr(max_keep_alive_requests => 100);
__PACKAGE__->attr(
    pid_file => sub {
        return File::Spec->catfile(File::Spec->splitdir(File::Spec->tmpdir),
            'mojo_daemon.pid');
    }
);

__PACKAGE__->attr(_connections => sub { {} });
__PACKAGE__->attr('_lock');

sub accept_lock {
    my ($self, $blocking) = @_;

    # Lock
    my $flags = $blocking ? LOCK_EX : LOCK_EX | LOCK_NB;
    my $lock = flock($self->_lock, $flags);

    return $lock;
}

sub accept_unlock { flock(shift->_lock, LOCK_UN) }

sub prepare_ioloop {
    my $self = shift;

    # Lock callback
    $self->ioloop->lock_cb(sub { $self->accept_lock($_[1]) });

    # Unlock callback
    $self->ioloop->unlock_cb(sub { $self->accept_unlock });

    # Listen
    my $listen = $self->listen || 'http://*:3000';
    $self->_listen($_) for split ',', $listen;

    # Max clients
    $self->ioloop->max_connections($self->max_clients);

    # Stop ioloop on HUP signal
    $SIG{HUP} = sub { $self->ioloop->stop };
}

sub prepare_lock_file {
    my $self = shift;

    my $file = $self->lock_file;

    # Create lock file
    my $fh = IO::File->new("> $file")
      or croak qq/Can't open lock file "$file"/;
    $self->_lock($fh);
}

sub prepare_pid_file {
    my $self = shift;

    my $file = $self->pid_file;

    # PID file
    my $fh;
    if (-e $file) {
        $fh = IO::File->new("< $file")
          or croak qq/Can't open PID file "$file": $!/;
        my $pid = <$fh>;
{'fraction_non_alphanumeric': 0.1763097949886105, 'fraction_numerical': 0.010022779043280182, 'mean_word_length': 3.2714007782101167, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6855050', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1582, 'n_words': 344}