<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package TaskManager::Task::Generic;

use strict;
use warnings;

use Common::Config;

use TaskManager::Constants;
use TaskManager::DB;
use TaskManager::System;

#our $CFG;

#our @ISA = qw(Exporter);
#our @EXPORT = qw(
#	$CFG
#);


sub new {
	my $class = shift || die "Bad method call!";
	my $self = {
		'table'			=&gt; undef,
		'server_params'	=&gt; undef,
	};

	bless($self, $class);
	return $self;
}


## it should be in Task/Generic and Task/Crontab, Task/Queue
sub load_config {
	my $self = shift or die "Bad method call!";
	my $cfg_file = shift || die "No config file given!";

	$CFG = readConfig($cfg_file) or die "Can't read config file $cfg_file: $!";

	defined($CFG-&gt;{$_}) || die "Can't find $_ key in config!\n"
		for(qw(DB_HOST DB_NAME DB_USER DB_PASSWORD));

	die "Server id (code) should be defined!"
		unless $CFG-&gt;{'SERVER_CODE'};

	$CFG-&gt;{'MAX_CHILD_PROCESS'} = 5
		unless $CFG-&gt;{'MAX_CHILD_PROCESS'};

#	$CFG-&gt;{'MUTEX_GROUPS'} = 'default'
#		unless defined $CFG-&gt;{'MUTEX_GROUPS'};
#
#	$CFG-&gt;{'__MUTEX_GROUPS'} = [split(/[ ,;]/,$CFG-&gt;{'MUTEX_GROUPS'})];

	$CFG-&gt;{'NEXT_ATTEMPT_TIMEOUT'} = 60
		unless defined $CFG-&gt;{'NEXT_ATTEMPT_TIMEOUT'};

	$CFG-&gt;{'NEXT_TASK_TIMEOUT'} = 1
		unless defined $CFG-&gt;{'NEXT_TASK_TIMEOUT'};

	$CFG-&gt;{'DEBUG_LEVEL'} = 'LOGFILE'
		unless defined $CFG-&gt;{'DEBUG_LEVEL'};

	$CFG-&gt;{'__DEBUG_LEVEL'} = { map { $_ =&gt; 1 } (split /[ ,;]/, $CFG-&gt;{'DEBUG_LEVEL'}) };

	$CFG-&gt;{'DAEMONIZE'} = 1
		unless defined $CFG-&gt;{'DAEMONIZE'};
	$CFG-&gt;{'DAEMONIZE'} = 0
		if( $CFG-&gt;{'DAEMONIZE'} =~ /^(?:0|off|no)$/i );

	$CFG-&gt;{'DB_CONNECTION_ATTEMPTS'} = 3
		unless defined $CFG-&gt;{'DB_CONNECTION_ATTEMPTS'};

	$CFG-&gt;{'DB_CONNECTION_INTERVAL'} = 1
		unless defined $CFG-&gt;{'DB_CONNECTION_INTERVAL'};

	$CFG-&gt;{'RESERVED_HIGH_PRIORITY_PROCESS'} = 0
		unless defined $CFG-&gt;{'RESERVED_HIGH_PRIORITY_PROCESS'};

	$CFG-&gt;{'NEXT_LOADAVG_UPDATE_TIMEOUT'} = 2
		unless defined $CFG-&gt;{'NEXT_LOADAVG_UPDATE_TIMEOUT'};

	$CFG-&gt;{'IS_ALIVE_PING_TIMEOUT'} = 20
		unless defined $CFG-&gt;{'IS_ALIVE_PING_TIMEOUT'};

	$CFG-&gt;{'SET_TASK_UID'} = 1
		unless defined $CFG-&gt;{'SET_TASK_UID'};
	$CFG-&gt;{'SET_TASK_UID'} = 0
		if( $CFG-&gt;{'SET_TASK_UID'} =~ /^(?:0|off|no)$/i );

	$CFG-&gt;{'DB_LOGFILE'} = ''
		unless defined $CFG-&gt;{'DB_LOGFILE'};

	return undef;
}


sub set_worker_pid {
	my $self = shift or die "Bad method call!";
	my $id_task = shift || die "No pid given!";

	TaskManager::DB::do_query(
		"UPDATE $self-&gt;{table} SET worker_status=?, worker_pid=?, id_worker_server=? WHERE id=?",
		TM_STATUS_WORK, $$, $CFG-&gt;{'SERVER_ID'}, $id_task
	)
		or make_die($TaskManager::DB::error);

	return undef;
}


sub set_task_done {
	my $self = shift or die "Bad method call!";
	my $id_task = shift or die "No task id given!";
	my $tm_status = shift;
	my $output = shift;

	TaskManager::DB::do_query(
		"UPDATE $self-&gt;{table} SET worker_status=?, dt_done=NOW(), attempt_no=attempt_no+1, exit_code=?, output=? WHERE id=?",
		TM_STATUS_DONE, $tm_status, $output, $id_task
	)
		or make_die($TaskManager::DB::error);

	return undef;
}


sub set_task_error {
	my $self = shift or die "Bad method call!";
	my $id_task = shift or die "No task id given!";
	my $signal = shift;# or die "No signal given";
	my $error = shift;

	my $SQL_chunk = '';
	my @params = (TM_STATUS_FAIL, $signal);
	if( $error ne 'died' ) {
		push @params, $error;
		$SQL_chunk = 'output=?,';
	}

	TaskManager::DB::do_query(
		"UPDATE $self-&gt;{table} SET worker_status=?, exit_code=?, $SQL_chunk attempt_no=attempt_no+1 WHERE id=?",
		@params, $id_task
	)
		or make_die($TaskManager::DB::error);

	return undef;
}


sub parse_exec_str {
	my $self = shift or die "Bad method call!";
	my $exec_str = shift or die "No str given!";

	chomp $exec_str;

	if( $exec_str =~ /\$([0-9a-zA-Z_]+)\$/ ) {
		my $key = $1;
		die "No $key var found in \%ENV!"
			unless defined $ENV{$key};

		my $val = $ENV{$key};

		$exec_str =~ s/\$$key\$/$val/g;
	}

	return $exec_str;
}


sub validate_exec_str {
	my $self = shift or die "Bad method call!";
	my $task_exec_str = shift or die "!";

	return undef
		if( index($task_exec_str, '&amp;&amp;')!=-1 || index($task_exec_str, ';')!=-1 );

	return 1;
}


sub startup_check_server {
	my $self = shift || die "Bad method call!";

	my $REZ = TaskManager::DB::select_query(
		"SELECT * FROM server_list WHERE code=?",
		$CFG-&gt;{'SERVER_CODE'}
	);

	make_die("No server found by code '$CFG-&gt;{SERVER_CODE}'!")
		unless scalar @$REZ;

	make_die("Collision detected! More than one server found!")
		if ( scalar(@$REZ) &gt; 1 );

	my $row = pop @$REZ;

	make_die("Server isn't active!")
		if $row-&gt;{'status'} ne TM_SERVER_STATUS_ACTIVE;

	$self-&gt;{'server_params'} = $row;

	$CFG-&gt;{'SERVER_TYPE'} = $row-&gt;{'limit_type'};
	$CFG-&gt;{'SERVER_ID'} = $row-&gt;{'id'};

	return undef;
}


1;
</pre></body></html>