Skip to content

Commit

Permalink
Clean empty group IDs (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
omaryasser authored May 11, 2024
1 parent 9b9187b commit 81a8780
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 3 deletions.
23 changes: 23 additions & 0 deletions deploy-jobs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# Note: Make sure that the desired jobs are marked true in application.yaml

# Exit script when a command fails
set -e

./mvnw package -Dmaven.test.skip=true

docker build --platform linux/amd64 -t tanafaso .

random_tag=$(printf "randomtag%08d" $((RANDOM%100000000)))

docker tag tanafaso europe-west1-docker.pkg.dev/tanafaso/tanafaso-jobs/tanafaso-jobs:$random_tag

docker push europe-west1-docker.pkg.dev/tanafaso/tanafaso-jobs/tanafaso-jobs:$random_tag

sed -i '' "s|image: europe-west1-docker.pkg.dev/tanafaso/tanafaso-jobs/tanafaso-jobs:.*|image: europe-west1-docker.pkg.dev/tanafaso/tanafaso-jobs/tanafaso-jobs:$random_tag|" tanafaso-cloud-run-jobs.yaml

gcloud run jobs replace tanafaso-cloud-run-jobs.yaml

echo "Deployed with tag: $random_tag"

2 changes: 1 addition & 1 deletion src/main/java/com/azkar/crons/ChallengesCleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ChallengesCleaner implements ApplicationRunner {
@Autowired
private ApplicationContext appContext;

@Value("${job-mode}")
@Value("${challenges-cleaner-job-run-mode}")
public boolean jobMode;

// Run every while to clean old challenges. Note that although after every challenge creation
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/com/azkar/crons/FriendshipEmptyGroupIdCleaner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.azkar.crons;

import com.azkar.entities.Friendship;
import com.azkar.entities.Friendship.Friend;
import com.azkar.repos.FriendshipRepo;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

@Component
public class FriendshipEmptyGroupIdCleaner implements ApplicationRunner {

private static final Logger logger = LoggerFactory.getLogger(FriendshipEmptyGroupIdCleaner.class);
private static final int FRIENDSHIPS_BATCH_SIZE = 100;
@Value("${friendship-empty-group-id-cleaner-job-run-mode}")
public boolean jobMode;
@Autowired
private FriendshipRepo friendshipRepo;
@Autowired
private ApplicationContext appContext;

// This is a cleanup after the fix https://github.com/tanafaso/tanafaso-backend/pull/463.
@Override
public void run(ApplicationArguments args) throws Exception {
if (!jobMode) {
logger.info(
"[Empty group ID cleaner] skipping as the application is not running in job mode");
return;
}

logger.info("[Empty group ID cleaner] started!");

long numberOfFriendships = friendshipRepo.count();
logger.info("[Empty group ID cleaner] number of friendships to be processed: {}",
numberOfFriendships);
logger.info("[Empty group ID cleaner] friendships will be processed in batches of {}",
FRIENDSHIPS_BATCH_SIZE);

long numberOfBatches =
(numberOfFriendships + FRIENDSHIPS_BATCH_SIZE - 1) / FRIENDSHIPS_BATCH_SIZE;
logger.info("[Empty group ID cleaner] number of batches to be processed: {}", numberOfBatches);

AtomicInteger cleanedFriendshps = new AtomicInteger();
for (int batch = 0; batch < numberOfBatches; batch++) {
Page<Friendship> page = friendshipRepo.findAll(PageRequest.of(batch, FRIENDSHIPS_BATCH_SIZE));

List<Friendship> modifiedFriendships = page.get().map(friendship -> {
int friendsCountBeforeCleaning = friendship.getFriends().size();

List<Friend> cleanedFriendships =
friendship.getFriends().stream().filter(friend -> friend.getGroupId() != null)
.collect(Collectors.toList());

int friendsCountAfterCleaning = cleanedFriendships.size();
int cleanedFriendshipsCount = friendsCountBeforeCleaning - friendsCountAfterCleaning;
if (cleanedFriendshipsCount != 0) {
friendship.setFriends(cleanedFriendships);
logger.info("[Empty group ID cleaner] cleaned {} friendships for user {}",
cleanedFriendshipsCount, friendship.getUserId());
}
cleanedFriendshps.addAndGet((cleanedFriendshipsCount));
return friendship;
}).collect(Collectors.toList());

friendshipRepo.saveAll(modifiedFriendships);

logger.info("[Empty group ID cleaner] processed {}/{} batches", batch + 1, numberOfBatches);
}
SpringApplication.exit(appContext, () -> 0);
}
}
4 changes: 3 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ files:
quran-metadata: quran_metadata.csv
apple_auth_private_key: sign_in_with_apple_auth_key

job-mode: false
challenges-cleaner-job-run-mode: false
friendship-empty-group-id-cleaner-job-run-mode: true

21 changes: 21 additions & 0 deletions tanafaso-cloud-run-jobs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: run.googleapis.com/v1
kind: Job
metadata:
name: tanafaso-jobs
namespace: 'tanafaso'
labels:
cloud.googleapis.com/location: europe-west1
annotations:
run.googleapis.com/ingress: all
run.googleapis.com/ingress-status: all
spec:
template:
spec:
parallelism: 1
taskCount: 1
template:
spec:
maxRetries: 0
timeoutSeconds: 3600
containers:
- image: europe-west1-docker.pkg.dev/tanafaso/tanafaso-jobs/tanafaso-jobs:randomtag00027311
2 changes: 1 addition & 1 deletion tanafaso-cloud-run-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ spec:
timeoutSeconds: 15
containers:
- name: tanafaso-1
image: europe-west1-docker.pkg.dev/tanafaso/tanafaso/tanafaso:randomtag00010272
image: europe-west1-docker.pkg.dev/tanafaso/tanafaso/tanafaso:randomtag00020031
ports:
- name: http1
containerPort: 443
Expand Down

0 comments on commit 81a8780

Please sign in to comment.