1

I am running a script to watch my events as follows:

run.js:

var fs = require('fs');
Web3   = require("web3");
web3   = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

address="0x..." 
abi=[...]
var myContractInstance  = web3.eth.contract(abi).at(address);

blkNum = 1000;
var event = myContractInstance.LogJob({}, { fromBlock: blkNum, toBlock: 'latest' });
event.watch( function (error, result) {
    fs.appendFile( "/tmp/test.txt", JSON.stringify(result) + '\n', function(err) { 
       process.exit();
    });
});

After running node run.js or nohup run.js &, we could find the output as follows:

cat /tmp/test.txt

Please note that when a new Event feed in running watch() terminates itself after reading the fed Events.

But when I do: sudo killall geth even geth is not working on the background, watch() continues to watch any event would come or not. It does not return any error or anything.

[Q] Is there any way to trigger watch() function to terminate itself when running geth or Parity on the background is killed? We could do cntl-c but I want watch() to kill itself automatically.

2

There is no perfect solution for that as far as i know, because geth does not send any "shutdown signal" to users connected to it via RPC (that would be a neat feature in the future) but you could:

  1. (re)set the web3 provider "once in a while" (line 3) while watching the event. If the node client is down, you will have some error returned ("Empty RPC response") which you can handle to stop the event watching,

  2. Use the capabilities of nodejs to watch for the node's port being open and stop the watching as soon it's not.

Good Luck !

| improve this answer | |
  • 1
    I guess it should watch port 8545. But the issue is when geth shut down it wont trigger watch(). So I might need to run an external function to check port 8545 or geth is working on the back ground or not. @Nikita Fuchs – alper Jun 7 '17 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.