Skip to content

Commit

Permalink
Queueable refactor
Browse files Browse the repository at this point in the history
Refactors the ShoveBundlr class to be queuable.  Mostly because (duh), a single execution of ShoveBundlr quickly hits the 50K record query limit.

This refactor chains the jobs sequentially, based on the Sequence__c field on the metadata, and increments it for each subsequent job.

Finally, each object queried now saves as a discreet static resource, rather than trying to work with one monster JSON string.  This was proactively done, since a heap limit would be tripped soon after getting around the query limit.

@todo next:  write the parser (implementing SandboxPostCopy) that will insert the records stored in the destination sandbox.  I might need to work some voodoo to manage the relationships between data - but that can wait until after sleep.
  • Loading branch information
XCMattHigel committed Aug 27, 2018
1 parent f2b5dfd commit 8ccf19e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 96 deletions.
96 changes: 0 additions & 96 deletions src/classes/ShoveBundlr.cls

This file was deleted.

89 changes: 89 additions & 0 deletions src/classes/ShovlrQueuable.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

global class ShovlrQueuable implements Queueable, Database.AllowsCallouts {

String query { get; set; }
String objectName { get; set; }
Integer counter { get; set; }
Data_Shover_Setting__mdt settings { get; set; }
String sessionId { get; set; }

public ShovlrQueuable() {
sessionId = UserInfo.getSessionId();
System.debug('ShovlrQueuable init');
counter = 0;
settings = [
SELECT Object_Name__c, Limit__c, Sequence__c
FROM Data_Shover_Setting__mdt
ORDER BY Sequence__c
][counter];
}

public ShovlrQueuable(Integer c, String sId) {
sessionId = sId;
System.debug('ShovlrQueuable');
counter = c;
System.debug('counter ' + String.valueOf(counter));
settings = [
SELECT Object_Name__c, Limit__c, Sequence__c
FROM Data_Shover_Setting__mdt
ORDER BY Sequence__c
][counter];
}

public void execute(QueueableContext context) {
System.debug('ShovlrQueuable execute');
query = createQuery(settings.Object_Name__c);
String qLimit = settings.Limit__c > 0 ? String.valueOf(settings.Limit__c) : '50000';
query += ' LIMIT ' + qLimit;
objectName = settings.Object_Name__c;
List<SObject> sObjectList = Database.query(query);
Map<String, List<SObject>> dataMap = new Map<String, List<SObject>>{
objectName => sObjectList
};

String jsonData = JSON.serialize(dataMap);

MetadataService.StaticResource sr = createStaticResource(jsonData);
MetadataService.MetadataPort service = createService();
MetadataService.SaveResult[] results = service.createMetadata(new List<MetadataService.StaticResource>{sr});
System.debug('saveResults = ' + JSON.serializePretty(results));

counter += 1;
System.enqueueJob(new ShovlrQueuable(counter, sessionId));
}

MetadataService.StaticResource createStaticResource(String jsonData) {
System.debug('ShovlrQueuable createStaticResource');
MetadataService.StaticResource sr = new MetadataService.StaticResource();
System.debug('objectName = ' + objectName);
String srName = objectName.replaceAll('_', '');
System.debug('srName = ' + srName);
sr.fullName = 'DataShove' + srName;
sr.contentType = 'text';
sr.cacheControl = 'private';
sr.content = EncodingUtil.base64Encode(Blob.valueOf(jsonData));
return sr;
}

String createQuery(String objectName) {
System.debug('ShovlrQueuable createQuery');
String q = 'SELECT ';

Schema.SObjectType soType = Schema.getGlobalDescribe().get(objectName);
Schema.DescribeSObjectResult dsr = soType.getDescribe();
List<String> fieldList = new List<String>(dsr.fields.getMap().keySet());

q += String.join(fieldList, ', ');
q += ' FROM ' + objectName;

return q;
}

MetadataService.MetadataPort createService() {
System.debug('ShovlrQueuable createService');
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = sessionId;
return service;
}
}
File renamed without changes.

0 comments on commit 8ccf19e

Please sign in to comment.