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

Automate Batch Job Scheduling via Post-Install Script #81

Merged
merged 1 commit into from
Feb 6, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global with sharing class AdyenPaymentLinkBatchJobScheduler implements Schedulable{
global void execute(SchedulableContext schedulableContext) {
AdyenPaymentLinkBatch adyenPaymentLinkBatch = new AdyenPaymentLinkBatch();
Database.executeBatch(adyenPaymentLinkBatch);
}
}
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>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@IsTest
private class AdyenPaymentLinkBatchJobSchedulerTest {

@IsTest
static void scheduleJobTest() {
// Given
AdyenPaymentLinkBatchJobScheduler paymentLinkJob = new AdyenPaymentLinkBatchJobScheduler();
String sch = '0 0 * * * ?';

// When
Test.startTest();
String jobId = System.schedule('Test Payment Link Batch', sch, paymentLinkJob);
Test.stopTest();

// Then
CronTrigger ct = [SELECT Id, CronExpression FROM CronTrigger WHERE Id = :jobId];
Assert.areEqual(sch, ct.CronExpression);
}
}
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>62.0</apiVersion>
<status>Active</status>
</ApexClass>
22 changes: 22 additions & 0 deletions force-app/main/default/classes/PostInstallScript.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class PostInstallScript implements InstallHandler {
private static final String JOB_NAME = 'Adyen_PBL_Batch_Job';
private static final String SCHEDULED_JOB_TYPE = '7'; // Scheduled Apex Job

public void onInstall(InstallContext context) {
if (!isBatchJobScheduled(JOB_NAME)) {
String cronExp = '0 0 * * * ?';
System.schedule(JOB_NAME, cronExp, new AdyenPaymentLinkBatchJobScheduler());
}
}

private Boolean isBatchJobScheduled(String jobName) {
List<CronTrigger> scheduledJobs = [
SELECT Id, CronJobDetail.Name
FROM CronTrigger
WHERE CronJobDetail.Name = :jobName
AND CronJobDetail.JobType = :SCHEDULED_JOB_TYPE
];

return !scheduledJobs.isEmpty();
}
}
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>62.0</apiVersion>
<status>Active</status>
</ApexClass>
23 changes: 23 additions & 0 deletions force-app/main/default/classes/PostInstallScriptTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@IsTest
private class PostInstallScriptTest {

@IsTest
static void testInstallScript() {
// Given
PostInstallScript postInstall = new PostInstallScript();

// When
Test.testInstall(postInstall, null);

// Then
List<CronTrigger> jobs = [SELECT Id FROM CronTrigger WHERE CronJobDetail.Name = 'Adyen_PBL_Batch_Job'];
Assert.areEqual(1, jobs.size(), 'Expected one scheduled job after fresh install');

// When: Simulating an upgrade from version 1.0 to the current version
Test.testInstall(postInstall, new Version(1, 0));

// Then: Ensure the job is still only scheduled once (no duplicate scheduling)
jobs = [SELECT Id FROM CronTrigger WHERE CronJobDetail.Name = 'Adyen_PBL_Batch_Job'];
Assert.areEqual(1, jobs.size(), 'Expected only one scheduled job after upgrade');
}
}
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>62.0</apiVersion>
<status>Active</status>
</ApexClass>
4 changes: 4 additions & 0 deletions manifest/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<members>AdyenPaymentLinkBatchTest</members>
<members>AdyenPBLController</members>
<members>AdyenPBLControllerTest</members>
<members>AdyenPaymentLinkBatchJobScheduler</members>
<members>AdyenPaymentLinkBatchJobSchedulerTest</members>
<members>PostInstallScript</members>
<members>PostInstallScriptTest</members>
<name>ApexClass</name>
</types>
<types>
Expand Down
1 change: 1 addition & 0 deletions sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"definitionFile": "config/project-scratch-def.json",
"versionName": "version 3.1",
"versionNumber": "3.1.0.NEXT",
"postInstallScript": "PostInstallScript",
"ancestorVersion": "HIGHEST",
"dependencies": [
{
Expand Down
Loading