You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过logic.js每日定时执行Hyperledger Composer特定交易?

How to Schedule Daily Hyperledger Composer Transactions

Great question! Let's break down how to handle scheduled transactions in Hyperledger Composer, since the core framework doesn’t support native cron-like scheduling directly in logic.js.

Key Background First

Hyperledger Composer's transaction processor (your logic.js) is event-driven—it only runs when a transaction is explicitly submitted to the network. There’s no built-in way to trigger code at a specific time from within the chaincode itself, even if you have timestamped assets. Timestamps on assets are useful for tracking state changes, but they don’t automatically trigger transactions on their own.

1. Build an External Scheduled Application (Most Common & Reliable Approach)

This is the standard way to handle scheduled transactions. You’ll create a simple service outside of Hyperledger Fabric/Composer that runs on a fixed schedule, and uses Composer’s SDK or REST API to submit the required transaction at your desired time.

For example:

  • Use a Node.js script with libraries like node-schedule or cron to manage timing.
  • The script connects to your Composer business network via the composer-client SDK, creates an instance of your target transaction, and submits it when the schedule fires.

Here’s a quick code snippet to illustrate this workflow:

const schedule = require('node-schedule');
const { BusinessNetworkConnection } = require('composer-client');

async function submitDailyTransaction() {
    const connection = new BusinessNetworkConnection();
    try {
        // Connect to your business network
        await connection.connect('admin@your-business-network');
        
        // Build your transaction instance with required parameters
        const transaction = connection.buildTransaction('org.your-namespace.YourTransaction', {
            targetAsset: 'resource:org.your-namespace.YourAsset#asset-123',
            // Add other transaction fields as needed
        });
        
        // Submit the transaction to the network
        await connection.submitTransaction(transaction);
        console.log('Daily scheduled transaction submitted successfully');
    } catch (error) {
        console.error('Failed to submit transaction:', error);
    } finally {
        await connection.disconnect();
    }
}

// Schedule to run every day at 8:00 AM (cron syntax: minute, hour, day, month, weekday)
schedule.scheduleJob('0 8 * * *', submitDailyTransaction);

You can host this script on a server, or use system-level tools like Linux cron or Windows Task Scheduler to run it at your desired time if you don’t want to use a Node.js scheduling library.

2. Query Timestamped Assets + Trigger Transactions (Alternative Workflow)

If your assets have time-based fields (e.g., scheduledActionDate or expiryDate), you can combine an external scheduler with chaincode queries:

  • The external service runs on a schedule, queries the business network for assets that meet your time-based condition (e.g., assets where scheduledActionDate equals today).
  • For each matching asset, the service submits the corresponding transaction to process it.

This approach is useful if you need to batch-process multiple assets based on their timestamped state, rather than running a single generic transaction daily.

What Not to Do

Don’t try to implement scheduling inside logic.js or chaincode. Chaincode runs on peer nodes, and adding timers or cron jobs there would break the blockchain’s consistency model—each peer would run the timer independently, leading to inconsistent state across the network. Chaincode execution must always be triggered by a explicitly submitted transaction.

Final Takeaway

Yes, you’ll need to set up an external application/service to handle scheduled transactions. This is standard practice in blockchain systems, as it aligns with the event-driven nature of the technology and keeps your chaincode focused on core business logic rather than scheduling.

内容的提问来源于stack exchange,提问作者K.Through

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:02:49