Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.5.70 - AsyncApexJob Guard Clause #451

Merged
merged 4 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ As well, don't miss [the Wiki](../../wiki), which includes even more info for co

## Deployment & Setup

<a href="https://login.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008SnlnAAC">
<a href="https://login.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008SnrXAAS">
<img alt="Deploy to Salesforce"
src="./media/deploy-package-to-prod.png">
</a>

<a href="https://test.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008SnlnAAC">
<a href="https://test.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008SnrXAAS">
<img alt="Deploy to Salesforce Sandbox"
src="./media/deploy-package-to-sandbox.png">
</a>
Expand Down
7 changes: 7 additions & 0 deletions extra-tests/classes/RollupLimitsTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@IsTest
private class RollupLimitsTest {
@IsTest
static void correctlyReferencesOrgLimits() {
System.assertEquals(false, new RollupLimits.Tester(RollupControl__mdt.getInstance('Org_Default'), false).hasExceededOrgAsyncLimit());
}
}
5 changes: 5 additions & 0 deletions extra-tests/classes/RollupLimitsTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>57.0</apiVersion>
<status>Active</status>
</ApexClass>
16 changes: 16 additions & 0 deletions extra-tests/classes/RollupTests.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2847,4 +2847,20 @@ private class RollupTests {
mock.Records[0].get('Description')
);
}

@IsTest
static void fallsBackToRunningSyncWhenOutOfAsyncJobs() {
RollupTestUtils.DMLMock mock = RollupTestUtils.loadAccountIdMock(
new List<ContactPointAddress>{ new ContactPointAddress(Name = 'One', PreferenceRank = 50) }
);
Rollup.apexContext = TriggerOperation.AFTER_INSERT;
// one over the default limit
RollupLimits.orgAsyncJobsUsed = 250001;

// specifically no Test.startTest()/Test.stopTest() to prove it's been run sync
Rollup.sumFromApex(ContactPointAddress.PreferenceRank, ContactPointAddress.ParentId, Account.Id, Account.AnnualRevenue, Account.SObjectType).runCalc();

System.assertEquals(1, mock.Records.size());
System.assertEquals(50, mock.Records[0].get(Account.AnnualRevenue));
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apex-rollup",
"version": "1.5.69",
"version": "1.5.70",
"description": "Fast, configurable, elastically scaling custom rollup solution. Apex Invocable action, one-liner Apex trigger/CMDT-driven logic, and scheduled Apex-ready.",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions rollup-namespaced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ For more info, see the base `README`.

## Deployment & Setup

<a href="https://login.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008SnlsAAC">
<a href="https://login.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008SnrcAAC">
<img alt="Deploy to Salesforce"
src="./media/deploy-package-to-prod.png">
</a>

<a href="https://test.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008SnlsAAC">
<a href="https://test.salesforce.com/packaging/installPackage.apexp?p0=04t6g000008SnrcAAC">
<img alt="Deploy to Salesforce Sandbox"
src="./media/deploy-package-to-sandbox.png">
</a>
7 changes: 4 additions & 3 deletions rollup-namespaced/sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"default": true,
"package": "apex-rollup-namespaced",
"path": "rollup-namespaced/source/rollup",
"versionName": "Tech debt cleanup",
"versionNumber": "1.0.42.0",
"versionName": "Check org limits for AsyncApexJob execution prior to enqueueing/batching",
"versionNumber": "1.0.43.0",
"versionDescription": "Fast, configurable, elastically scaling custom rollup solution. Apex Invocable action, one-liner Apex trigger/CMDT-driven logic, and scheduled Apex-ready.",
"releaseNotesUrl": "https://github.com/jamessimone/apex-rollup/releases/latest",
"unpackagedMetadata": {
Expand Down Expand Up @@ -36,6 +36,7 @@
"apex-rollup-namespaced@1.0.39-0": "04t6g000008SneMAAS",
"apex-rollup-namespaced@1.0.40-0": "04t6g000008SnebAAC",
"apex-rollup-namespaced@1.0.41-0": "04t6g000008SnldAAC",
"apex-rollup-namespaced@1.0.42-0": "04t6g000008SnlsAAC"
"apex-rollup-namespaced@1.0.42-0": "04t6g000008SnlsAAC",
"apex-rollup-namespaced@1.0.43-0": "04t6g000008SnrcAAC"
}
}
11 changes: 9 additions & 2 deletions rollup/core/classes/RollupAsyncProcessor.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,9 @@ global virtual without sharing class RollupAsyncProcessor extends Rollup impleme
}

private Boolean ingestRollupControlData(List<RollupAsyncProcessor> syncRollups) {
Boolean isRunningAsnyc = this.getIsRunningAsync();
Boolean isRunningAsync = this.getIsRunningAsync();
Boolean shouldRollupsRunWithoutCustomSetting = false;
RollupLimits.Tester limitTester = new RollupLimits.Tester(this.rollupControl, isRunningAsync);
for (Integer index = this.rollups.size() - 1; index >= 0; index--) {
RollupAsyncProcessor rollup = this.rollups[index];
shouldRollupsRunWithoutCustomSetting = rollup.getCanRollupWithoutCustomSetting();
Expand All @@ -1068,8 +1069,14 @@ global virtual without sharing class RollupAsyncProcessor extends Rollup impleme
Boolean shouldRunSyncDeferred = this.getShouldRunSyncDeferred(rollup);
Boolean couldRunSync =
rollup.rollupControl.ShouldRunAs__c == RollupMetaPicklists.ShouldRunAs.Synchronous ||
(isRunningAsnyc && this.getIsTimingOut(rollup.rollupControl, rollup) == false) ||
(isRunningAsync && this.getIsTimingOut(rollup.rollupControl, rollup) == false) ||
this.isSingleRecordSyncUpdate(rollup);

if (limitTester.hasExceededOrgAsyncLimit()) {
couldRunSync = true;
shouldRunSyncDeferred = false;
}

String hashedRollupKey = rollup.getHashedContents() + rollup.calcItems?.hashCode() + rollup.oldCalcItems?.hashCode();
if (hashedRollups.contains(hashedRollupKey)) {
this.rollups.remove(index);
Expand Down
25 changes: 25 additions & 0 deletions rollup/core/classes/RollupLimits.cls
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
public without sharing class RollupLimits {
@TestVisible
private static Integer stubbedQueryRows;
@TestVisible
private static Integer orgAsyncJobsUsed;

private static final Integer SYNC_TIMEOUT_INTERVAL_MS = 3000;
private static final Integer ASYNC_TIMEOUT_INTERVAL_MS = 13000;

private static final Integer LIMIT_HEAP_SIZE = Limits.getLimitHeapSize();
private static final Integer LIMIT_QUERY_ROWS = 50000;

private static final Boolean HAS_EXCEEDED_ORG_ASYNC_JOB_LIMIT {
get {
Integer countOfJobsUsed = 0;
Integer asyncJobLimit = 250000;
// at the moment, packaging orgs run tests synchronously, which produces an error when fetching System.OrgLimits.getMap()
// this is a workaround until it's safe to access OrgLimits from a synchronously running test
if (HAS_EXCEEDED_ORG_ASYNC_JOB_LIMIT == null && Test.isRunningTest() == false) {
System.OrgLimit asyncLimit = System.OrgLimits.getMap().get('DailyAsyncApexExecutions');
asyncJobLimit = asyncLimit.getLimit();
countOfJobsUsed = asyncLimit.getValue();
} else if (HAS_EXCEEDED_ORG_ASYNC_JOB_LIMIT == null && orgAsyncJobsUsed != null) {
countOfJobsUsed = orgAsyncJobsUsed;
}
HAS_EXCEEDED_ORG_ASYNC_JOB_LIMIT = countOfJobsUsed - asyncJobLimit > 0;
return HAS_EXCEEDED_ORG_ASYNC_JOB_LIMIT;
}
set;
}

public class Tester {
private final transient RollupControl__mdt control;
private final transient Boolean isRunningAsync;
Expand Down Expand Up @@ -61,5 +82,9 @@ public without sharing class RollupLimits {
Integer remainingQueryRows = this.control.MaxQueryRows__c?.intValue() - queryRowsUsed;
return remainingQueryRows > 0 ? remainingQueryRows : 0;
}

public Boolean hasExceededOrgAsyncLimit() {
return HAS_EXCEEDED_ORG_ASYNC_JOB_LIMIT;
}
}
}
2 changes: 1 addition & 1 deletion rollup/core/classes/RollupLogger.cls
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
global without sharing virtual class RollupLogger implements ILogger {
@TestVisible
// this gets updated via the pipeline as the version number gets incremented
private static final String CURRENT_VERSION_NUMBER = 'v1.5.69';
private static final String CURRENT_VERSION_NUMBER = 'v1.5.70';
private static final LoggingLevel FALLBACK_LOGGING_LEVEL = LoggingLevel.DEBUG;
private static final RollupPlugin PLUGIN = new RollupPlugin();

Expand Down
7 changes: 4 additions & 3 deletions sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"default": true,
"package": "apex-rollup",
"path": "rollup",
"versionName": "Tech debt cleanup",
"versionNumber": "1.5.69.0",
"versionName": "Check org limits for AsyncApexJob execution prior to enqueueing/batching",
"versionNumber": "1.5.70.0",
"versionDescription": "Fast, configurable, elastically scaling custom rollup solution. Apex Invocable action, one-liner Apex trigger/CMDT-driven logic, and scheduled Apex-ready.",
"releaseNotesUrl": "https://github.com/jamessimone/apex-rollup/releases/latest",
"unpackagedMetadata": {
Expand Down Expand Up @@ -106,6 +106,7 @@
"apex-rollup@1.5.66-0": "04t6g000008SneHAAS",
"apex-rollup@1.5.67-0": "04t6g000008SneWAAS",
"apex-rollup@1.5.68-0": "04t6g000008SnlYAAS",
"apex-rollup@1.5.69-0": "04t6g000008SnlnAAC"
"apex-rollup@1.5.69-0": "04t6g000008SnlnAAC",
"apex-rollup@1.5.70-0": "04t6g000008SnrXAAS"
}
}