-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathworker.js
More file actions
114 lines (93 loc) · 3.39 KB
/
worker.js
File metadata and controls
114 lines (93 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const {
Contract,
Keypair,
Networks,
TransactionBuilder,
rpc,
} = require('@stellar/stellar-sdk');
const fs = require('fs');
const path = require('path');
// --- PATH CORRECTION: Accessing src/ from root ---
const { loadConfig } = require('./src/config');
const logger = require('./src/services/loggerService');
const { pollLeaseEvents } = require('./src/jobs/eventPoller');
const config = loadConfig();
const DB_PATH = path.join(__dirname, 'leases.json');
// Mock Database Helper
function getLeases() {
if (!fs.existsSync(DB_PATH)) return {};
return JSON.parse(fs.readFileSync(DB_PATH, 'utf8'));
}
function saveLeases(leases) {
fs.writeFileSync(DB_PATH, JSON.stringify(leases, null, 2));
}
/**
* Coordination Worker: Monitors and triggers lease initialization
*/
async function checkAndInitializeLease(leaseId) {
const leases = getLeases();
const lease = leases[leaseId];
if (!lease) {
logger.error(`Lease ${leaseId} not found.`);
return;
}
if (lease.landlord_signed && lease.tenant_signed && !lease.initialized_on_chain) {
logger.info(`[Worker] Coordination triggered for Lease: ${leaseId}. Both parties signed.`);
try {
logger.info(`[Worker] Attempting to initialize on-chain for ${leaseId}...`);
await triggerOnChainInitialization(leaseId, lease.contract_data);
lease.initialized_on_chain = true;
lease.status = 'INITIALIZED';
saveLeases(leases);
logger.info(`[Worker] Lease ${leaseId} successfully initialized on-chain.`);
} catch (error) {
logger.error(`[Worker] CRITICAL FAILURE for lease ${leaseId}:`, { error: error.message });
}
} else {
logger.info(`[Worker] Lease ${leaseId} still pending signatures or already initialized.`);
}
}
async function triggerOnChainInitialization(leaseId, data) {
const server = new rpc.Server(config.contracts.rpcUrl);
const networkPassphrase = Networks.TESTNET;
const secretKey = process.env.CONTRACT_ADMIN_SECRET || 'S...';
if (secretKey === 'S...' || secretKey === 'SDP...') {
logger.info(`[Stellar] Skipping actual transaction building... Simulation mode active.`);
return new Promise((resolve) => setTimeout(resolve, 100));
}
const sourceKey = Keypair.fromSecret(secretKey);
const contractId = config.contracts.defaultContractId;
const contract = new Contract(contractId);
logger.info(`[Stellar] Building transaction for contract ${contractId}...`);
logger.info(`[Stellar] Calling initialize_lease(${leaseId}, ...)`);
return new Promise((resolve) => setTimeout(resolve, 1000));
}
/**
* --- START: INTERNAL DASHBOARD ENTRY POINT ---
* This block starts the Transaction Monitor automatically.
*/
(function startBackgroundJobs() {
console.log(" LeaseFlow Worker Process Started (Root)");
// Run the Transaction Monitor (Issue #9)
if (config.jobs.renewalJobEnabled) {
logger.info(" Transaction Monitor: Active", {
interval: config.jobs.monitorIntervalMs,
contract: config.contracts.defaultContractId
});
// Run immediately on start
pollLeaseEvents();
// Set recurring interval for the monitor
setInterval(async () => {
try {
await pollLeaseEvents();
} catch (err) {
logger.error("Transaction Monitor Polling Error", { error: err.message });
}
}, config.jobs.monitorIntervalMs);
}
})();
module.exports = {
checkAndInitializeLease,
getLeases,
saveLeases
};