| 1 | from datetime import datetime |
| 2 | |
| 3 | from loguru import logger |
| 4 | from sqlalchemy.future import select |
| 5 | |
| 6 | from app.connectors.utils import is_connector_verified |
| 7 | from app.connectors.wazuh_indexer.routes.monitoring import ( |
| 8 | resize_wazuh_index_fields_route, |
| 9 | ) |
| 10 | from app.db.db_session import get_db_session |
| 11 | from app.schedulers.models.scheduler import JobMetadata |
| 12 | |
| 13 | |
| 14 | async def resize_wazuh_index_fields(): |
| 15 | """ |
| 16 | Synchronizes agents by sending a request to the server and updating the job metadata. |
| 17 | |
| 18 | This function retrieves the scheduler auth token, sends a POST request to the server |
| 19 | to synchronize agents, and updates the job metadata with the last success timestamp. |
| 20 | |
| 21 | If the token retrieval fails, it prints a failure message. If the job metadata for |
| 22 | 'resize_wazuh_index_fields' does not exist, it prints a message indicating the absence of the metadata. |
| 23 | """ |
| 24 | logger.info("Resizing Wazuh index fields via scheduler...") |
| 25 | async with get_db_session() as session: |
| 26 | if not await is_connector_verified("Wazuh-Indexer", session): |
| 27 | logger.warning("Wazuh Indexer connector is not verified.") |
| 28 | return None |
| 29 | logger.info("Wazuh Indexer connector is verified.") |
| 30 | await resize_wazuh_index_fields_route() |
| 31 | |
| 32 | stmt = select(JobMetadata).where(JobMetadata.job_id == "resize_wazuh_index_fields") |
| 33 | result = await session.execute(stmt) |
| 34 | job_metadata = result.scalars().first() |
| 35 | |
| 36 | if job_metadata: |
| 37 | job_metadata.last_success = datetime.utcnow() |
| 38 | session.add(job_metadata) |
| 39 | await session.commit() # Asynchronously commit the transaction |
| 40 | logger.info("Updated job metadata with the last success timestamp.") |
| 41 | else: |
| 42 | logger.warning("JobMetadata for 'resize_wazuh_index_fields' not found.") |