2021-10-29 18:48:12 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
"""
|
|
|
|
The SYNC Module
|
|
|
|
================================
|
|
|
|
|
|
|
|
This module .
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
##################################
|
|
|
|
# Import External packages
|
|
|
|
##################################
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from core import ail_2_ail
|
2022-11-28 15:01:40 +01:00
|
|
|
from lib.objects.Items import Item
|
2021-10-29 18:48:12 +02:00
|
|
|
from modules.abstract_module import AbstractModule
|
|
|
|
|
|
|
|
|
|
|
|
class Sync_module(AbstractModule):
|
|
|
|
"""
|
|
|
|
Sync_module module for AIL framework
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(Sync_module, self).__init__()
|
|
|
|
|
2022-11-28 15:01:40 +01:00
|
|
|
# Waiting time in seconds between to message processed
|
2021-10-29 18:48:12 +02:00
|
|
|
self.pending_seconds = 10
|
|
|
|
|
|
|
|
self.dict_sync_queues = ail_2_ail.get_all_sync_queue_dict()
|
|
|
|
self.last_refresh = time.time()
|
|
|
|
|
|
|
|
print(self.dict_sync_queues)
|
|
|
|
|
|
|
|
# Send module state to logs
|
2023-05-12 15:29:53 +02:00
|
|
|
self.logger.info(f'Module {self.module_name} Launched')
|
2021-10-29 18:48:12 +02:00
|
|
|
|
|
|
|
def compute(self, message):
|
|
|
|
|
|
|
|
### REFRESH DICT
|
2021-11-22 23:45:41 +01:00
|
|
|
if self.last_refresh < ail_2_ail.get_last_updated_sync_config():
|
2021-10-29 18:48:12 +02:00
|
|
|
self.last_refresh = time.time()
|
2021-11-22 23:45:41 +01:00
|
|
|
self.dict_sync_queues = ail_2_ail.get_all_sync_queue_dict()
|
|
|
|
print('sync queues refreshed')
|
|
|
|
print(self.dict_sync_queues)
|
2021-10-29 18:48:12 +02:00
|
|
|
|
|
|
|
# Extract object from message
|
|
|
|
# # TODO: USE JSON DICT ????
|
|
|
|
mess_split = message.split(';')
|
|
|
|
if len(mess_split) == 3:
|
|
|
|
obj_type = mess_split[0]
|
|
|
|
obj_subtype = mess_split[1]
|
|
|
|
obj_id = mess_split[2]
|
|
|
|
|
|
|
|
# OBJECT => Item
|
2022-11-28 15:01:40 +01:00
|
|
|
# if obj_type == 'item':
|
|
|
|
obj = Item(obj_id)
|
|
|
|
|
|
|
|
tags = obj.get_tags()
|
2021-10-29 18:48:12 +02:00
|
|
|
|
|
|
|
# check filter + tags
|
2022-11-28 15:01:40 +01:00
|
|
|
# print(message)
|
2021-10-29 18:48:12 +02:00
|
|
|
for queue_uuid in self.dict_sync_queues:
|
|
|
|
filter_tags = self.dict_sync_queues[queue_uuid]['filter']
|
|
|
|
if filter_tags and tags:
|
2022-11-28 15:01:40 +01:00
|
|
|
# print('tags: {tags} filter: {filter_tags}')
|
2022-05-18 14:59:51 +02:00
|
|
|
if filter_tags.issubset(tags):
|
2021-10-29 18:48:12 +02:00
|
|
|
obj_dict = obj.get_default_meta()
|
|
|
|
# send to queue push and/or pull
|
|
|
|
for dict_ail in self.dict_sync_queues[queue_uuid]['ail_instances']:
|
2022-05-18 14:59:51 +02:00
|
|
|
print(f'ail_uuid: {dict_ail["ail_uuid"]} obj: {message}')
|
2021-10-29 18:48:12 +02:00
|
|
|
ail_2_ail.add_object_to_sync_queue(queue_uuid, dict_ail['ail_uuid'], obj_dict,
|
2022-11-28 15:01:40 +01:00
|
|
|
push=dict_ail['push'], pull=dict_ail['pull'])
|
2021-10-29 18:48:12 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Malformed message
|
|
|
|
raise Exception(f'too many values to unpack (expected 3) given {len(mess_split)} with message {message}')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
module = Sync_module()
|
|
|
|
module.run()
|