Skip to content

Commit f6f5b83

Browse files
committed
feat(terraform): deploy execution service
1 parent 12edf42 commit f6f5b83

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

backend/execution-service/src/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
const http = require("http");
12
const RabbitMQWorker = require("./rabbitmqWorker");
23
const PubSubWorker = require("./pubsubWorker");
34

45
const useGcp = !!process.env.PUBSUB_PROJECT_ID;
6+
const PORT = process.env.PORT || 8080;
57

68
async function main() {
79
let worker;
@@ -16,16 +18,34 @@ async function main() {
1618

1719
await worker.start();
1820

21+
let server = null;
22+
if (useGcp) {
23+
// Create a minimal HTTP server for GCP Cloud Run health checks
24+
server = http.createServer((req, res) => {
25+
res.writeHead(200, { 'Content-Type': 'application/json' });
26+
res.end(JSON.stringify({
27+
data: 'Hello World!',
28+
}));
29+
});
30+
server.listen(PORT);
31+
}
32+
1933
// Graceful shutdown
2034
process.on("SIGTERM", async () => {
2135
console.log("SIGTERM signal received: closing worker");
2236
await worker.stop();
37+
if (server) {
38+
server.close();
39+
}
2340
process.exit(0);
2441
});
2542

2643
process.on("SIGINT", async () => {
2744
console.log("SIGINT signal received: closing worker");
2845
await worker.stop();
46+
if (server) {
47+
server.close();
48+
}
2949
process.exit(0);
3050
});
3151
}

terraform/main.tf

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ resource "google_redis_instance" "redis" {
7575
depends_on = [google_project_service.apis]
7676
}
7777

78-
# Pub/Sub Topics (Kafka replacement)
78+
# Pub/Sub Topics (Kafka/RabbitMQ replacement)
7979
resource "google_pubsub_topic" "match_topic" {
8080
name = "match_topic"
8181

@@ -100,7 +100,13 @@ resource "google_pubsub_topic" "room_created_topic" {
100100
depends_on = [google_project_service.apis]
101101
}
102102

103-
# Pub/Sub Subscriptions (Kafka replacement)
103+
resource "google_pubsub_topic" "job_execution_topic" {
104+
name = "job_execution_topic"
105+
106+
depends_on = [google_project_service.apis]
107+
}
108+
109+
# Pub/Sub Subscriptions (Kafka/RabbitMQ replacement)
104110
resource "google_pubsub_subscription" "match_sub" {
105111
name = "match_sub"
106112
topic = google_pubsub_topic.match_topic.name
@@ -149,6 +155,18 @@ resource "google_pubsub_subscription" "room_created_sub" {
149155
}
150156
}
151157

158+
resource "google_pubsub_subscription" "job_execution_sub" {
159+
name = "job_execution_sub"
160+
topic = google_pubsub_topic.job_execution_topic.name
161+
162+
ack_deadline_seconds = 20
163+
164+
retry_policy {
165+
minimum_backoff = "10s"
166+
maximum_backoff = "600s"
167+
}
168+
}
169+
152170
# Artifact Registry for Docker images
153171
resource "google_artifact_registry_repository" "docker_repo" {
154172
location = var.region
@@ -465,6 +483,57 @@ resource "google_cloud_run_v2_service" "collaboration_service" {
465483
depends_on = [google_project_service.apis]
466484
}
467485

486+
resource "google_cloud_run_v2_service" "execution_service" {
487+
name = "execution-service"
488+
location = var.region
489+
deletion_protection = false
490+
491+
template {
492+
service_account = google_service_account.service_account.email
493+
494+
containers {
495+
image = "${var.region}-docker.pkg.dev/${var.project_id}/docker-repo/execution-service:latest"
496+
497+
ports {
498+
container_port = 8080
499+
}
500+
501+
env {
502+
name = "CALLBACK_URL"
503+
value = "${google_cloud_run_v2_service.collaboration_service.uri}/api/v1/code/execute-callback"
504+
}
505+
506+
env {
507+
name = "PISTON_URL"
508+
value = "https://emkc.org/api/v2/piston/execute"
509+
}
510+
env {
511+
name = "PUBSUB_PROJECT_ID"
512+
value = var.project_id
513+
}
514+
515+
resources {
516+
limits = {
517+
cpu = "1"
518+
memory = "512Mi"
519+
}
520+
}
521+
}
522+
}
523+
524+
scaling {
525+
min_instance_count = 1
526+
max_instance_count = 5
527+
}
528+
529+
traffic {
530+
type = "TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST"
531+
percent = 100
532+
}
533+
534+
depends_on = [google_project_service.apis]
535+
}
536+
468537
# Cloud Run Service - Video Call Service
469538
resource "google_cloud_run_v2_service" "video_call_service" {
470539
name = "video-call-service"
@@ -590,6 +659,13 @@ resource "google_cloud_run_service_iam_member" "collaboration_service_public" {
590659
member = "allUsers"
591660
}
592661

662+
resource "google_cloud_run_service_iam_member" "execution_service_public" {
663+
service = google_cloud_run_v2_service.execution_service.name
664+
location = google_cloud_run_v2_service.execution_service.location
665+
role = "roles/run.invoker"
666+
member = "allUsers"
667+
}
668+
593669
resource "google_cloud_run_service_iam_member" "video_call_service_public" {
594670
service = google_cloud_run_v2_service.video_call_service.name
595671
location = google_cloud_run_v2_service.video_call_service.location

terraform/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ output "collaboration_service_url" {
2828
value = google_cloud_run_v2_service.collaboration_service.uri
2929
}
3030

31+
output "execution_service_url" {
32+
description = "Execution service URL"
33+
value = google_cloud_run_v2_service.execution_service.uri
34+
}
35+
3136
output "video_call_service_url" {
3237
description = "Video call service URL"
3338
value = google_cloud_run_v2_service.video_call_service.uri

0 commit comments

Comments
 (0)