<commit_before>package contractor

import (
	"github.com/NebulousLabs/Sia/modules"
	"github.com/NebulousLabs/Sia/types"
)

// managedArchiveContracts will figure out which contracts are no longer needed
// and move them to the historic set of contracts.
//
// TODO: This function should be performed by threadedContractMaintenance.
// threadedContractMaintenance will currently quit if there are no hosts, but it
// should at least run this code before quitting.
func (c *Contractor) managedArchiveContracts() {
	// Determine the current block height.
	c.mu.RLock()
	currentHeight := c.blockHeight
	c.mu.RUnlock()

	// Loop through the current set of contracts and migrate any expired ones to
	// the set of old contracts.
	for _, contract := range c.contracts.ViewAll() {
		if currentHeight > contract.EndHeight() {
			id := contract.ID
			c.mu.Lock()
			c.oldContracts[id] = contract
			c.mu.Unlock()
			if contract, ok := c.contracts.Acquire(id); ok {
				c.contracts.Delete(contract)
			}
			c.log.Println("INFO: archived expired contract", id)
		}
	}

	// Save.<commit_msg>resolve unclean shutdown in contractor<commit_after>package contractor

import (
	"github.com/NebulousLabs/Sia/modules"
	"github.com/NebulousLabs/Sia/types"
)

// managedArchiveContracts will figure out which contracts are no longer needed
// and move them to the historic set of contracts.
//
// TODO: This function should be performed by threadedContractMaintenance.
// threadedContractMaintenance will currently quit if there are no hosts, but it
// should at least run this code before quitting.
func (c *Contractor) managedArchiveContracts() {
	err := c.tg.Add()
	if err != nil {
		return
	}
	defer c.tg.Done()

	// Determine the current block height.
	c.mu.RLock()
	currentHeight := c.blockHeight
	c.mu.RUnlock()

	// Loop through the current set of contracts and migrate any expired ones to
	// the set of old contracts.
	for _, contract := range c.contracts.ViewAll() {
		if currentHeight > contract.EndHeight() {
			id := contract.ID
			c.mu.Lock()
			c.oldContracts[id] = contract
			c.mu.Unlock()
			if contract, ok := c.contracts.Acquire(id); ok {
				c.contracts.Delete(contract)
			}
			c.log.Println("INFO: archived expired contract", id)
		}
	}

	// Save.
{'fraction_non_alphanumeric': 0.10760337927967986, 'fraction_numerical': 0.0, 'mean_word_length': 3.610655737704918, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2537906', 'n_tokens_mistral': 755, 'n_tokens_neox': 688, 'n_words': 225}