ETLp

 view release on metacpan or  search on metacpan

lib/ETLp/Audit/Browser/Controller/Schedule.pm  view on Meta::CPAN


# Save the schedule
sub save {
    my $self = shift;

    # validate user input
    my ($results, $err_page) = $self->check_rm('edit', '_schedule_profile');
    return $err_page if ($err_page);

    my $q = $results->valid;

    my $crontab_file =
        $self->conf->param('crontab_publish_dir') . '/'
      . $self->conf->param('crontab_owner');
    my $pipeline_script = $self->conf->param('pipeline_script');

    if ($q->{'delete'}) {
        $self->model->delete($q->{schedule_id});
        $self->model->publish_crontab(
            {
                crontab_file    => $crontab_file,
                pipeline_script => $pipeline_script
            }
        );
        return $self->redirect($self->conf->param('root_url') . '/schedule');
    }

    # We've concatenated the config file name to the section name to ensure
    # that the section name is unique across config files (so that the
    # Javascript onChange event would fire if an Ajax call updated a section
    # with the same section name from a different config). However, we now need
    # to strip the config_file off again
    my $config_file = $q->{config_file};
    if ($q->{section} =~ /^$config_file\-(.*)$/) {
        $q->{section} = $1;
    }

    $q->{user_id} = $self->session->param('user_id');

    $self->logger->debug(Dumper($q));
    my $schedule_id = $self->model->save($q);

    $self->logger->debug("Crontab file: $crontab_file");
    $self->logger->debug("Pipeline script: $pipeline_script");

    #$self->model->publish_crontab(
    #    {crontab_file => $crontab_file, pipeline_script => $pipeline_script});

    if ($q->{schedule_id}) {
        $self->session->param('message', 'Schedule Updated');
    } else {
        $self->session->param('message', 'Schedule Created');
    }

    return $self->redirect(
        $self->conf->param('root_url') . '/schedule/edit/' . $schedule_id);
}

# Process ajax call to update the section list drop down if the config item
# changes
sub update_sections {
    my $self        = shift;
    my $config_file = $self->query->param('config_file');
    my $show_blank  = $self->query->param('show_blank') || 0;
    
    return $self->model->get_section_options(
        {
            app_conf_dir => $self->conf->param('app_conf_dir'),
            config_file  => $config_file,
            show_blank   => $show_blank
        }
    );
}

# Return the crontab from the database
sub view_crontab {
    my $self = shift;
    my $crontab =
      $self->model->generate_crontab($self->conf->param('pipeline_script'));
    return $self->tt_process({crontab => $crontab});
}

# Disable the entire crontab
sub disable_crontabe {
    my $self = shift;
    return $self->_set_crontab_status('disabled');
}

# Enable the entire crontab
sub enable_crontab  {
    my $self = shift;
    return $self->_set_crontab_status('enabled');
}

sub _set_crontab_status {
    my $self   = shift;
    my $status = shift;

    my $crontab_file =
        $self->conf->param('crontab_publish_dir') . '/'
      . $self->conf->param('crontab_owner');

    my $pipeline_script = $self->conf->param('pipeline_script');

    # save the status to the database
    $self->model->set_scheduler_status($status);

    $self->logger->debug("Publishing the crontab");
    # Publish the crontab now that the status has changed
    $self->model->publish_crontab(
        {crontab_file => $crontab_file, pipeline_script => $pipeline_script});

    return $self->redirect($self->conf->param('root_url') . '/schedule/');
}

# This runmode is used to return the chain of pipelines dependent on the one
# provided
sub update_dependencies {
    my $self        = shift;
    my $config_file = $self->query->param('config');
    my $section     = $self->query->param('section');

    if ($section =~ /^$config_file\-(.*)$/) {
        $section = $1;
    }

    return join(
        ' -> ',
        $self->model->get_dependencies(
            #{
                config_dir  => $self->conf->param('app_conf_dir'),
                config_file => $config_file,
                section     => $section
            #}
        )
    );
}

sub module {
    return 'Schedules';
}

# This is a validation profile that checks the user inut for a schedule.
# Refer to http://search.cpan.org/dist/Data-FormValidator for documentation
# of its syntax
sub _schedule_profile {
    my $self = shift;
    return {
        required => [qw/schedule_description config_file section/],
        optional => [
            qw/schedule_comment dow_id schedule_hours schedule_minutes status
              schedule_dom_id month_id schedule_doms schedule_id delete/
        ],
        constraints => {
            dow_id => sub {
                return $self->model->validate_days_of_week(shift);
            },
            schedule_hours => sub {
                return $self->model->validate_hours(shift);
            },
            schedule_minutes => sub {
                return $self->model->validate_minutes(shift);
            },
            status          => qr/^1$/,
            schedule_dom_id => sub {
                return $self->model->validate_days_of_month(shift);
            },
            schedule_doms => sub {
                return $self->model->validate_days_of_month(shift);
            },
            schedule_id => qr/^\d+$/,
            month_id    => qr/^\d+$/,
            delete      => qr/^Delete$/,
            config_file => [
                # Check that the config file exists
                {
                    name       => 'no_config',
                    constraint => sub {



( run in 1.783 second using v1.01-cache-2.11-cpan-d7f47b0818f )