Skip to content

Commit

Permalink
add tmp file cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
GeneralTao2 committed Mar 24, 2024
1 parent acdc25e commit 8f14489
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ForwardingService (
try {
forwardGroupContentIfEnabled(vkBotGroupDetailsEntity)
} catch (e: Exception) {
logger.error { "Error occurred while forwarding process: ${e.message}" }
logger.error(e) { "Error occurred while forwarding process" }
cacheService.clearCache()
}
logger.debug { "Finish forwarding for the group" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.vk.api.sdk.objects.stories.Story
import com.vk.api.sdk.objects.stories.StoryType
import mu.KotlinLogging
import org.openqa.selenium.TimeoutException
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import org.taonity.vkforwarderbot.CacheService
import org.taonity.vkforwarderbot.exceptions.DbUnexpectedResponseException
Expand Down Expand Up @@ -36,20 +37,25 @@ class StoryForwardingService (
private val seleniumService: SeleniumService,
private val vkGroupDetailsRepository: VkGroupDetailsRepository,
private val cacheService: CacheService,
@Value("\${forwarder.debug.max-stories-to-process}") private val maxStoriesToProcess: Int
) {
fun forwardStories(vkBotGroupDetails: VkGroupDetailsEntity) {
val stories = retrieveStories(vkBotGroupDetails)
?: return

logger.debug { "${stories.size} stories are ready to forward" }
if (stories.isEmpty()) {
logger.debug { "${stories.size} stories before trim" }

val trimmedStories = stories.take(maxStoriesToProcess)

logger.debug { "${trimmedStories.size} stories are ready to forward" }
if (trimmedStories.isEmpty()) {
return
}

forwardStories(stories, vkBotGroupDetails)
forwardStories(trimmedStories, vkBotGroupDetails)
}

private fun forwardStories(stories: MutableList<Story>, vkBotGroupDetails: VkGroupDetailsEntity) {
private fun forwardStories(stories: List<Story>, vkBotGroupDetails: VkGroupDetailsEntity) {
val seleniumVkWalker = seleniumService.buildVkWalker()
// TODO: refactor to use several groups
try {
Expand All @@ -64,7 +70,7 @@ class StoryForwardingService (

private fun forwardStoriesUsingSeleniumVkWalker(
seleniumVkWalker: SeleniumVkWalker,
stories: MutableList<Story>,
stories: List<Story>,
vkBotGroupDetails: VkGroupDetailsEntity
) {
loginIntoVkWith2Attempts(seleniumVkWalker)
Expand Down Expand Up @@ -98,7 +104,7 @@ class StoryForwardingService (
}
}

private fun divideStoriesOnChunks(stories: MutableList<Story>): MutableCollection<MutableList<Story>> {
private fun divideStoriesOnChunks(stories: List<Story>): MutableCollection<MutableList<Story>> {
val counter = AtomicInteger()
return stories.stream()
.collect(Collectors.groupingBy { counter.getAndIncrement() / STORY_CHUNK_SIZE })
Expand Down Expand Up @@ -149,7 +155,7 @@ class StoryForwardingService (
if (storyVideoFile.exists()) {
tgService.sendVideo(storyVideoFile, null, tgTargetId)
} else {
println("Failed to download video")
logger.error{ "Failed to download video, video not found in cache" }
}
}
cacheService.clearCache()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class SeleniumService (
@Value("\${forwarder.vk.username}") private val vkUsername: String,
@Value("\${forwarder.vk.password}") private val vkPassword: String,
@Value("\${forwarder.vk.selenium.browser-log-file-enabled}") private val browserLogFileEnabled: Boolean,
@Value("\${forwarder.vk.selenium.tmp-dir-cleaning-enabled}") private val tmpDirCleaningEnabled: Boolean,
) {
fun buildVkWalker() : SeleniumVkWalker {
return SeleniumVkWalker(vkUsername, vkPassword, cacheService.cacheDirPath, browserLogFileEnabled)
return SeleniumVkWalker(vkUsername, vkPassword, cacheService.cacheDirPath, browserLogFileEnabled, tmpDirCleaningEnabled)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.taonity.vkforwarderbot.vk.selenium

import com.vk.api.sdk.objects.stories.Story
import mu.KotlinLogging
import org.apache.commons.io.FileUtils
import org.openqa.selenium.*
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxOptions
Expand All @@ -17,7 +18,8 @@ class SeleniumVkWalker (
private val vkUsername: String,
private val vkPassword: String,
private val cacheDirPath: String,
private val browserLogFileEnabled: Boolean
private val browserLogFileEnabled: Boolean,
private val tmpDirCleaningEnabled: Boolean
) {
private lateinit var driver: WebDriver
private lateinit var wait: Wait<WebDriver>
Expand All @@ -40,6 +42,18 @@ class SeleniumVkWalker (

fun quit() {
driver.quit()
logger.debug { "Driver have been quit" }
cleanTmpDirIfEnabled()

}

private fun cleanTmpDirIfEnabled() {
if (tmpDirCleaningEnabled) {
FileUtils.cleanDirectory(File("/tmp"))
logger.debug { "/tmp dir have been cleaned" }
} else {
logger.debug { "/tmp dir cleaning disabled" }
}
}

private fun buildFirefoxDriver(): WebDriver {
Expand Down Expand Up @@ -114,4 +128,5 @@ class SeleniumVkWalker (
logger.debug { "Top profile link loaded" }
}


}
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ forwarder:
password: ${FORWARDER_VK_PASSWORD}
selenium:
browser-log-file-enabled: false
tmp-dir-cleaning-enabled: ${FORWARDER_VK_SELENIUM_TMP_DIR_CLEANING_ENABLED:false}
tg:
token: ${FORWARDER_TG_TOKEN}
bot-username: ${FORWARDER_TG_BOT_USERNAME}
cache-dir-path: ${FORWARDER_CACHE_DIR_PATH:cache}
yt-ylp-file-path: ${FORWARDER_YT_YLP_FILE_PATH:src/dev/yt-dlp.exe}
debug:
max-stories-to-process: 50



Expand Down
1 change: 1 addition & 0 deletions src/templates/docker/.test-env
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ FORWARDER_VK_USER_ID=%{FORWARDER_VK_USER_ID}
FORWARDER_VK_GROUP_ID=%{FORWARDER_VK_GROUP_ID}
FORWARDER_VK_USERNAME=%{FORWARDER_VK_USERNAME}
FORWARDER_VK_PASSWORD=%{FORWARDER_VK_PASSWORD}
FORWARDER_VK_SELENIUM_TMP_DIR_CLEANING_ENABLED=true
FORWARDER_TG_TOKEN=%{FORWARDER_TG_TOKEN}
FORWARDER_TG_BOT_USERNAME=%{FORWARDER_TG_BOT_USERNAME}
FORWARDER_TG_TARGET_USER_ID=%{FORWARDER_TG_TARGET_USER_ID}
Expand Down
29 changes: 29 additions & 0 deletions src/test/kotlin/automation/runner/CucumberRunnerIT.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package automation.runner

import automation.services.VkGroupService
import automation.services.VkTestBotConfig
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit.jupiter.SpringExtension

@ExtendWith(SpringExtension::class)
@ActiveProfiles("at")
@ContextConfiguration(
initializers = [ConfigDataApplicationContextInitializer::class],
classes = [
VkGroupService::class,
VkTestBotConfig::class
]
)
class CucumberRunnerIT (
@Autowired private val vkGroupService: VkGroupService
) {
@Test
fun test1() {
vkGroupService.post()
}
}
19 changes: 19 additions & 0 deletions src/test/kotlin/automation/services/VkGroupService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package automation.services

import com.vk.api.sdk.client.VkApiClient
import com.vk.api.sdk.client.actors.UserActor
import org.springframework.stereotype.Component

@Component
class VkGroupService (
private val vkApiClient: VkApiClient,
private val userActor: UserActor
) {
fun post() {
vkApiClient.wall()
.post(userActor)
.ownerId(224746392)
.message("test")
.execute()
}
}
27 changes: 27 additions & 0 deletions src/test/kotlin/automation/services/VkTestBotConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package automation.services

import com.vk.api.sdk.client.TransportClient
import com.vk.api.sdk.client.VkApiClient
import com.vk.api.sdk.client.actors.UserActor
import com.vk.api.sdk.httpclient.HttpTransportClient
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class VkTestBotConfig(
@Value("\${forwarder.at.vk.token}") private val vkToken: String,
@Value("\${forwarder.at.vk.user-id}") private val vkUserId: Long
) {

@Bean
fun vkApiClient(): VkApiClient {
val transportClient: TransportClient = HttpTransportClient()
return VkApiClient(transportClient)
}

@Bean
fun userActor(): UserActor {
return UserActor(vkUserId, vkToken)
}
}
16 changes: 16 additions & 0 deletions src/test/resources/application-at.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---

postgres-url: jdbc:postgresql://${POSTGRES_ADDRESS:localhost}:${POSTGRES_PORT:5433}/${POSTGRES_DB:vk_forwarder_bot}

spring:
datasource:
url: ${postgres-url}
username: ${POSTGRES_APP_USER:d}
password: ${POSTGRES_APP_PASSWORD:d}

forwarder:
at:
vk:
token: vk1.a.ba0S2riE8j2TtehMCLFd39Jez8LwYoQ8VWN0pjV_W_A5KuXf7nh9DAm79vEW4EhhNGNAXhunKLtZ6fwuKU3aw5PL0aBrmo4mudqggC9TrpmEIu9w3LvLGHXBodAP9fdGjDSwNG6W3Ofpgn_EWXKtFxUj6g-m0iNi83xZ-2XVBaK-sOajrrUdx1OWW9bATAi959m83Afnx1hnWE5hp3yd_g
user-id: 840295095

0 comments on commit 8f14489

Please sign in to comment.