Skip to content

Commit

Permalink
#299 change the mcdo_RunCopadoFunctionFromLWC.cls and mcdo_RetrieveTa…
Browse files Browse the repository at this point in the history
…ble.js to its previous state, once that this logic wont work, and will try to use the event's logic
  • Loading branch information
Daniel-Ventura-25 committed Nov 25, 2022
1 parent 7dccdac commit 638202b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 113 deletions.
37 changes: 1 addition & 36 deletions force-app/main/default/classes/mcdo_RunCopadoFunctionFromLWC.cls
Original file line number Diff line number Diff line change
Expand Up @@ -129,39 +129,4 @@ public with sharing class mcdo_RunCopadoFunctionFromLWC {
throw new AuraHandledException(e.getMessage());
}
}

/**
* This function is getting called from the refreshJobProgress in order to give the most recent data from the respective result record.
* @param jobExecutionId the id of the job execution
* @return Returns resultId, status, lastModified, stepName, progress and isCompleted.
*/
@AuraEnabled
public static jobProgressWrapper getJobProgress(String jobExecutionId) {
// get the newest result associated with this job execution
copado__Result__c[] results = [SELECT Id, LastModifiedDate,
copado__JobStep__r.copado__JobExecution__r.copado__Status__c,
copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c,
copado__Progress_Status__c,
copado__JobStep__r.Name
FROM copado__Result__c
WHERE copado__JobStep__r.copado__JobExecution__c =
:jobExecutionId
WITH SECURITY_ENFORCED ORDER BY CreatedDate DESC LIMIT 1];

JobProgressWrapper response = new JobProgressWrapper();
if(!results.isEmpty()) {
response.resultId = results[0].Id;
response.status = results[0].copado__JobStep__r.copado__JobExecution__r.copado__Status__c;
response.lastModified = results[0].LastModifiedDate;
response.stepName = results[0].copado__JobStep__r.Name;

// calculate progress with the Job Execution error message, or the last progress status
response.progress = String.isNotEmpty(results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c)
?results[0].copado__JobStep__r.copado__JobExecution__r.copado__ErrorMessage__c
:results[0].copado__Progress_Status__c;
response.isCompleted = (response.status == 'Successful' || response.status == 'Error' || response.status == 'Canceled');
}

return response;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import {
} from "lightning/empApi";

// Apex Methods for retrieving and committing metadata (And Communication with the Copado Package)
import executeRetrieve from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve";
import getJobProgress from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getJobProgress";
import ExecuteRetrieveFromCopado from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.executeRetrieve";
import getMetadataFromEnvironment from "@salesforce/apex/mcdo_RunCopadoFunctionFromLWC.getMetadataFromEnvironment";

// Apex functions to retrieve Recorddata from LWC
Expand Down Expand Up @@ -306,26 +305,11 @@ export default class mcdo_RetrieveTable extends LightningElement {
*/
async retrieve() {
this.loadingState(true, "Starting Retrieve");
// update the UI immediately, clearing any previous values
this.jobExecutionId = null;
this.progressStatus = "Requesting";
this.lastModified = new Date();
this.progress = "";
this.jobIsRunning = true;
this.showProgress = true;

try {
const jobExecutionId = await executeRetrieve({
const jobExecutionId = await ExecuteRetrieveFromCopado({
userStoryId: this.userStoryId
});

console.log(`jobExecutionId: ${jobExecutionId} - daniel`);

// Start the polling process to track the job via UI
this.jobExecutionId = jobExecutionId;
this.jobExecutionIdUrl = "/" + jobExecutionId;
this.poll(() => this.refreshJobProgress());

// TODO get result ID from Job step related to job execution
//! has to be last result created for that job step if there are multiple
this.subscribeToCompletionEvent(jobExecutionId);
Expand All @@ -341,62 +325,6 @@ export default class mcdo_RetrieveTable extends LightningElement {
this.selectedRows = this.selectedRows.map(({ id }) => id);
}
}
console.log("ended - daniel");
}

// read the job progress and assign the UI properties
async refreshJobProgress() {
try {
let details = await getJobProgress({ jobExecutionId: this.jobExecutionId });
this.resultId = details.resultId;
this.progressStatus = details.status;
this.progress = details.progress;
this.stepName = details.stepName;
this.lastModified = details.lastModified;
this.jobIsRunning = !details.isCompleted;
} catch (error) {
console.error(
"There was an error trying to read the progress of the job. Will retry.",
error
);
}
}

// LWC METHODS
disconnectedCallback() {
this.stopPoll(); // ensure we disconnect the polling.
}

// call functionCall and if it returns true, call again every 5s for the first 5m, 10s after 5m, 30s after 30m, 60s after 1h
poll(functionCall, pollStartTime) {
if (!pollStartTime) {
this.pollStop = false;
pollStartTime = new Date().getTime();
}
if (!functionCall.apply() || this.pollStop) {
return this.stopPoll();
}
let elapsedMs = new Date().getTime() - pollStartTime;
let intervalMs =
elapsedMs > 3600000
? 60000
: elapsedMs > 1800000
? 30000
: elapsedMs > 300000
? 10000
: 5000;
this.pollProcessId = window.setTimeout(
() => this.poll(functionCall, pollStartTime),
intervalMs
);
}
// use this method to stop the polling, e.g. from the UI or when disconnectedCallback occurs
stopPoll() {
this.pollStop = true;
if (this.pollProcessId) {
window.clearTimeout(this.pollProcessId);
this.pollProcessId = null;
}
}

/**
Expand All @@ -421,8 +349,6 @@ export default class mcdo_RetrieveTable extends LightningElement {
// show progress on screen; try-catch is needed because copado__Payload__c sometimes contains bad JSON
const stepStatus = JSON.parse(response.data.payload.copado__Payload__c);
this.progressStatus = stepStatus.data.progressStatus || this.progressStatus;
console.log("its not completed yet - daniel");
console.log(`stepStatus: ${JSON.stringify(stepStatus)} - daniel`);
} catch {
// ignore
}
Expand Down Expand Up @@ -678,4 +604,4 @@ export default class mcdo_RetrieveTable extends LightningElement {
this.showTable = !isLoading;
this.refreshButtonDisabled = isLoading;
}
}
}

0 comments on commit 638202b

Please sign in to comment.