Fine tune migration concurrency

Fix migration concurrency so that it goes in an orderly process.  This ensures that big migrations will not crash.
This commit is contained in:
supervj 2021-03-12 16:47:24 -05:00
parent a99faad77c
commit 9b63457ae1
2 changed files with 21 additions and 11 deletions

View file

@ -6,8 +6,9 @@ export const migrateWorld = async function() {
ui.notifications.info(`Applying SW5e System Migration for version ${game.system.data.version}. Please be patient and do not close your game or shut down your server.`, {permanent: true}); ui.notifications.info(`Applying SW5e System Migration for version ${game.system.data.version}. Please be patient and do not close your game or shut down your server.`, {permanent: true});
// Migrate World Actors // Migrate World Actors
for ( let a of game.actors.entities ) { for await ( let a of game.actors.entities ) {
try { try {
console.log(`Checking Actor entity ${a.name} for migration needs`);
const updateData = await migrateActorData(a.data); const updateData = await migrateActorData(a.data);
if ( !isObjectEmpty(updateData) ) { if ( !isObjectEmpty(updateData) ) {
console.log(`Migrating Actor entity ${a.name}`); console.log(`Migrating Actor entity ${a.name}`);
@ -79,7 +80,7 @@ export const migrateCompendium = async function(pack) {
const content = await pack.getContent(); const content = await pack.getContent();
// Iterate over compendium entries - applying fine-tuned migration functions // Iterate over compendium entries - applying fine-tuned migration functions
for ( let ent of content ) { for await ( let ent of content ) {
let updateData = {}; let updateData = {};
try { try {
switch (entity) { switch (entity) {
@ -133,7 +134,8 @@ export const migrateActorData = async function(actor) {
// Migrate Owned Items // Migrate Owned Items
if ( !!actor.items ) { if ( !!actor.items ) {
let hasItemUpdates = false; let hasItemUpdates = false;
const items = await Promise.all(actor.items.map(async (i) => { const items = await actor.items.reduce(async (memo, i) => {
const results = await memo;
// Migrate the Owned Item // Migrate the Owned Item
let itemUpdate = await migrateActorItemData(i, actor); let itemUpdate = await migrateActorItemData(i, actor);
@ -148,9 +150,11 @@ export const migrateActorData = async function(actor) {
// Update the Owned Item // Update the Owned Item
if ( !isObjectEmpty(itemUpdate) ) { if ( !isObjectEmpty(itemUpdate) ) {
hasItemUpdates = true; hasItemUpdates = true;
return mergeObject(i, itemUpdate, {enforceTypes: false, inplace: false}); console.log(`Migrating Actor ${actor.name}'s ${i.name}`);
} else return i; return [...results, mergeObject(i, itemUpdate, {enforceTypes: false, inplace: false})];
})); } else return [...results, i];
}, []);
if ( hasItemUpdates ) updateData.items = items; if ( hasItemUpdates ) updateData.items = items;
} }
@ -229,7 +233,7 @@ export const migrateActorItemData = async function(item, actor) {
* @param {Object} scene The Scene data to Update * @param {Object} scene The Scene data to Update
* @return {Object} The updateData to apply * @return {Object} The updateData to apply
*/ */
export const migrateSceneData = async function(scene) { export const migrateSceneData = async function(scene) {
const tokens = duplicate(scene.tokens); const tokens = duplicate(scene.tokens);
return { return {
tokens: await Promise.all(tokens.map(async (t) => { tokens: await Promise.all(tokens.map(async (t) => {
@ -483,6 +487,7 @@ async function _migrateItemPower(item, actor, updateData) {
// if item is not a power shortcut out // if item is not a power shortcut out
if (item.type !== "power") return updateData; if (item.type !== "power") return updateData;
console.log(`Checking Actor ${actor.name}'s ${item.name} for migration needs`);
// check for flag.core, if not there is no compendium power so exit // check for flag.core, if not there is no compendium power so exit
const hasSource = item?.flags?.core?.sourceId !== undefined; const hasSource = item?.flags?.core?.sourceId !== undefined;
if (!hasSource) return updateData; if (!hasSource) return updateData;
@ -502,14 +507,19 @@ async function _migrateItemPower(item, actor, updateData) {
if (coreSource === "Compendium.sw5e.techpowers") powerType = "sw5e.techpowers"; if (coreSource === "Compendium.sw5e.techpowers") powerType = "sw5e.techpowers";
if (powerType === "none") return updateData; if (powerType === "none") return updateData;
game.packs.get(powerType).getEntity(core_id).then(corePower => { const corePower = duplicate(await game.packs.get(powerType).getEntity(core_id));
const corePowerData = corePower.data.data; console.log(`Updating Actor ${actor.name}'s ${item.name} from compendium`);
const corePowerData = corePower.data;
// copy Core Power Data over original Power // copy Core Power Data over original Power
updateData["data"] = corePowerData; updateData["data"] = corePowerData;
updateData["flags"] = {"sw5e": {"dataVersion": "1.2.4"}}; updateData["flags"] = {"sw5e": {"dataVersion": "1.2.4"}};
return updateData; return updateData;
})
//game.packs.get(powerType).getEntity(core_id).then(corePower => {
//})
} }
/* -------------------------------------------- */ /* -------------------------------------------- */

View file

@ -196,7 +196,7 @@ Hooks.once("ready", function() {
// Determine whether a system migration is required and feasible // Determine whether a system migration is required and feasible
if ( !game.user.isGM ) return; if ( !game.user.isGM ) return;
const currentVersion = game.settings.get("sw5e", "systemMigrationVersion"); const currentVersion = game.settings.get("sw5e", "systemMigrationVersion");
const NEEDS_MIGRATION_VERSION = "1.2.4.R1-A4"; const NEEDS_MIGRATION_VERSION = "1.2.4.R1-A5";
// Check for R1 SW5E versions // Check for R1 SW5E versions
const SW5E_NEEDS_MIGRATION_VERSION = "R1-A5"; const SW5E_NEEDS_MIGRATION_VERSION = "R1-A5";
const COMPATIBLE_MIGRATION_VERSION = 0.80; const COMPATIBLE_MIGRATION_VERSION = 0.80;