I’m trying to run a function to clean some teams involving looping over the manager records, updating them and then saving. It works locally. However on live the function doesn’t get to the end, I confirm the following via logs when the function runs:
I have 313 managers to loop over:
Here is the function:
private func cleanManagerTeams(managers : [T.Manager], allPlayers : [DTOs.PlayerDTO]) : async [T.Manager] {
let managerBuffer = Buffer.fromArray<T.Manager>([]);
await logStatus("Cleaning " # Nat.toText(Array.size(managers)) # " teams.");
for (manager in Iter.fromArray(managers)) {
var captainRemoved = false;
let playerIdBuffer = Buffer.fromArray<T.PlayerId>([]);
for (playerId in Iter.fromArray(manager.playerIds)) {
let activePlayer = Array.find<DTOs.PlayerDTO>(allPlayers, func(player: DTOs.PlayerDTO){
player.id == playerId
});
if(Option.isSome(activePlayer)){
playerIdBuffer.add(playerId);
};
if(Option.isNull(activePlayer)){
if(playerId == manager.captainId){
captainRemoved := true;
};
playerIdBuffer.add(0);
};
};
var captainId = manager.captainId;
if(captainRemoved){
let highestValuedPlayer = Array.foldLeft<T.PlayerId, ?DTOs.PlayerDTO>(
Buffer.toArray(playerIdBuffer),
null,
func(highest, id) : ?DTOs.PlayerDTO {
if (id == 0) { return highest };
let player = Array.find<DTOs.PlayerDTO>(allPlayers, func(p) { p.id == id });
switch (highest, player) {
case (null, ?p) {
?p;
};
case (?h, ?p) {
if (p.valueQuarterMillions > h.valueQuarterMillions) {
?p;
} else {
?h;
};
};
case (_, null) {
highest;
};
};
},
);
switch(highestValuedPlayer){
case (?foundPlayer){
captainId := foundPlayer.id;
};
case (null){}
};
};
var updatedPlayerIds = Buffer.toArray(playerIdBuffer);
let allTeamPlayers = Array.filter<DTOs.PlayerDTO>(allPlayers, func(player: DTOs.PlayerDTO){
Array.find<T.PlayerId>(updatedPlayerIds, func(playerId){
playerId == player.id
}) != null;
});
let allPlayerValues = Array.map<DTOs.PlayerDTO, Nat16>(allTeamPlayers, func (player: DTOs.PlayerDTO) : Nat16 { return player.valueQuarterMillions; });
let currentTeamValue = Array.foldLeft<Nat16, Nat16>(allPlayerValues, 0, func(sumSoFar, x) = sumSoFar + x);
var arrstr = "";
for(id in Iter.fromArray(manager.playerIds)){
arrstr := arrstr # Nat16.toText(id) # ",";
};
await logStatus("Team value for " # manager.principalId # "is " # Nat16.toText(currentTeamValue) # ", " # arrstr);
if(currentTeamValue > 1200){
await logStatus("Team " # manager.principalId # " has a team over 300m.");
};
var bankBalance: Nat16 = 0;
var testBalance: Int = 1200 - Int16.toInt(Int16.fromNat16(currentTeamValue));
if(testBalance >= 0){
bankBalance := Nat16.fromIntWrap(testBalance);
};
if(testBalance < 0){
updatedPlayerIds := [];
bankBalance := 1200;
};
let updatedManager : T.Manager = {
principalId = manager.principalId;
username = manager.username;
termsAccepted = manager.termsAccepted;
favouriteClubId = manager.favouriteClubId;
createDate = manager.createDate;
history = manager.history;
profilePicture = manager.profilePicture;
profilePictureType = manager.profilePictureType;
transfersAvailable = manager.transfersAvailable;
monthlyBonusesAvailable = manager.monthlyBonusesAvailable;
bankQuarterMillions = bankBalance;
playerIds = updatedPlayerIds;
captainId = captainId;
goalGetterGameweek = manager.goalGetterGameweek;
goalGetterPlayerId = manager.goalGetterPlayerId;
passMasterGameweek = manager.passMasterGameweek;
passMasterPlayerId = manager.passMasterPlayerId;
noEntryGameweek = manager.noEntryGameweek;
noEntryPlayerId = manager.noEntryPlayerId;
teamBoostGameweek = manager.teamBoostGameweek;
teamBoostClubId = manager.teamBoostClubId;
safeHandsGameweek = manager.safeHandsGameweek;
safeHandsPlayerId = manager.safeHandsPlayerId;
captainFantasticGameweek = manager.captainFantasticGameweek;
captainFantasticPlayerId = manager.captainFantasticPlayerId;
countrymenGameweek = manager.countrymenGameweek;
countrymenCountryId = manager.countrymenCountryId;
prospectsGameweek = manager.prospectsGameweek;
braceBonusGameweek = manager.braceBonusGameweek;
hatTrickHeroGameweek = manager.hatTrickHeroGameweek;
transferWindowGameweek = manager.transferWindowGameweek;
ownedPrivateLeagues = manager.ownedPrivateLeagues;
privateLeagueMemberships = manager.privateLeagueMemberships;
};
managerBuffer.add(updatedManager);
};
return Buffer.toArray(managerBuffer);
};
As you can see I check by logging the team value that it loops through everyone and I get 313 log records:
After this function completes its execution I have another log statement immediately after, which only executes on local, for some reason this function doesn’t complete after the final loop where it logs the team value.
The complete file is here:
Any help appreciated.