Skip to content

Commit

Permalink
fix: Can't list deployments (#2701)
Browse files Browse the repository at this point in the history
* fix: Fixed the issue with list deployments:

- Created a migration function and modified the moduleName to be 'Fullvm' to list all old VMs with 'Fullvm' type.

* fix: fixed an issue happened when listing

- Updated the import of 'BaseModule' in 'migration.ts' to be local dir instead of the dist one to fix the un-exported issue
- Flat the array of the contracts then loop over them all
- Updated the moduleName after listing the 'Fullvm' to 'vm' again to list the other deployments
- Pushed the newly changed contract to the 'newContracts' array to avoid the lag of gql
- Updated the 'moduleName' in 'base.ts' to take 'Fullvm' if the 'moduleNames' is undefined

* fix: Removed unused 'console.log' in 'base.ts'

* docs: DocString the '_migrateOldFullVMs' function.

* fix: Apply comments:

- Removed un-needed 'await' in 'Promise.all()'
- Removed un-needed 'await' in returning the 'extrinsics'
- Returned the 'module.tfClient.applyAllExtrinsics<Contract>(extrinsics);' without awaiting on it
- Moved the update of the moduleName to the 'migrateModule'

* fix: Apply comments:

- Updated the import of the 'BaseModule' from '@threefold/grid_client'
- Moved the update of the 'module.moduleName' to the '_migrateOldFullVMs'

* fix: fixed the issue of listing deployments by changing the project Name to '{project Name}' instead of '{project Name/deployment_name}'.

* fix(Listing deployments): Updated the 'GridClient/base.ts/getMyContracts' method with some improvements:

- Replaced the 'exact' search with 'contains' algo by adding 'includes' when searching on the 'projectName'.
- Introduced a new field 'updatedAt' in the 'GqlNodeContract' type to store the updated time in it.
- Added a condition on checking on the 'updatedAt' field to replace the newly added contract with the Gql contract.
  • Loading branch information
Mahmoud-Emad authored May 21, 2024
1 parent 3c8f13a commit 0da6be6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/grid_client/src/clients/tf-grid/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface GqlBaseContract {
twinID: string;
state: string;
createdAt: string;
updatedAt: string;
solutionProviderID: string;
}

Expand Down
23 changes: 19 additions & 4 deletions packages/grid_client/src/modules/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class BaseModule {
BaseModule.newContracts.push({
contractID: String(contract.contractId),
createdAt: Date.now().toString(),
updatedAt: Date.now().toString(),
deploymentData: contract.contractType.nodeContract.deploymentData,
deploymentHash: contract.contractType.nodeContract.deploymentHash,
gridVersion: "4",
Expand Down Expand Up @@ -179,15 +180,29 @@ class BaseModule {
if (fetch || !this.contracts) {
let contracts = await this.tfClient.contracts.listMyNodeContracts({
graphqlURL: this.config.graphqlURL,
type: modulesNames[this.moduleName],
type: modulesNames[this.moduleName] ?? this.moduleName,
projectName: this.projectName,
});

const alreadyFetchedContracts: GqlNodeContract[] = [];

for (const contract of BaseModule.newContracts) {
if (contract.parsedDeploymentData?.projectName !== this.projectName) continue;
if (!contract.parsedDeploymentData?.projectName.includes(this.projectName)) continue;
if (contract.parsedDeploymentData.type !== modulesNames[this.moduleName]) continue;
const c = contracts.filter(c => +c.contractID === +contract.contractID);
if (c.length > 0) {

const filteredContract = contracts.filter(c => c.contractID === contract.contractID);

const now = new Date();
const contractUpdatedAt = new Date(+contract.updatedAt);
const beforeOneMin = now.getTime() - contractUpdatedAt.getTime() < 1000 * 60;

if (filteredContract.length > 0) {
if (beforeOneMin) {
const idx = contracts.indexOf(filteredContract[0]);
contracts[idx] = contract;
continue;
}

alreadyFetchedContracts.push(contract);
continue;
}
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * from "./farmerbot";
export * from "./farms";
export * from "./networks";
export * from "./bridge";
export * from "./base";
1 change: 1 addition & 0 deletions packages/grid_client/src/primitives/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ PersistentKeepalive = 25\nEndpoint = ${endpoint}`;
Network.newContracts.push({
contractID: String(AddedContract.contractId),
createdAt: Date.now().toString(),
updatedAt: Date.now().toString(),
deploymentData: AddedContract.contractType.nodeContract.deploymentData,
deploymentHash: AddedContract.contractType.nodeContract.deploymentHash,
gridVersion: "4",
Expand Down
59 changes: 56 additions & 3 deletions packages/playground/src/utils/migration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
import type { BaseModule } from "@threefold/grid_client/dist/es6/modules/base";
import type { Contract } from "@threefold/tfchain_client";
import { BaseModule } from "@threefold/grid_client";
import type { Contract, ExtrinsicResult } from "@threefold/tfchain_client";

/**
* `_migrateOldFullVMs` is a function is implemented to list all old `FullVM` that listed in the old `Playground` with type `Fullvm`
* @param `module` takes the `module` which is an instance of the `BaseModule`.
* @returns Applying the extrinsic in the chain with the batch request.
*/
async function _migrateOldFullVMs(module: BaseModule) {
const moduleName = module.moduleName;
module.moduleName = "Fullvm"; // Load old deployments that deployed with `Fullvm` type.
const deploymentNames = await module._list();
const contracts = await Promise.all(
deploymentNames.map(deploymentName => module.getDeploymentContracts(deploymentName)),
);

const loadUpdateExtrinsics = () => {
return Promise.all(
contracts.flat().map(contract => {
const oldData = JSON.parse(contract.deploymentData || "{}") as unknown as {
type: string;
name: string;
projectName: string;
};

if (oldData.type === "vm") {
return;
}

oldData.type = "vm";
contract.parsedDeploymentData = oldData;
contract.deploymentData = JSON.stringify(oldData);
contract.updatedAt = Date.now().toString();

BaseModule.newContracts.push(contract);

return module.tfClient.contracts.updateNode({
id: +contract.contractID,
data: contract.deploymentData,
hash: contract.deploymentHash,
});
}),
);
};

module.moduleName = moduleName;
if (deploymentNames.length) {
const extrinsics = await loadUpdateExtrinsics().then(exts => exts.filter(Boolean) as ExtrinsicResult<Contract>[]);
return module.tfClient.applyAllExtrinsics<Contract>(extrinsics);
}
}

export async function migrateModule(module: BaseModule) {
await module._list(); // force grid_client migration
// To list only `Full` and `Micro` VMs.
if (module.moduleName === "machines") {
await _migrateOldFullVMs(module);
}

await module._list(); // force grid_client migration
const path = module.getNewDeploymentPath();
const keys = await module.backendStorage.list(path);

Expand Down

0 comments on commit 0da6be6

Please sign in to comment.