-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b9187b
commit 81a8780
Showing
6 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/com/azkar/crons/FriendshipEmptyGroupIdCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters