#=============================================================================
# Doloforge::Service::Cron : prepare cron/(daily,weekly,...) directories
#
# Create the directories in which developers will create cron scripts.
#
# Full help :
#   perldoc Doloforge::Service::Cron
#=============================================================================


package Doloforge::Service::Cron;

use strict;

# This base class implements the constructor and help methods in a generic way.
use base qw(Doloforge::Service);
use File::Path;


#=============================================================================
=encoding utf8

=head1 NAME

Doloforge::Service::Cron : prepare cron/(daily,weekly,...) directories

=head1 SYNOPSIS

    # Update the full configuration, running each client/project/service update
    doloforge config --update

=head1 DESCRIPTION

Todo

=cut


#=============================================================================
# Method finish
#
# Called when the whole processing is finished, when it's time to do one-time things,
# like creating the lock directory...

sub finish {
    my $self = shift;

    # Create the lock directory on the NFS server.
    # This lock dir should belong to user "nobody", which is the ID the client user "root" have when it connects to a root_squash NFS share
    if ($self->has_role('nfs_role')) {
        my $lockdir = ($self->{global}->{stages}->{$self->{global}->{stage}}->{dir}->{shared} || $self->{global}->{dir}->{shared})."/.doloforge/$self->{name}/lock";
        unless (-d $lockdir) {
            # 20190329/NH root@sodium:~# doloforge project --client edition --name admin --update
            #   cron notice: Creating  /.doloforge/cron/lock lock directory, with owner 'nobody'
            $self->message("Creating $self->{global}->{stage} $lockdir lock directory, with owner 'nobody'");
            mkpath($lockdir);
            # Change owner to numeric ID of 'nobody' (which must exist on every system), without changing group
            chown(scalar(getpwnam('nobody')), -1, $lockdir);
        }
    }
}


#=============================================================================
# Method cron_run
#
# Run the regular cron tasks on a per-project basis, on the application servers,
# which is mainly DB backup for every project that uses this RDBMS.

sub cron_run {
    my $self = shift;

    my ($client, $project, $date, $interval) = @_;

    # Run all the jobs in a controled environment...
    $self->run_job($client, $project, "cron/$interval/*", [$date, $interval], undef);
}



1;
__END__

=head1 AUTHOR

Nicolas Huillard <F<nicolas@huillard.net>>, Dolomède

=head1 SEE ALSO

doloforge(1)

=cut
