-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream-logger.py
executable file
·1029 lines (778 loc) · 32.5 KB
/
stream-logger.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python3
# -----------------------------------------------------------------------------
# stream-logger.py Logger of streaming input.
# -----------------------------------------------------------------------------
# Import from standard library. https://docs.python.org/3/library/
import argparse
import configparser
import datetime
import json
import linecache
import logging
import math
import multiprocessing
import os
import queue
import signal
import string
import sys
import threading
import time
from glob import glob
from urllib.parse import urlparse, urlunparse
from urllib.request import urlopen
# Import from https://pypi.org/
import boto3
import confluent_kafka
import pika
__all__ = []
__version__ = "1.1.6" # See https://www.python.org/dev/peps/pep-0396/
__date__ = '2020-02-06'
__updated__ = '2023-09-30'
SENZING_PRODUCT_ID = "5011" # See https://github.com/senzing-garage/knowledge-base/blob/main/lists/senzing-product-ids.md
log_format = '%(asctime)s %(message)s'
# The "configuration_locator" describes where configuration variables are in:
# 1) Command line options, 2) Environment variables, 3) Configuration files, 4) Default values
configuration_locator = {
"debug": {
"default": False,
"env": "SENZING_DEBUG",
"cli": "debug"
},
"delay_in_seconds": {
"default": 0,
"env": "SENZING_DELAY_IN_SECONDS",
"cli": "delay-in-seconds"
},
"kafka_bootstrap_server": {
"default": "localhost:9092",
"env": "SENZING_KAFKA_BOOTSTRAP_SERVER",
"cli": "kafka-bootstrap-server",
},
"kafka_group": {
"default": "senzing-kafka-group",
"env": "SENZING_KAFKA_GROUP",
"cli": "kafka-group"
},
"kafka_topic": {
"default": "senzing-kafka-topic",
"env": "SENZING_KAFKA_TOPIC",
"cli": "kafka-topic"
},
"monitoring_period_in_seconds": {
"default": 60 * 10,
"env": "SENZING_MONITORING_PERIOD_IN_SECONDS",
"cli": "monitoring-period-in-seconds",
},
"rabbitmq_host": {
"default": "localhost:5672",
"env": "SENZING_RABBITMQ_HOST",
"cli": "rabbitmq-host",
},
"rabbitmq_password": {
"default": "bitnami",
"env": "SENZING_RABBITMQ_PASSWORD",
"cli": "rabbitmq-password",
},
"rabbitmq_prefetch_count": {
"default": 50,
"env": "SENZING_RABBITMQ_PREFETCH_COUNT",
"cli": "rabbitmq-prefetch-count",
},
"rabbitmq_queue": {
"default": "senzing-rabbitmq-queue",
"env": "SENZING_RABBITMQ_QUEUE",
"cli": "rabbitmq-queue",
},
"rabbitmq_use_existing_entities": {
"default": True,
"env": "SENZING_RABBITMQ_USE_EXISTING_ENTITIES",
"cli": "rabbitmq-use-existing-entities",
},
"rabbitmq_username": {
"default": "user",
"env": "SENZING_RABBITMQ_USERNAME",
"cli": "rabbitmq-username",
},
"sleep_time_in_seconds": {
"default": 0,
"env": "SENZING_SLEEP_TIME_IN_SECONDS",
"cli": "sleep-time-in-seconds"
},
"sqs_queue_url": {
"default": None,
"env": "SENZING_SQS_QUEUE_URL",
"cli": "sqs-queue-url"
},
"subcommand": {
"default": None,
"env": "SENZING_SUBCOMMAND",
},
"threads_per_process": {
"default": 4,
"env": "SENZING_THREADS_PER_PROCESS",
"cli": "threads-per-process",
},
}
# Enumerate keys in 'configuration_locator' that should not be printed to the log.
keys_to_redact = [
]
# -----------------------------------------------------------------------------
# Define argument parser
# -----------------------------------------------------------------------------
def get_parser():
''' Parse commandline arguments. '''
subcommands = {
'kafka': {
"help": 'Read JSON Lines from Apache Kafka topic.',
"argument_aspects": ["common"],
"arguments": {
"--kafka-bootstrap-server": {
"dest": "kafka_bootstrap_server",
"metavar": "SENZING_KAFKA_BOOTSTRAP_SERVER",
"help": "Kafka bootstrap server. Default: localhost:9092"
},
"--kafka-group": {
"dest": "kafka_group",
"metavar": "SENZING_KAFKA_GROUP",
"help": "Kafka group. Default: senzing-kafka-group"
},
"--kafka-topic": {
"dest": "kafka_topic",
"metavar": "SENZING_KAFKA_TOPIC",
"help": "Kafka topic. Default: senzing-kafka-topic"
},
},
},
'rabbitmq': {
"help": 'Read JSON Lines from RabbitMQ queue.',
"argument_aspects": ["common"],
"arguments": {
"--rabbitmq-host": {
"dest": "rabbitmq_host",
"metavar": "SENZING_RABBITMQ_HOST",
"help": "RabbitMQ host. Default: localhost:5672"
},
"--rabbitmq-password": {
"dest": "rabbitmq_password",
"metavar": "SENZING_RABBITMQ_PASSWORD",
"help": "RabbitMQ password. Default: bitnami"
},
"--rabbitmq-prefetch-count": {
"dest": "rabbitmq_prefetch_count",
"metavar": "SENZING_RABBITMQ_PREFETCH_COUNT",
"help": "RabbitMQ prefetch-count. Default: 50"
},
"--rabbitmq-queue": {
"dest": "rabbitmq_queue",
"metavar": "SENZING_RABBITMQ_QUEUE",
"help": "RabbitMQ queue. Default: senzing-rabbitmq-queue"
},
"--rabbitmq-use-existing-entities": {
"dest": "rabbitmq_use_existing_entities",
"metavar": "SENZING_RABBITMQ_USE_EXISTING_ENTITIES",
"help": "Connect to an existing queue using its settings. An error is thrown if the queue does not exist. If False, it will create the queue if it does not exist. If it exists, then it will attempt to connect, checking the settings match. Default: True"
},
"--rabbitmq-username": {
"dest": "rabbitmq_username",
"metavar": "SENZING_RABBITMQ_USERNAME",
"help": "RabbitMQ username. Default: user"
},
},
},
'sqs': {
"help": 'Read JSON Lines from AWS SQS queue.',
"argument_aspects": ["common"],
"arguments": {
"--sqs-queue-url": {
"dest": "sqs_queue_url",
"metavar": "SENZING_SQS_QUEUE_URL",
"help": "AWS SQS URL. Default: none"
},
},
},
'sleep': {
"help": 'Do nothing but sleep. For Docker testing.',
"arguments": {
"--sleep-time-in-seconds": {
"dest": "sleep_time_in_seconds",
"metavar": "SENZING_SLEEP_TIME_IN_SECONDS",
"help": "Sleep time in seconds. DEFAULT: 0 (infinite)"
},
},
},
'version': {
"help": 'Print version of program.',
},
'docker-acceptance-test': {
"help": 'For Docker acceptance testing.',
},
}
argument_aspects = {
"common": {
"--debug": {
"dest": "debug",
"action": "store_true",
"help": "Enable debugging. (SENZING_DEBUG) Default: False"
},
"--delay-in-seconds": {
"dest": "delay_in_seconds",
"metavar": "SENZING_DELAY_IN_SECONDS",
"help": "Delay before processing in seconds. DEFAULT: 0"
},
"--monitoring-period-in-seconds": {
"dest": "monitoring_period_in_seconds",
"metavar": "SENZING_MONITORING_PERIOD_IN_SECONDS",
"help": "Period, in seconds, between monitoring reports. Default: 600"
},
"--threads-per-process": {
"dest": "threads_per_process",
"metavar": "SENZING_THREADS_PER_PROCESS",
"help": "Number of threads per process. Default: 4"
},
},
}
# Augment "subcommands" variable with arguments specified by aspects.
for subcommand, subcommand_value in subcommands.items():
if 'argument_aspects' in subcommand_value:
for aspect in subcommand_value['argument_aspects']:
if 'arguments' not in subcommands[subcommand]:
subcommands[subcommand]['arguments'] = {}
arguments = argument_aspects.get(aspect, {})
for argument, argument_value in arguments.items():
subcommands[subcommand]['arguments'][argument] = argument_value
parser = argparse.ArgumentParser(prog="init-container.py", description="Initialize Senzing installation. For more information, see https://github.com/senzing-garage/docker-init-container")
subparsers = parser.add_subparsers(dest='subcommand', help='Subcommands (SENZING_SUBCOMMAND):')
for subcommand_key, subcommand_values in subcommands.items():
subcommand_help = subcommand_values.get('help', "")
subcommand_arguments = subcommand_values.get('arguments', {})
subparser = subparsers.add_parser(subcommand_key, help=subcommand_help)
for argument_key, argument_values in subcommand_arguments.items():
subparser.add_argument(argument_key, **argument_values)
return parser
# -----------------------------------------------------------------------------
# Message handling
# -----------------------------------------------------------------------------
# 1xx Informational (i.e. logging.info())
# 3xx Warning (i.e. logging.warning())
# 5xx User configuration issues (either logging.warning() or logging.err() for Client errors)
# 7xx Internal error (i.e. logging.error for Server errors)
# 9xx Debugging (i.e. logging.debug())
MESSAGE_INFO = 100
MESSAGE_WARN = 300
MESSAGE_ERROR = 700
MESSAGE_DEBUG = 900
message_dictionary = {
"100": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}I",
"101": "{0}",
"120": "Sleeping for requested delay of {0} seconds.",
"127": "Monitor: {0}",
"129": "{0} is running.",
"130": "RabbitMQ channel closed by the broker. Shutting down thread {0}.",
"152": "Sleeping {0} seconds before deploying administrative threads.",
"190": "AWS SQS Long-polling: No messages from {0}",
"293": "For information on warnings and errors, see https://github.com/senzing-garage/stream-logger#errors",
"294": "Version: {0} Updated: {1}",
"295": "Sleeping infinitely.",
"296": "Sleeping {0} seconds.",
"297": "Enter {0}",
"298": "Exit {0}",
"299": "{0}",
"300": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}W",
"499": "{0}",
"500": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}E",
"561": "Unknown RabbitMQ error when connecting: {0}.",
"562": "Could not connect to RabbitMQ host at {1}. The host name maybe wrong, it may not be ready, or your credentials are incorrect. See the RabbitMQ log for more details. Error: {0}",
"696": "Bad SENZING_SUBCOMMAND: {0}.",
"697": "No processing done.",
"698": "Program terminated with error.",
"699": "{0}",
"700": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}E",
"721": "Running low on workers. May need to restart",
"722": "Kafka commit failed for {0}",
"899": "{0}",
"900": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}D",
"904": "{0} processed: {1}",
"999": "{0}",
}
def message(index, *args):
index_string = str(index)
template = message_dictionary.get(index_string, "No message for index {0}.".format(index_string))
return template.format(*args)
def message_generic(generic_index, index, *args):
index_string = str(index)
return "{0} {1}".format(message(generic_index, index), message(index, *args))
def message_info(index, *args):
return message_generic(MESSAGE_INFO, index, *args)
def message_warning(index, *args):
return message_generic(MESSAGE_WARN, index, *args)
def message_error(index, *args):
return message_generic(MESSAGE_ERROR, index, *args)
def message_debug(index, *args):
return message_generic(MESSAGE_DEBUG, index, *args)
def get_exception():
''' Get details about an exception. '''
exception_type, exception_object, traceback = sys.exc_info()
frame = traceback.tb_frame
line_number = traceback.tb_lineno
filename = frame.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, line_number, frame.f_globals)
return {
"filename": filename,
"line_number": line_number,
"line": line.strip(),
"exception": exception_object,
"type": exception_type,
"traceback": traceback,
}
# -----------------------------------------------------------------------------
# Configuration
# -----------------------------------------------------------------------------
def get_configuration(args):
''' Order of precedence: CLI, OS environment variables, INI file, default. '''
result = {}
# Copy default values into configuration dictionary.
for key, value in list(configuration_locator.items()):
result[key] = value.get('default', None)
# "Prime the pump" with command line args. This will be done again as the last step.
for key, value in list(args.__dict__.items()):
new_key = key.format(subcommand.replace('-', '_'))
if value:
result[new_key] = value
# Copy OS environment variables into configuration dictionary.
for key, value in list(configuration_locator.items()):
os_env_var = value.get('env', None)
if os_env_var:
os_env_value = os.getenv(os_env_var, None)
if os_env_value:
result[key] = os_env_value
# Copy 'args' into configuration dictionary.
for key, value in list(args.__dict__.items()):
new_key = key.format(subcommand.replace('-', '_'))
if value:
result[new_key] = value
# Add program information.
result['program_version'] = __version__
result['program_updated'] = __updated__
# Special case: subcommand from command-line
if args.subcommand:
result['subcommand'] = args.subcommand
# Special case: Change boolean strings to booleans.
booleans = [
'debug',
'rabbitmq_use_existing_entities',
]
for boolean in booleans:
boolean_value = result.get(boolean)
if isinstance(boolean_value, str):
boolean_value_lower_case = boolean_value.lower()
if boolean_value_lower_case in ['true', '1', 't', 'y', 'yes']:
result[boolean] = True
else:
result[boolean] = False
# Special case: Change integer strings to integers.
integers = [
"delay_in_seconds",
"monitoring_period_in_seconds",
"threads_per_process",
]
for integer in integers:
integer_string = result.get(integer)
result[integer] = int(integer_string)
# Initialize counters.
result['counter_processed_messages'] = 0
return result
def validate_configuration(config):
''' Check aggregate configuration from commandline options, environment variables, config files, and defaults. '''
user_warning_messages = []
user_error_messages = []
# Perform subcommand specific checking.
subcommand = config.get('subcommand')
if subcommand in ['rabbitmq', 'kafka', 'sqs']:
pass
# Log warning messages.
for user_warning_message in user_warning_messages:
logging.warning(user_warning_message)
# Log error messages.
for user_error_message in user_error_messages:
logging.error(user_error_message)
# Log where to go for help.
if len(user_warning_messages) > 0 or len(user_error_messages) > 0:
logging.info(message_info(293))
# If there are error messages, exit.
if len(user_error_messages) > 0:
exit_error(697)
def redact_configuration(config):
''' Return a shallow copy of config with certain keys removed. '''
result = config.copy()
for key in keys_to_redact:
try:
result.pop(key)
except:
pass
return result
# -----------------------------------------------------------------------------
# Class: ReadThread
# -----------------------------------------------------------------------------
class ReadThread(threading.Thread):
def __init__(self, config):
threading.Thread.__init__(self)
self.config = config
# -----------------------------------------------------------------------------
# Class: ReadKafkaThread
# -----------------------------------------------------------------------------
class ReadKafkaThread(ReadThread):
def __init__(self, config):
super().__init__(config)
def run(self):
'''Process for reading lines from Kafka and feeding them to a process_function() function.'''
logging.info(message_info(129, threading.current_thread().name))
# Create Kafka client.
consumer_configuration = {
'bootstrap.servers': self.config.get('kafka_bootstrap_server'),
'group.id': self.config.get("kafka_group"),
'enable.auto.commit': False,
'auto.offset.reset': 'earliest'
}
consumer = confluent_kafka.Consumer(consumer_configuration)
consumer.subscribe([self.config.get("kafka_topic")])
# In a loop, get messages from Kafka.
while True:
# Get message from Kafka queue.
# Timeout quickly to allow other co-routines to process.
kafka_message = consumer.poll(1.0)
# Handle non-standard Kafka output.
if kafka_message is None:
continue
if kafka_message.error():
if kafka_message.error().code() == confluent_kafka.KafkaError._PARTITION_EOF:
continue
else:
logging.error(message_error(722, kafka_message.error()))
continue
# Construct and verify Kafka message.
kafka_message_string = kafka_message.value().strip()
if not kafka_message_string:
continue
if isinstance(kafka_message_string, bytes):
kafka_message_string = kafka_message_string.decode()
logging.debug(message_debug(904, threading.current_thread().name, kafka_message_string))
self.config['counter_processed_messages'] += 1
# Write message to log.
logging.info(message_info(101, kafka_message_string))
consumer.commit()
consumer.close()
# -----------------------------------------------------------------------------
# Class: ReadRabbitMQThread
# -----------------------------------------------------------------------------
class ReadRabbitMQThread(ReadThread):
def __init__(self, config):
super().__init__(config)
def callback(self, channel, method, header, body):
''' Called by Pika whenever a message is received. '''
jsonline = body.decode()
logging.debug(message_debug(904, threading.current_thread().name, jsonline))
self.config['counter_processed_messages'] += 1
logging.info(message_info(101, jsonline))
channel.basic_ack(delivery_tag=method.delivery_tag)
def run(self):
'''Process for reading lines from RabbitMQ and feeding them to a process_function() function.'''
logging.info(message_info(129, threading.current_thread().name))
# Get config parameters.
rabbitmq_queue = self.config.get("rabbitmq_queue")
rabbitmq_passive_declare = self.config.get("rabbitmq_use_existing_entities")
rabbitmq_username = self.config.get("rabbitmq_username")
rabbitmq_password = self.config.get("rabbitmq_password")
rabbitmq_host = self.config.get("rabbitmq_host")
rabbitmq_prefetch_count = self.config.get("rabbitmq_prefetch_count")
# Connect to RabbitMQ queue.
try:
credentials = pika.PlainCredentials(rabbitmq_username, rabbitmq_password)
connection = pika.BlockingConnection(pika.ConnectionParameters(host=rabbitmq_host, credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue=rabbitmq_queue, passive=rabbitmq_passive_declare)
channel.basic_qos(prefetch_count=rabbitmq_prefetch_count)
channel.basic_consume(on_message_callback=self.callback, queue=rabbitmq_queue)
except pika.exceptions.AMQPConnectionError as err:
exit_error(562, err, rabbitmq_host)
except BaseException as err:
exit_error(561, err)
# Start consuming.
try:
channel.start_consuming()
except pika.exceptions.ChannelClosed:
logging.info(message_info(130, threading.current_thread().name))
# -----------------------------------------------------------------------------
# Class: ReadSqsThread
# -----------------------------------------------------------------------------
class ReadSqsThread(ReadThread):
def __init__(self, config):
super().__init__(config)
self.queue_url = config.get("sqs_queue_url")
self.sqs = boto3.client("sqs")
def run(self):
'''Process for reading lines from SQS and feeding them to a process_function() function.'''
logging.info(message_info(129, threading.current_thread().name))
# In a loop, get messages from AWS SQS.
while True:
# Get message from AWS SQS queue.
sqs_response = self.sqs.receive_message(
QueueUrl=self.queue_url,
AttributeNames=[],
MaxNumberOfMessages=1,
MessageAttributeNames=[],
VisibilityTimeout=30,
WaitTimeSeconds=20
)
# If non-standard SQS output or empty messages, just loop.
if sqs_response is None:
continue
sqs_messages = sqs_response.get("Messages", [])
if not sqs_messages:
logging.info(message_info(190, self.queue_url))
continue
# Construct and verify SQS message.
sqs_message = sqs_messages[0]
sqs_message_body = sqs_message.get("Body")
sqs_message_receipt_handle = sqs_message.get("ReceiptHandle")
# Write message to log.
logging.info(message_info(101, sqs_message_body))
# After successful import into Senzing, tell AWS SQS we're done with message.
self.sqs.delete_message(
QueueUrl=self.queue_url,
ReceiptHandle=sqs_message_receipt_handle
)
# Count the processing
self.config['counter_processed_messages'] += 1
# -----------------------------------------------------------------------------
# Class: MonitorThread
# -----------------------------------------------------------------------------
class MonitorThread(threading.Thread):
def __init__(self, config, workers):
threading.Thread.__init__(self)
self.config = config
self.workers = workers
def run(self):
'''Periodically monitor what is happening.'''
last_processed_records = 0
last_time = time.time()
# Define monitoring report interval.
sleep_time_in_seconds = self.config.get('monitoring_period_in_seconds')
# Sleep-monitor loop.
active_workers = len(self.workers)
for worker in self.workers:
if not worker.is_alive():
active_workers -= 1
while active_workers > 0:
time.sleep(sleep_time_in_seconds)
# Calculate active Threads.
active_workers = len(self.workers)
for worker in self.workers:
if not worker.is_alive():
active_workers -= 1
# Determine if we're running out of workers.
if (active_workers / float(len(self.workers))) < 0.5:
logging.warning(message_warning(721))
# Calculate times.
now = time.time()
uptime = now - self.config.get('start_time', now)
elapsed_time = now - last_time
# Calculate rates.
processed_messages_total = self.config['counter_processed_messages']
processed_messages_interval = processed_messages_total - last_processed_records
rate_processed_total = int(processed_messages_total / uptime)
rate_processed_interval = int(processed_messages_interval / elapsed_time)
# Construct and log monitor statistics.
stats = {
"processed_messages_interval": processed_messages_interval,
"processed_messages_total": processed_messages_total,
"rate_processed_interval": rate_processed_interval,
"rate_processed_total": rate_processed_total,
"uptime": int(uptime),
"workers_total": len(self.workers),
"workers_active": active_workers,
}
logging.info(message_info(127, json.dumps(stats, sort_keys=True)))
# Store values for next iteration of loop.
last_processed_records = processed_messages_total
last_time = now
# -----------------------------------------------------------------------------
# Utility functions
# -----------------------------------------------------------------------------
def bootstrap_signal_handler(signal, frame):
sys.exit(0)
def create_signal_handler_function(args):
''' Tricky code. Uses currying technique. Create a function for signal handling.
that knows about "args".
'''
def result_function(signal_number, frame):
logging.info(message_info(298, args))
sys.exit(0)
return result_function
def delay(config):
delay_in_seconds = config.get('delay_in_seconds')
if delay_in_seconds > 0:
logging.info(message_info(120, delay_in_seconds))
time.sleep(delay_in_seconds)
def entry_template(config):
''' Format of entry message. '''
debug = config.get("debug", False)
config['start_time'] = time.time()
if debug:
final_config = config
else:
final_config = redact_configuration(config)
config_json = json.dumps(final_config, sort_keys=True)
return message_info(297, config_json)
def exit_template(config):
''' Format of exit message. '''
debug = config.get("debug", False)
stop_time = time.time()
config['stop_time'] = stop_time
config['elapsed_time'] = stop_time - config.get('start_time', stop_time)
if debug:
final_config = config
else:
final_config = redact_configuration(config)
config_json = json.dumps(final_config, sort_keys=True)
return message_info(298, config_json)
def exit_error(index, *args):
''' Log error message and exit program. '''
logging.error(message_error(index, *args))
logging.error(message_error(698))
sys.exit(1)
def exit_silently():
''' Exit program. '''
sys.exit(0)
# -----------------------------------------------------------------------------
# dohelper_* functions
# -----------------------------------------------------------------------------
def dohelper_thread_runner(args, threadClass):
''' Performs threadClass. '''
# Get context from CLI, environment variables, and ini files.
config = get_configuration(args)
# Prolog.
logging.info(entry_template(config))
# If requested, delay start.
delay(config)
# Pull values from configuration.
threads_per_process = config.get('threads_per_process')
# Create queue reader threads for master process.
threads = []
for i in range(0, threads_per_process):
thread = threadClass(config)
thread.name = "{0}-0-thread-{1}".format(threadClass.__name__, i)
threads.append(thread)
# Create monitor thread for master process.
adminThreads = []
thread = MonitorThread(config, threads)
thread.name = "{0}-0-thread-monitor".format(threadClass.__name__)
adminThreads.append(thread)
# Start threads for master process.
for thread in threads:
thread.start()
# Sleep, if requested.
sleep_time_in_seconds = config.get('sleep_time_in_seconds')
if sleep_time_in_seconds > 0:
logging.info(message_info(152, sleep_time_in_seconds))
time.sleep(sleep_time_in_seconds)
# Start administrative threads for master process.
for thread in adminThreads:
thread.start()
# Collect inactive threads from master process.
for thread in threads:
thread.join()
# Epilog.
logging.info(exit_template(config))
# -----------------------------------------------------------------------------
# do_* functions
# Common function signature: do_XXX(args)
# -----------------------------------------------------------------------------
def do_docker_acceptance_test(args):
''' For use with Docker acceptance testing. '''
# Get context from CLI, environment variables, and ini files.
config = get_configuration(args)
# Prolog.
logging.info(entry_template(config))
# Epilog.
logging.info(exit_template(config))
def do_kafka(args):
''' Read from Kafka. '''
dohelper_thread_runner(args, ReadKafkaThread)
def do_rabbitmq(args):
''' Read from rabbitmq. '''
dohelper_thread_runner(args, ReadRabbitMQThread)
def do_sleep(args):
''' Sleep. Used for debugging. '''
# Get context from CLI, environment variables, and ini files.
config = get_configuration(args)
# Prolog.
logging.info(entry_template(config))
# Pull values from configuration.
sleep_time_in_seconds = config.get('sleep_time_in_seconds')
# Sleep
if sleep_time_in_seconds > 0:
logging.info(message_info(296, sleep_time_in_seconds))
time.sleep(sleep_time_in_seconds)
else:
sleep_time_in_seconds = 3600
while True:
logging.info(message_info(295))
time.sleep(sleep_time_in_seconds)
# Epilog.
logging.info(exit_template(config))
def do_sqs(args):
''' Read from AWS SQS. '''
dohelper_thread_runner(args, ReadSqsThread)
def do_version(args):
''' Log version information. '''
logging.info(message_info(294, __version__, __updated__))
# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------
if __name__ == "__main__":
# Configure logging. See https://docs.python.org/2/library/logging.html#levels
log_level_map = {
"notset": logging.NOTSET,
"debug": logging.DEBUG,
"info": logging.INFO,
"fatal": logging.FATAL,
"warning": logging.WARNING,
"error": logging.ERROR,
"critical": logging.CRITICAL
}
log_level_parameter = os.getenv("SENZING_LOG_LEVEL", "info").lower()
log_level = log_level_map.get(log_level_parameter, logging.INFO)
logging.basicConfig(format=log_format, level=log_level)
# Trap signals temporarily until args are parsed.
signal.signal(signal.SIGTERM, bootstrap_signal_handler)
signal.signal(signal.SIGINT, bootstrap_signal_handler)
# Parse the command line arguments.
subcommand = os.getenv("SENZING_SUBCOMMAND", None)
parser = get_parser()
if len(sys.argv) > 1:
args = parser.parse_args()
subcommand = args.subcommand
elif subcommand: