Skip to content

Commit a489be9

Browse files
committed
fix: pozil review (#5)
1 parent e5f8da1 commit a489be9

36 files changed

+1249
-922
lines changed

.forceignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
# .forceignore v2
12
# List files or directories below to ignore them when running force:source:push, force:source:pull, and force:source:status
23
# More information: https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_exclude_source.htm
34
#
45

6+
# Standard metadata
57
package.xml
8+
**/appMenus/**
9+
**/appSwitcher/**
10+
**/objectTranslations/**
11+
**/profiles/**
12+
**/settings/**
613

714
# LWC configuration files
815
**/jsconfig.json

.github/workflows/main.yml

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
1-
name: CI
1+
name: main
22

33
on:
4-
pull_request:
4+
push:
55
branches: [master]
66

77
jobs:
8-
formatting-and-linting:
9-
runs-on: ubuntu-latest
10-
steps:
11-
- name: "Checkout source code"
12-
uses: actions/checkout@v2
13-
14-
- uses: actions/cache@v2
15-
with:
16-
path: ~/.npm
17-
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
18-
restore-keys: |
19-
${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
20-
21-
- name: "Install npm dependencies"
22-
run: npm install
238

24-
- name: "Code formatting verification with Prettier"
25-
run: npm run prettier:verify
26-
27-
- name: Install PMD
28-
run: wget https://github.com/pmd/pmd/releases/download/pmd_releases%2F6.22.0/pmd-bin-6.22.0.zip && unzip pmd-bin-6.22.0.zip
29-
30-
- name: Execute PMD
31-
run: pmd-bin-6.22.0/bin/run.sh pmd -minimumpriority 2 -d chain -R apex-ruleset.xml -f text -l apex
32-
33-
ci-build:
9+
build:
3410
runs-on: ubuntu-latest
3511
steps:
12+
- name: Set environment variables
13+
run: |
14+
echo "SFDX_DISABLE_AUTOUPDATE=true" >> $GITHUB_ENV
15+
echo "SFDX_DISABLE_SOURCE_MEMBER_POLLING=true" >> $GITHUB_ENV
16+
echo "SFDX_DISABLE_TELEMETRY=true" >> $GITHUB_ENV
17+
3618
- name: Install Salesforce CLI
3719
run: |
3820
wget https://developer.salesforce.com/media/salesforce-cli/sfdx-linux-amd64.tar.xz
@@ -57,7 +39,12 @@ jobs:
5739
run: 'sfdx force:source:push'
5840

5941
- name: 'Run Apex tests'
60-
run: npm run test:coverage
42+
run: sfdx force:apex:test:run -c -r human -d ./tests/apex -w 20
43+
44+
- name: 'Upload code coverage for Apex to Codecov.io'
45+
uses: codecov/[email protected]
46+
with:
47+
flags: Apex
6148

6249
- name: 'Delete scratch org'
6350
if: always()

.github/workflows/pull-request.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches: [master]
6+
7+
jobs:
8+
formatting-and-linting:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: "Checkout source code"
12+
uses: actions/checkout@v2
13+
14+
- uses: actions/cache@v2
15+
with:
16+
path: ~/.npm
17+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
18+
restore-keys: |
19+
${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
20+
21+
- name: Set environment variables
22+
run: |
23+
echo "SFDX_DISABLE_AUTOUPDATE=true" >> $GITHUB_ENV
24+
echo "SFDX_DISABLE_TELEMETRY=true" >> $GITHUB_ENV
25+
26+
- name: Install Salesforce CLI
27+
run: |
28+
wget https://developer.salesforce.com/media/salesforce-cli/sfdx-linux-amd64.tar.xz
29+
mkdir sfdx-cli
30+
tar xJf sfdx-linux-amd64.tar.xz -C sfdx-cli --strip-components 1
31+
./sfdx-cli/install
32+
33+
- name: "Install npm dependencies"
34+
run: npm install
35+
36+
- name: "Code formatting verification with Prettier"
37+
run: npm run prettier:verify
38+
39+
- name: Execute Static Analysis
40+
run: npm run lint
41+
42+
ci-build:
43+
runs-on: ubuntu-latest
44+
steps:
45+
- name: Set environment variables
46+
run: |
47+
echo "SFDX_DISABLE_AUTOUPDATE=true" >> $GITHUB_ENV
48+
echo "SFDX_DISABLE_SOURCE_MEMBER_POLLING=true" >> $GITHUB_ENV
49+
echo "SFDX_DISABLE_TELEMETRY=true" >> $GITHUB_ENV
50+
51+
- name: Install Salesforce CLI
52+
run: |
53+
wget https://developer.salesforce.com/media/salesforce-cli/sfdx-linux-amd64.tar.xz
54+
mkdir sfdx-cli
55+
tar xJf sfdx-linux-amd64.tar.xz -C sfdx-cli --strip-components 1
56+
./sfdx-cli/install
57+
58+
- name: Checkout
59+
uses: actions/checkout@v2
60+
61+
- name: "Populate auth file with CI_URL secret"
62+
shell: bash
63+
run: "echo ${{ secrets.CI_URL}} > ./CI_URL.txt"
64+
65+
- name: "Authenticate CI Sandbox"
66+
run: "sfdx force:auth:sfdxurl:store -f ./CI_URL.txt -a ci -d"
67+
68+
- name: 'Create scratch org'
69+
run: 'sfdx force:org:create -f config/project-scratch-def.json -a scratch-org -s -d 1'
70+
71+
- name: 'Push source to scratch org'
72+
run: 'sfdx force:source:push'
73+
74+
- name: 'Run Apex tests'
75+
run: npm run test:coverage
76+
77+
- name: 'Upload code coverage for Apex to Codecov.io'
78+
uses: codecov/[email protected]
79+
with:
80+
flags: Apex
81+
82+
- name: 'Delete scratch org'
83+
if: always()
84+
run: 'sfdx force:org:delete -p -u scratch-org'

.lintstagedrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,yaml,yml}": [
33
"prettier --write"
4+
],
5+
"**/classes/*.cls": [
6+
"sfdx scanner:run -v -f table -c 'Async-Linkable' -t "
47
]
58
}

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
coverage/
55
tests/
66
sfdx-project.json
7-
config/project-scratch-def.json
7+
config/project-scratch-def.json
8+
sfdx-cli

README.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,79 @@
1-
## Apex AsyncLinkable
1+
## Apex AsyncLinkable ![Build](https://github.com/scolladon/apex-async-linkable/actions/workflows/main.yml/badge.svg) [![codecov](https://codecov.io/gh/scolladon/apex-async-linkable/branch/master/graph/badge.svg?token=DFHDV3OCIS)](https://codecov.io/gh/scolladon/apex-async-linkable)
22

33
This library provides all the classes required to chain all kind of Async jobs.
44

55
## Installation
66

77
```bash
8-
$ sfdx force:source:deploy -p path/to/source
8+
$ sfdx force:source:deploy -p chain/src/lib
99
```
1010

1111
## Usage
1212

13-
Create a class and extend the type of the AsyncLinkable class you require.\
14-
Your class can have its own constructors and its own attributes of course !\
15-
Respect the interface contract and override the job method.\
16-
You need to override the start method also for the BatchLink and ScheduleBatchLink class\
17-
The job method will contains your business logic. It can access the private attributes of your class (and the protected ones of the base class).\
18-
If you need some extra interface to make you're code work, it is up to you to add them (Database.Stateful, Database.AllowsCallouts, etc).\
19-
You're ready to chain !\
20-
You do not need to override another method except if you really know what you're doing\
13+
1. Create a class and extend the type of the `[AsyncLinkable](https://github.com/scolladon/apex-async-linkable/blob/master/chain/src/lib/classes/AsyncLinkable.cls)` class you require.
14+
_Your class can have its own constructors and its own attributes of course !_
15+
2. Respect the interface contract and override the `job` method.
16+
_`start` method must also be overridden for the BatchLink and ScheduleBatchLink class_
17+
18+
The `job` method will contains your business logic. It can access the private attributes of your class (and the protected ones of the base class).
19+
20+
If you need some extra interface to make you're code work, it is up to you to add them (`Database.Stateful`, `Database.AllowsCallouts`, etc).
21+
22+
You're ready to chain apex asynchronous process!
23+
24+
Example for Batchable:
2125

2226
```apex
2327
// Subclass BatchLink for example
2428
public class BatchLink_EXAMPLE extends BatchLink {
25-
public override Database.QueryLocator start(Database.BatchLinkableContext bc) {
29+
public override Database.QueryLocator start(
30+
Database.BatchLinkableContext bc
31+
) {
2632
return Database.getQueryLocator('select id from account limit 1');
2733
}
2834
29-
public BatchLink_EXAMPLE(){
35+
public BatchLink_EXAMPLE() {
3036
super();
3137
}
3238
3339
protected override void job() {
3440
System.Debug('BatchLink_EXAMPLE');
3541
}
3642
}
43+
```
44+
45+
Example for Queue:
3746

47+
```apex
3848
//Subclass QueueLink for example
3949
public class QueueLink_EXAMPLE extends QueueLink {
40-
41-
public QueueLink_EXAMPLE(){
50+
public QueueLink_EXAMPLE() {
4251
super();
4352
}
4453
4554
protected override void job() {
4655
System.Debug('BatchLink_EXAMPLE');
4756
}
4857
}
58+
```
4959

60+
Example chaining `BatchLink_EXAMPLE` and `QueueLink_EXAMPLE`;
61+
62+
```apex
5063
// Chain both and execute them
5164
public class Service {
5265
public static void doService() {
53-
AsyncLinkable aChain = new BatchLink_EXAMPLE();
54-
aChain.Add(new QueueLink_EXAMPLE());
55-
56-
aChain.startChain();
66+
ChainManager.instance
67+
.add(new BatchLink_EXAMPLE())
68+
.add(new QueueLink_EXAMPLE())
69+
.startChain();
5770
}
5871
}
5972
```
6073

6174
## Improvement
6275

6376
Implement sub class allowing to handle BatchLink and ScheduleBatchLink with iterable in addition of QueryLocator\
64-
Allow to benefit from [this pilot](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_enhanced_FutureLink_overview.htm) for the FutureLink class
6577

6678
## Versioning
6779

apex-ruleset.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<ruleset
33
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
name="Custom Apex Rules"
5+
name="Async-Linkable"
66
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"
77
>
88
<description>Custom Ruleset to use with Apex PMD</description>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public inherited sharing class AsyncLinkIterable implements Iterable<AsyncLinkable> {
2+
public AsyncLinkable link { get; private set; }
3+
public AsyncLinkIterable(final AsyncLinkable link) {
4+
this.link = link;
5+
}
6+
7+
public Iterator<AsyncLinkable> iterator() {
8+
return new AsyncLinkIterator(this);
9+
}
10+
11+
public inherited sharing class AsyncLinkIterator implements Iterator<AsyncLinkable> {
12+
private AsyncLinkIterable linkIterable;
13+
private Boolean hasStarted;
14+
15+
private AsyncLinkIterator(AsyncLinkIterable linkIterable) {
16+
this.linkIterable = linkIterable;
17+
this.hasStarted = false;
18+
}
19+
20+
public Boolean hasNext() {
21+
return !hasStarted
22+
? this.linkIterable?.link != null
23+
: this.linkIterable?.link?.nextLink != null;
24+
}
25+
26+
public AsyncLinkable next() {
27+
if (!this.hasStarted) {
28+
this.hasStarted = true;
29+
return this.linkIterable.link;
30+
}
31+
this.linkIterable.link = this.linkIterable?.link?.nextLink;
32+
return this.linkIterable?.link;
33+
}
34+
}
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>52.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

chain/src/lib/classes/AsyncLinkable.cls

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
public inherited sharing abstract class AsyncLinkable {
2-
protected AsyncLinkable nextLink;
2+
public AsyncLinkable nextLink { get; private set; }
33
protected boolean canBreak;
44
protected String internalExecutor;
55

@@ -27,14 +27,6 @@ public inherited sharing abstract class AsyncLinkable {
2727
this.nextLink = link;
2828
}
2929

30-
public virtual AsyncLinkable getNext() {
31-
return this.nextLink;
32-
}
33-
34-
public virtual Boolean hasNext() {
35-
return this.getNext() != null;
36-
}
37-
3830
public virtual void accept(Visitor aVisitor) {
3931
aVisitor.visit(this);
4032
}

0 commit comments

Comments
 (0)