<commit_before>/***************************************************************
 *
 * Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
 * University of Wisconsin-Madison, WI.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************/


#include "condor_common.h"

#include "script.h"
#include "util.h"
#include "job.h"

#include "dag.h"

ScriptQ::ScriptQ( Dag* dag ) : 
	_scriptDeferredCount	(0)
{
	_dag = dag;
	_numScriptsRunning = 0;

    _scriptPidTable = new HashTable<int,Script*>( &hashFuncInt );
    _waitingQueue = new std::queue<Script*>();

    if( _scriptPidTable == NULL || _waitingQueue == NULL ) {
        EXCEPT( "ERROR: out of memory!");
    }

	// register daemonCore reaper for PRE/POST/HOLD script completion
    _scriptReaperId =
		daemonCore->Register_Reaper( "PRE/POST/HOLD Script Reaper",
									 (ReaperHandlercpp)&ScriptQ::ScriptReaper,
									 "ScriptQ::ScriptReaper", this );
}

ScriptQ::~ScriptQ()
{
	// should we un-register the daemonCore reaper here?
	delete _scriptPidTable;
	delete _waitingQueue;
};

// run script if possible, otherwise insert it into the waiting queue
int
ScriptQ::Run( Script *script )
{
	//TEMP -- should ScriptQ object know whether it's pre or post?
	const char *prefix = script->GetScriptName();

	bool deferScript = false;

		// Defer PRE scripts if the DAG is halted (we need to go ahead
		// and run POST scripts so we don't "waste" the fact that the
		// job completed).  (Allow PRE script for final node, though.)
	if ( _dag->IsHalted() && ( script->_type == ScriptType::PRE ) &&
				!( script->GetNode()->GetType() == NodeType::FINAL ) ) {
		debug_printf( DEBUG_DEBUG_1,
					"Deferring %s script of node %s because DAG is halted\n",
					prefix, script->GetNodeName() );
		deferScript = true;
	}

		// Defer the script if we've hit the max PRE/POST scripts
		// running limit.
		//TEMP -- the scriptQ object should really know the max scripts
		// limit, instead of getting it from the Dag object.  wenger 2015-03-18
	int maxScripts = 20; // TODO: Use better default value?
	switch( script->_type ) {
		case ScriptType::PRE: maxScripts = _dag->_maxPreScripts;
		case ScriptType::POST: maxScripts = _dag->_maxPostScripts;
		case ScriptType::HOLD: maxScripts = _dag->_maxHoldScripts;
	}
	if ( maxScripts != 0 && _numScriptsRunning >= maxScripts ) {
			// max scripts already running
		debug_printf( DEBUG_DEBUG_1, "Max %s scripts (%d) already running; "
					  "deferring %s script of Job %s\n", prefix, maxScripts,
					  prefix, script->GetNodeName() );
		deferScript = true;
	}
	if ( deferScript ) {<commit_msg>Fixed bug in max running scripts logic (#7781)<commit_after>/***************************************************************
 *
 * Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
 * University of Wisconsin-Madison, WI.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************/


#include "condor_common.h"

#include "script.h"
#include "util.h"
#include "job.h"

#include "dag.h"

ScriptQ::ScriptQ( Dag* dag ) : 
	_scriptDeferredCount	(0)
{
	_dag = dag;
	_numScriptsRunning = 0;

    _scriptPidTable = new HashTable<int,Script*>( &hashFuncInt );
    _waitingQueue = new std::queue<Script*>();

    if( _scriptPidTable == NULL || _waitingQueue == NULL ) {
        EXCEPT( "ERROR: out of memory!");
    }

	// register daemonCore reaper for PRE/POST/HOLD script completion
    _scriptReaperId =
		daemonCore->Register_Reaper( "PRE/POST/HOLD Script Reaper",
									 (ReaperHandlercpp)&ScriptQ::ScriptReaper,
									 "ScriptQ::ScriptReaper", this );
}

ScriptQ::~ScriptQ()
{
	// should we un-register the daemonCore reaper here?
	delete _scriptPidTable;
	delete _waitingQueue;
};

// run script if possible, otherwise insert it into the waiting queue
int
ScriptQ::Run( Script *script )
{
	//TEMP -- should ScriptQ object know whether it's pre or post?
	const char *prefix = script->GetScriptName();

	bool deferScript = false;

		// Defer PRE scripts if the DAG is halted (we need to go ahead
		// and run POST scripts so we don't "waste" the fact that the
		// job completed).  (Allow PRE script for final node, though.)
	if ( _dag->IsHalted() && ( script->_type == ScriptType::PRE ) &&
				!( script->GetNode()->GetType() == NodeType::FINAL ) ) {
		debug_printf( DEBUG_DEBUG_1,
					"Deferring %s script of node %s because DAG is halted\n",
					prefix, script->GetNodeName() );
		deferScript = true;
	}

		// Defer the script if we've hit the max PRE/POST scripts
		// running limit.
		//TEMP -- the scriptQ object should really know the max scripts
		// limit, instead of getting it from the Dag object.  wenger 2015-03-18
	int maxScripts = 20; // TODO: Use better default value?
	switch( script->_type ) {
		case ScriptType::PRE: maxScripts = _dag->_maxPreScripts; break;
		case ScriptType::POST: maxScripts = _dag->_maxPostScripts; break;
		case ScriptType::HOLD: maxScripts = _dag->_maxHoldScripts; break;
	}
	if ( maxScripts != 0 && _numScriptsRunning >= maxScripts ) {
			// max scripts already running
		debug_printf( DEBUG_DEBUG_1, "Max %s scripts (%d) already running; "
					  "deferring %s script of Job %s\n", prefix, maxScripts,
					  prefix, script->GetNodeName() );
		deferScript = true;
	}
	if ( deferScript ) {
{'fraction_non_alphanumeric': 0.1486232790988736, 'fraction_numerical': 0.009073842302878598, 'mean_word_length': 3.8103837471783297, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2204065', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1885, 'n_words': 666}