Skip to content

Commit cfc598b

Browse files
authored
Merge pull request #3782 from akto-api-security/abhi/feat/account-jobs-executor
feat: add account jobs
2 parents 0cfb902 + f9ba95d commit cfc598b

File tree

10 files changed

+1448
-0
lines changed

10 files changed

+1448
-0
lines changed

apps/account-job-executor/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.akto.apps</groupId>
9+
<artifactId>apps</artifactId>
10+
<version>${revision}</version>
11+
</parent>
12+
13+
<groupId>com.akto.apps.account-job-executor</groupId>
14+
<artifactId>account-job-executor</artifactId>
15+
<packaging>jar</packaging>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.akto.libs.dao</groupId>
20+
<artifactId>dao</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.akto.libs.utils</groupId>
25+
<artifactId>utils</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-compiler-plugin</artifactId>
35+
<version>3.1</version>
36+
<configuration>
37+
<source>8</source>
38+
<target>8</target>
39+
</configuration>
40+
</plugin>
41+
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-assembly-plugin</artifactId>
45+
<executions>
46+
<execution>
47+
<phase>package</phase>
48+
<goals>
49+
<goal>single</goal>
50+
</goals>
51+
<configuration>
52+
<archive>
53+
<manifest>
54+
<mainClass>com.akto.account_job_executor.Main</mainClass>
55+
</manifest>
56+
</archive>
57+
<descriptorRefs>
58+
<descriptorRef>jar-with-dependencies</descriptorRef>
59+
</descriptorRefs>
60+
</configuration>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
</plugins>
65+
<sourceDirectory>src/main/java</sourceDirectory>
66+
</build>
67+
</project>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)