|
| 1 | +package com.akto.account_job_executor; |
| 2 | + |
| 3 | +import com.akto.account_job_executor.cron.AccountJobsCron; |
| 4 | +import com.akto.dao.context.Context; |
| 5 | +import com.akto.log.LoggerMaker; |
| 6 | + |
| 7 | +/** |
| 8 | + * Main entry point for the AccountJob Executor service. |
| 9 | + * This service polls AccountJob collection via Cyborg API and executes jobs. |
| 10 | + * |
| 11 | + * Required environment variables: |
| 12 | + * - DATABASE_ABSTRACTOR_SERVICE_URL: URL of the Cyborg service (e.g., https://cyborg.akto.io) |
| 13 | + * - DATABASE_ABSTRACTOR_SERVICE_TOKEN: JWT token for authentication |
| 14 | + * |
| 15 | + * Optional environment variables: |
| 16 | + * - AKTO_ACCOUNT_ID: Account ID for context (if needed) |
| 17 | + */ |
| 18 | +public class Main { |
| 19 | + |
| 20 | + private static final LoggerMaker logger = new LoggerMaker(Main.class); |
| 21 | + |
| 22 | + public static void main(String[] args) { |
| 23 | + logger.info("==========================================="); |
| 24 | + logger.info("Starting Account Job Executor Service"); |
| 25 | + logger.info("==========================================="); |
| 26 | + |
| 27 | + // Validate required environment variables |
| 28 | + String cyborgUrl = System.getenv("DATABASE_ABSTRACTOR_SERVICE_URL"); |
| 29 | + String token = System.getenv("DATABASE_ABSTRACTOR_SERVICE_TOKEN"); |
| 30 | + |
| 31 | + if (cyborgUrl == null || cyborgUrl.isEmpty()) { |
| 32 | + logger.error("FATAL: DATABASE_ABSTRACTOR_SERVICE_URL environment variable not set"); |
| 33 | + logger.error("Please set DATABASE_ABSTRACTOR_SERVICE_URL to the Cyborg service URL"); |
| 34 | + System.exit(1); |
| 35 | + } |
| 36 | + |
| 37 | + if (token == null || token.isEmpty()) { |
| 38 | + logger.error("FATAL: DATABASE_ABSTRACTOR_SERVICE_TOKEN environment variable not set"); |
| 39 | + logger.error("Please set DATABASE_ABSTRACTOR_SERVICE_TOKEN to a valid JWT token"); |
| 40 | + System.exit(1); |
| 41 | + } |
| 42 | + |
| 43 | + logger.info("Environment configuration:"); |
| 44 | + logger.info(" Cyborg URL: {}", cyborgUrl); |
| 45 | + logger.info(" Token: {}...", token.substring(0, Math.min(20, token.length()))); |
| 46 | + |
| 47 | + // Set account context if provided |
| 48 | + String accountIdStr = System.getenv("AKTO_ACCOUNT_ID"); |
| 49 | + if (accountIdStr != null && !accountIdStr.isEmpty()) { |
| 50 | + try { |
| 51 | + int accountId = Integer.parseInt(accountIdStr); |
| 52 | + Context.accountId.set(accountId); |
| 53 | + logger.info(" Account ID: {}", accountId); |
| 54 | + } catch (NumberFormatException e) { |
| 55 | + logger.error("Invalid AKTO_ACCOUNT_ID: {}. Must be an integer.", accountIdStr); |
| 56 | + System.exit(1); |
| 57 | + } |
| 58 | + } else { |
| 59 | + logger.info(" Account ID: Not set (will be determined from job context)"); |
| 60 | + } |
| 61 | + |
| 62 | + // Add shutdown hook for graceful shutdown |
| 63 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| 64 | + logger.info("==========================================="); |
| 65 | + logger.info("Shutting down Account Job Executor Service"); |
| 66 | + logger.info("==========================================="); |
| 67 | + |
| 68 | + try { |
| 69 | + AccountJobsCron.instance.shutdown(); |
| 70 | + logger.info("Shutdown complete"); |
| 71 | + } catch (Exception e) { |
| 72 | + logger.error("Error during shutdown", e); |
| 73 | + } |
| 74 | + })); |
| 75 | + |
| 76 | + // Start the job scheduler |
| 77 | + try { |
| 78 | + logger.info("Starting AccountJobsCron scheduler..."); |
| 79 | + AccountJobsCron.instance.startScheduler(); |
| 80 | + |
| 81 | + logger.info("==========================================="); |
| 82 | + logger.info("Account Job Executor Service started successfully (API-only mode)"); |
| 83 | + logger.info("Service is now polling for jobs every 5 seconds"); |
| 84 | + logger.info("Press Ctrl+C to stop"); |
| 85 | + logger.info("==========================================="); |
| 86 | + |
| 87 | + } catch (Exception e) { |
| 88 | + logger.error("FATAL: Failed to start AccountJobsCron scheduler", e); |
| 89 | + System.exit(1); |
| 90 | + } |
| 91 | + |
| 92 | + // Keep main thread alive |
| 93 | + try { |
| 94 | + Thread.currentThread().join(); |
| 95 | + } catch (InterruptedException e) { |
| 96 | + logger.info("Main thread interrupted, shutting down..."); |
| 97 | + Thread.currentThread().interrupt(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments