Skip to content

Commit 78ef997

Browse files
jarekdanielakphilippfromme
authored andcommitted
chore: setup REST client early
1 parent 7009952 commit 78ef997

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

demo/server.mjs

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,41 @@ import dotenv from 'dotenv';
55
const __filename = fileURLToPath(import.meta.url);
66
const __dirname = path.dirname(__filename);
77

8-
dotenv.config({ path: path.join(__dirname, '.env') });
9-
108
import express from 'express';
119

1210
import { Camunda8 } from '@camunda8/sdk';
1311

14-
const app = express();
15-
16-
app.use(express.json());
17-
18-
app.use(express.static(path.join(__dirname, 'public')));
1912

2013
/** @type {import('@camunda8/sdk').CamundaRestClient} */
2114
let camundaRestClient;
2215

23-
const PORT = process.env.PORT || 3000;
16+
try {
2417

25-
app.listen(PORT, () => {
26-
console.log(`\n🚀 Camunda 8 API starting on port ${PORT}\n`);
18+
const config = dotenv.config({ path: path.join(__dirname, '.env') }).parsed;
2719

20+
if (!Object.keys(config)?.length) {
21+
throw new Error('No configuration found in .env file');
22+
}
2823

29-
const config = {
30-
...process.env,
31-
CAMUNDA_TOKEN_DISK_CACHE_DISABLE: true
32-
};
24+
const c8 = new Camunda8(config);
25+
camundaRestClient = c8.getCamundaRestClient();
3326

34-
try {
35-
const c8 = new Camunda8(config);
36-
camundaRestClient = c8.getCamundaRestClient();
37-
} catch (error) {
38-
console.error('Failed to create Camunda 8 REST client:', error);
39-
console.warn('API requests will return { success: false }');
40-
return;
41-
}
27+
createJobWorker();
28+
} catch (error) {
29+
console.error('Failed to create Camunda 8 REST client:', error);
30+
console.warn('API requests will return { success: false }');
31+
}
4232

43-
camundaRestClient.createJobWorker({
44-
type: 'foo',
45-
jobHandler: async (job) => {
46-
console.log('🚀 Processing job worker...');
33+
const app = express();
4734

48-
// Simulate some work with a delay
49-
await new Promise((resolve) => setTimeout(resolve, 2000));
35+
app.use(express.json());
5036

51-
console.log('🚀 Job completed');
37+
app.use(express.static(path.join(__dirname, 'public')));
5238

53-
job.complete({
54-
foo: 'jobWorkerWasHere'
55-
});
56-
},
57-
pollInterval: 1000,
58-
timeout: 5000,
59-
maxJobsToActivate: 5
60-
});
39+
const PORT = process.env.PORT || 3000;
40+
41+
app.listen(PORT, () => {
42+
console.log(`\n🚀 Camunda 8 API starting on port ${PORT}\n`);
6143
});
6244

6345
app.post('/api/deploy', async (req, res) => {
@@ -193,4 +175,30 @@ app.get('/api/getProcessInstanceIncident/:processInstanceKey', async (req, res)
193175
} catch (err) {
194176
res.status(500).json({ success: false, error: err.message });
195177
}
196-
});
178+
});
179+
180+
function createJobWorker() {
181+
if (!camundaRestClient) {
182+
console.warn('Camunda environment not configured, job worker not started');
183+
return;
184+
}
185+
186+
camundaRestClient.createJobWorker({
187+
type: 'foo',
188+
jobHandler: async (job) => {
189+
console.log('🚀 Processing job worker...');
190+
191+
// Simulate some work with a delay
192+
await new Promise((resolve) => setTimeout(resolve, 2000));
193+
194+
console.log('🚀 Job completed');
195+
196+
job.complete({
197+
foo: 'jobWorkerWasHere'
198+
});
199+
},
200+
pollInterval: 1000,
201+
timeout: 5000,
202+
maxJobsToActivate: 5
203+
});
204+
}

0 commit comments

Comments
 (0)