move reconcile relays to dvmconfig, shutdown client on reconcile

This commit is contained in:
Believethehype
2024-06-02 17:28:51 +02:00
parent 0ad44f2917
commit f53a292b89
6 changed files with 57 additions and 42 deletions

View File

@@ -175,24 +175,23 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface):
return 1 return 1
def sync_db(self): def sync_db(self):
opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_LONG_TIMEOUT)))
sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY)
keys = Keys.parse(sk.to_hex()) keys = Keys.parse(sk.to_hex())
signer = NostrSigner.keys(keys) signer = NostrSigner.keys(keys)
database = NostrDatabase.sqlite(self.db_name) database = NostrDatabase.sqlite(self.db_name)
cli = ClientBuilder().signer(signer).database(database).opts(opts).build() cli = ClientBuilder().signer(signer).database(database).opts(opts).build()
ropts = RelayOptions().ping(True) for relay in self.dvm_config.RECONCILE_DB_RELAY_LIST:
cli.add_relay_with_opts("wss://relay.damus.io", ropts) cli.add_relay(relay)
cli.add_relay_with_opts("wss://nostr.oxtr.dev", ropts)
cli.add_relay_with_opts("wss://nostr21.com", ropts)
cli.connect() cli.connect()
timestamp_hour_ago = Timestamp.now().as_secs() - self.db_since timestamp_since = Timestamp.now().as_secs() - self.db_since
lasthour = Timestamp.from_secs(timestamp_hour_ago) since = Timestamp.from_secs(timestamp_since)
filter1 = Filter().kinds([definitions.EventDefinitions.KIND_NOTE, definitions.EventDefinitions.KIND_REACTION, filter1 = Filter().kinds([definitions.EventDefinitions.KIND_NOTE, definitions.EventDefinitions.KIND_REACTION,
definitions.EventDefinitions.KIND_ZAP]).since(lasthour) # Notes, reactions, zaps definitions.EventDefinitions.KIND_ZAP]).since(since) # Notes, reactions, zaps
# filter = Filter().author(keys.public_key()) # filter = Filter().author(keys.public_key())
print("[" + self.dvm_config.IDENTIFIER + "] Syncing notes of the last " + str( print("[" + self.dvm_config.IDENTIFIER + "] Syncing notes of the last " + str(
@@ -200,8 +199,8 @@ class DicoverContentCurrentlyPopular(DVMTaskInterface):
dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN) dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN)
cli.reconcile(filter1, dbopts) cli.reconcile(filter1, dbopts)
database.delete(Filter().until(Timestamp.from_secs( database.delete(Filter().until(Timestamp.from_secs(
Timestamp.now().as_secs() - self.db_since))) # Clear old events so db doesnt get too full. Timestamp.now().as_secs() - self.db_since))) # Clear old events so db doesn't get too full.
cli.shutdown()
print( print(
"[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..") "[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..")

View File

@@ -193,23 +193,34 @@ class DicoverContentCurrentlyPopularZaps(DVMTaskInterface):
return 1 return 1
def sync_db(self): def sync_db(self):
opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_LONG_TIMEOUT)))
sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY)
keys = Keys.parse(sk.to_hex())
signer = NostrSigner.keys(keys)
database = NostrDatabase.sqlite(self.db_name)
cli = ClientBuilder().signer(signer).database(database).opts(opts).build()
timestamp_hour_ago = Timestamp.now().as_secs() - self.db_since for relay in self.dvm_config.RECONCILE_DB_RELAY_LIST:
lasthour = Timestamp.from_secs(timestamp_hour_ago) cli.add_relay(relay)
cli.connect()
timestamp_since = Timestamp.now().as_secs() - self.db_since
since = Timestamp.from_secs(timestamp_since)
filter1 = Filter().kinds([definitions.EventDefinitions.KIND_NOTE, definitions.EventDefinitions.KIND_REACTION, filter1 = Filter().kinds([definitions.EventDefinitions.KIND_NOTE, definitions.EventDefinitions.KIND_REACTION,
definitions.EventDefinitions.KIND_ZAP]).since(lasthour) # Notes, reactions, zaps definitions.EventDefinitions.KIND_ZAP]).since(since) # Notes, reactions, zaps
# filter = Filter().author(keys.public_key()) # filter = Filter().author(keys.public_key())
print("[" + self.dvm_config.NIP89.NAME + "] Syncing notes of the last " + str( print("[" + self.dvm_config.IDENTIFIER + "] Syncing notes of the last " + str(
self.db_since) + " seconds.. this might take a while..") self.db_since) + " seconds.. this might take a while..")
dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN) dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN)
self.client.reconcile(filter1, dbopts) cli.reconcile(filter1, dbopts)
filter_delete = Filter().until(Timestamp.from_secs(Timestamp.now().as_secs() - self.db_since)) database.delete(Filter().until(Timestamp.from_secs(
self.client.database().delete(filter_delete) # Clear old events so db doesn't get too full. Timestamp.now().as_secs() - self.db_since))) # Clear old events so db doesn't get too full.
cli.shutdown()
print( print(
"[" + self.dvm_config.NIP89.NAME + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..") "[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..")
# We build an example here that we can call by either calling this file directly from the main directory, # We build an example here that we can call by either calling this file directly from the main directory,

View File

@@ -190,31 +190,37 @@ class DicoverContentCurrentlyPopularFollowers(DVMTaskInterface):
self.last_schedule = Timestamp.now().as_secs() self.last_schedule = Timestamp.now().as_secs()
return 1 return 1
def sync_db(self): def sync_db(self):
opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_LONG_TIMEOUT)))
sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY)
keys = Keys.parse(sk.to_hex()) keys = Keys.parse(sk.to_hex())
signer = NostrSigner.keys(keys) signer = NostrSigner.keys(keys)
database = NostrDatabase.sqlite(self.db_name) database = NostrDatabase.sqlite(self.db_name)
cli = ClientBuilder().signer(signer).database(database).opts(opts).build() cli = ClientBuilder().signer(signer).database(database).opts(opts).build()
cli.add_relay("wss://relay.damus.io") for relay in self.dvm_config.RECONCILE_DB_RELAY_LIST:
cli.add_relay(relay)
cli.connect() cli.connect()
timestamp_hour_ago = Timestamp.now().as_secs() - self.db_since timestamp_since = Timestamp.now().as_secs() - self.db_since
lasthour = Timestamp.from_secs(timestamp_hour_ago) since = Timestamp.from_secs(timestamp_since)
filter1 = Filter().kinds([definitions.EventDefinitions.KIND_NOTE, definitions.EventDefinitions.KIND_REACTION, filter1 = Filter().kinds(
definitions.EventDefinitions.KIND_ZAP]).since(lasthour) # Notes, reactions, zaps [definitions.EventDefinitions.KIND_NOTE, definitions.EventDefinitions.KIND_REACTION,
definitions.EventDefinitions.KIND_ZAP]).since(since) # Notes, reactions, zaps
# filter = Filter().author(keys.public_key()) # filter = Filter().author(keys.public_key())
print("[" + self.dvm_config.IDENTIFIER + "] Syncing notes of the last " + str(self.db_since) + " seconds.. this might take a while..") print("[" + self.dvm_config.IDENTIFIER + "] Syncing notes of the last " + str(
self.db_since) + " seconds.. this might take a while..")
dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN) dbopts = NegentropyOptions().direction(NegentropyDirection.DOWN)
cli.reconcile(filter1, dbopts) cli.reconcile(filter1, dbopts)
database.delete(Filter().until(Timestamp.from_secs( database.delete(Filter().until(Timestamp.from_secs(
Timestamp.now().as_secs() - self.db_since))) # Clear old events so db doesnt get too full. Timestamp.now().as_secs() - self.db_since))) # Clear old events so db doesn't get too full.
cli.shutdown()
print("[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..") print("[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(
self.db_since) + " seconds..")
# We build an example here that we can call by either calling this file directly from the main directory, # We build an example here that we can call by either calling this file directly from the main directory,

View File

@@ -196,24 +196,16 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface):
return 1 return 1
def sync_db(self): def sync_db(self):
opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_LONG_TIMEOUT)))
sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY)
keys = Keys.parse(sk.to_hex()) keys = Keys.parse(sk.to_hex())
signer = NostrSigner.keys(keys) signer = NostrSigner.keys(keys)
database = NostrDatabase.sqlite(self.db_name) database = NostrDatabase.sqlite(self.db_name)
cli = ClientBuilder().signer(signer).database(database).opts(opts).build() cli = ClientBuilder().signer(signer).database(database).opts(opts).build()
cli.add_relay("wss://relay.damus.io") for relay in self.dvm_config.RECONCILE_DB_RELAY_LIST:
cli.add_relay("wss://nostr.oxtr.dev") cli.add_relay(relay)
cli.add_relay("wss://relay.nostr.net")
cli.add_relay("wss://relay.nostr.bg")
cli.add_relay("wss://nostr.wine")
cli.add_relay("wss://nostr21.com")
#RELAY_LIST = [ "wss://nostr.wine",
# , "wss://relay.nostr.bg",
# , "wss://relay.nostr.net"
# ]
cli.connect() cli.connect()
timestamp_since = Timestamp.now().as_secs() - self.db_since timestamp_since = Timestamp.now().as_secs() - self.db_since
@@ -227,7 +219,7 @@ class DicoverContentCurrentlyPopularbyTopic(DVMTaskInterface):
cli.reconcile(filter1, dbopts) cli.reconcile(filter1, dbopts)
database.delete(Filter().until(Timestamp.from_secs( database.delete(Filter().until(Timestamp.from_secs(
Timestamp.now().as_secs() - self.db_since))) # Clear old events so db doesn't get too full. Timestamp.now().as_secs() - self.db_since))) # Clear old events so db doesn't get too full.
cli.shutdown()
print("[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..") print("[" + self.dvm_config.IDENTIFIER + "] Done Syncing Notes of the last " + str(self.db_since) + " seconds..")

View File

@@ -21,6 +21,13 @@ class DVMConfig:
"wss://relay.nostr.net" "wss://relay.nostr.net"
] ]
RECONCILE_DB_RELAY_LIST = ["wss://relay.damus.io", "wss://nostr21.com",
"wss://nostr.oxtr.dev", "wss://relay.nostr.bg",
"wss://relay.nostr.net" , "wss://relay.primal.net"] #, "wss://relay.snort.social"]
# cli.add_relay("wss://relay.primal.net")
#"wss://relay.damus.io" #"wss://relay.damus.io"
RELAY_TIMEOUT = 5 RELAY_TIMEOUT = 5

View File

@@ -1,6 +1,6 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
VERSION = '0.5.6' VERSION = '0.5.7'
DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines' DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines'
LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information') LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information')