<commit_before>exports.handler = function( event, context, callback ) {
	console.log( "Received request:\n", JSON.stringify( event ) );

	var spotFleetRequestID = event.SpotFleetRequestID;
	if(! spotFleetRequestID) {
		console.log( "Failed to find Spot Fleet request ID." );
		callback( "Failed to find Spot Fleet request ID." );
		return;
	}
	console.log( "Found Spot Fleet request ID: " + spotFleetRequestID );

	var ruleID = event.RuleID;
	if(! ruleID) {
		console.log( "Failed to find rule ID." );
		callback( "Failed to find rule ID." );
		return;
	}
	console.log( "Found rule ID: " + ruleID );

	var targetID = event.TargetID;
	if(! targetID) {
		console.log( "Failed to find target ID." );
		callback( "Failed to find target ID." );
		return;
	}
	console.log( "Found target ID: " + targetID );

	var leaseExpiration = event.LeaseExpiration;
	if(! leaseExpiration) {
		console.log( "Failed to find lease expiration." );
		callback( "Failed to find lease expiration." );
		return;
	}
	console.log( "Found lease expiration: " + leaseExpiration );

	var timestamp = Math.floor( Date.now() / 1000 );
	if( timestamp <= leaseExpiration ) {
		console.log( "Lease has not expired yet (" + timestamp + " <= " + leaseExpiration + ")." )
		callback( null );
		return;
	}
	console.log( "Lease has expired: " + timestamp + " > " + leaseExpiration + "." )

	var AWS = require( 'aws-sdk' );
	var ec2 = new AWS.EC2();

	// Assumes we have fewer than 1000 SFRs.
	var p = { };
	ec2.describeSpotFleetRequests( p, function( err, data ) {
		var spotFleetRequestIDs = [];

		if( err ) {
			console.log( err, err.stack );
			callback( err, err.stack );
		} else {
			for( var i = 0; i < data.SpotFleetRequestConfigs.length; ++i ) {
				var config = data.SpotFleetRequestConfigs[i];
				var sfrc = config.SpotFleetRequestConfig;
				if( sfrc.ClientToken.startsWith( spotFleetRequestID ) ) {
					// spotFleetRequestID = config.SpotFleetRequestId;
					spotFleetRequestIDs.push( config.SpotFleetRequestId );
					break;
				}
			}


	var params = {
		// SpotFleetRequestIds : [ spotFleetRequestID ],
		SpotFleetRequestIds : spotFleetRequestIDs,
		TerminateInstances : true
	};
	ec2.cancelSpotFleetRequests( params, function( err, data ) {
		// cancelSpotFleetRequests() always succeeds.  *sigh*
		if( data.UnsuccessfulFleetRequests.length != 0 ) {
			console.log( "Failed to cancel Spot Fleet request." );
			callback( "Failed to cancel Spot Fleet request.", data );
		} else {
			console.log( "Succesfully cancelled Spot Fleet request." );

			var cwe = new AWS.CloudWatchEvents();
			var params = {
				Rule : ruleID,
				Ids : [ targetID ]
			};<commit_msg>Allow the SFR lease function to delete more than one matching SFR.<commit_after>exports.handler = function( event, context, callback ) {
	console.log( "Received request:\n", JSON.stringify( event ) );

	var spotFleetRequestID = event.SpotFleetRequestID;
	if(! spotFleetRequestID) {
		console.log( "Failed to find Spot Fleet request ID." );
		callback( "Failed to find Spot Fleet request ID." );
		return;
	}
	console.log( "Found Spot Fleet request ID: " + spotFleetRequestID );

	var ruleID = event.RuleID;
	if(! ruleID) {
		console.log( "Failed to find rule ID." );
		callback( "Failed to find rule ID." );
		return;
	}
	console.log( "Found rule ID: " + ruleID );

	var targetID = event.TargetID;
	if(! targetID) {
		console.log( "Failed to find target ID." );
		callback( "Failed to find target ID." );
		return;
	}
	console.log( "Found target ID: " + targetID );

	var leaseExpiration = event.LeaseExpiration;
	if(! leaseExpiration) {
		console.log( "Failed to find lease expiration." );
		callback( "Failed to find lease expiration." );
		return;
	}
	console.log( "Found lease expiration: " + leaseExpiration );

	var timestamp = Math.floor( Date.now() / 1000 );
	if( timestamp <= leaseExpiration ) {
		console.log( "Lease has not expired yet (" + timestamp + " <= " + leaseExpiration + ")." )
		callback( null );
		return;
	}
	console.log( "Lease has expired: " + timestamp + " > " + leaseExpiration + "." )

	var AWS = require( 'aws-sdk' );
	var ec2 = new AWS.EC2();

	// Assumes we have fewer than 1000 SFRs.
	var p = { };
	ec2.describeSpotFleetRequests( p, function( err, data ) {
		var spotFleetRequestIDs = [];

		if( err ) {
			console.log( err, err.stack );
			callback( err, err.stack );
		} else {
			for( var i = 0; i < data.SpotFleetRequestConfigs.length; ++i ) {
				var config = data.SpotFleetRequestConfigs[i];
				var sfrc = config.SpotFleetRequestConfig;
				if( sfrc.ClientToken.startsWith( spotFleetRequestID ) ) {
					spotFleetRequestIDs.push( config.SpotFleetRequestId );
				}
			}


	var params = {
		// SpotFleetRequestIds : [ spotFleetRequestID ],
		SpotFleetRequestIds : spotFleetRequestIDs,
		TerminateInstances : true
	};
	ec2.cancelSpotFleetRequests( params, function( err, data ) {
		// cancelSpotFleetRequests() always succeeds.  *sigh*
		if( data.UnsuccessfulFleetRequests.length != 0 ) {
			console.log( "Failed to cancel Spot Fleet request." );
			callback( "Failed to cancel Spot Fleet request.", data );
		} else {
			console.log( "Succesfully cancelled Spot Fleet request." );

			var cwe = new AWS.CloudWatchEvents();
			var params = {
				Rule : ruleID,
				Ids : [ targetID ]
			};
{'fraction_non_alphanumeric': 0.13233339662046706, 'fraction_numerical': 0.005316119232959939, 'mean_word_length': 3.521888412017167, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '6248943', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1746, 'n_words': 486}