66import socket
77import time
88from collections import defaultdict
9- from dataclasses import dataclass , field
9+ from dataclasses import dataclass , field , fields
1010from typing import Any , Callable , Dict , List , Optional , Set , Tuple
1111
1212from channelfinder import ChannelFinderClient
@@ -89,6 +89,15 @@ def loads(cls, conf: ConfigAdapter) -> "CFConfig":
8989 push_always_retry = conf .getboolean ("pushAlwaysRetry" , True ),
9090 )
9191
92+ def __repr__ (self ) -> str :
93+ parts = []
94+ for f in fields (self ):
95+ value = getattr (self , f .name )
96+ if f .name == "cf_password" :
97+ value = "***" if value else None
98+ parts .append (f"{ f .name } ={ value !r} " )
99+ return f"CFConfig({ ', ' .join (parts )} )"
100+
92101
93102@dataclass
94103class CFProperty :
@@ -555,26 +564,41 @@ def _commit_with_thread(self, transaction: CommitTransaction):
555564 Args:
556565 transaction: The transaction to commit.
557566 """
567+ host = transaction .source_address .host
568+ port = transaction .source_address .port
569+
558570 if not self .running :
559- host = transaction .source_address .host
560- port = transaction .source_address .port
561571 raise defer .CancelledError (f"CF Processor is not running (transaction: { host } :{ port } )" )
562572
563573 _log .info ("CF_COMMIT: %s" , transaction )
564574 _log .debug ("CF_COMMIT: transaction: %s" , repr (transaction ))
565575
576+ ioc_name = transaction .client_infos .get ("IOCNAME" )
577+ if not ioc_name :
578+ ioc_name = str (port )
579+ _log .debug ("IOC at %s:%d did not send IOCNAME; using port as iocName" , host , port )
580+
581+ owner = (
582+ transaction .client_infos .get (self .cf_config .env_owner_variable )
583+ or transaction .client_infos .get ("CF_USERNAME" )
584+ or self .cf_config .username
585+ )
586+ if owner == self .cf_config .username :
587+ _log .debug (
588+ "IOC at %s:%d did not send %s or CF_USERNAME; using service account as owner" ,
589+ host ,
590+ port ,
591+ self .cf_config .env_owner_variable ,
592+ )
593+
566594 ioc_info = IocInfo (
567- host = transaction .source_address .host ,
568- hostname = transaction .client_infos .get ("HOSTNAME" ) or transaction .source_address .host ,
569- ioc_name = transaction .client_infos .get ("IOCNAME" ) or str (transaction .source_address .port ),
570- ioc_IP = transaction .source_address .host ,
571- owner = (
572- transaction .client_infos .get (self .cf_config .env_owner_variable )
573- or transaction .client_infos .get ("CF_USERNAME" )
574- or self .cf_config .username
575- ),
595+ host = host ,
596+ hostname = transaction .client_infos .get ("HOSTNAME" ) or host ,
597+ ioc_name = ioc_name ,
598+ ioc_IP = host ,
599+ owner = owner ,
576600 time = self .current_time (self .cf_config .timezone ),
577- port = transaction . source_address . port ,
601+ port = port ,
578602 )
579603
580604 record_infos = self .transaction_to_record_infos (ioc_info , transaction )
@@ -596,13 +620,16 @@ def remove_channel(self, recordName: str, iocid: str) -> None:
596620 iocid: The IOC ID of the record to remove from.
597621 """
598622 self .channel_ioc_ids [recordName ].remove (iocid )
599- if iocid in self .iocs :
600- self .iocs [iocid ].channelcount -= 1
601- if self .iocs [iocid ].channelcount == 0 :
623+ if iocid not in self .iocs :
624+ if len (self .channel_ioc_ids [recordName ]) == 0 :
625+ del self .channel_ioc_ids [recordName ]
626+ return
627+ self .iocs [iocid ].channelcount -= 1
628+ if self .iocs [iocid ].channelcount <= 0 :
629+ if self .iocs [iocid ].channelcount < 0 :
630+ _log .error ("Channel count negative: %s" , iocid )
602631 self .iocs .pop (iocid )
603- elif self .iocs [iocid ].channelcount < 0 :
604- _log .error ("Channel count negative: %s" , iocid )
605- if len (self .channel_ioc_ids [recordName ]) <= 0 : # case: channel has no more iocs
632+ if len (self .channel_ioc_ids [recordName ]) == 0 :
606633 del self .channel_ioc_ids [recordName ]
607634
608635 def clean_service (self ) -> None :
@@ -1095,9 +1122,9 @@ def _update_channelfinder(
10951122
10961123 if iocid not in iocs :
10971124 _log .warning (
1098- "IOC %s did not send an initial transaction to join IOC list: %s " ,
1125+ "IOC %s did not send an initial transaction to join IOC list (%d IOCs known) " ,
10991126 ioc_info ,
1100- [( ioc . ioc_name , ioc . ioc_id ) for ioc in iocs . values ()] ,
1127+ len ( iocs ) ,
11011128 )
11021129
11031130 if ioc_info .hostname is None or ioc_info .ioc_name is None :
0 commit comments