forked from Normation/rudder
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
Showing
6 changed files
with
235 additions
and
7 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
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
46 changes: 46 additions & 0 deletions
46
...ventory-repository/src/main/scala/com/normation/inventory/ldap/core/SoftwareService.scala
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,46 @@ | ||
package com.normation.inventory.ldap.core | ||
|
||
|
||
import com.normation.inventory.services.core.{ReadOnlySoftwareDAO, WriteOnlySoftwareDAO} | ||
import net.liftweb.common.Loggable | ||
import net.liftweb.common._ | ||
|
||
|
||
trait SoftwareService { | ||
def deleteUnreferencedSoftware() : Box[Seq[String]] | ||
|
||
} | ||
class SoftwareServiceImpl( | ||
readOnlySoftware: ReadOnlySoftwareDAO | ||
, writeOnlySoftwareDAO: WriteOnlySoftwareDAO) | ||
extends SoftwareService with Loggable { | ||
|
||
/** Delete all unreferenced softwares | ||
* First search in software, and then in nodes, so that if a node arrives in between (new inventory) | ||
* its software wont be deleted | ||
*/ | ||
def deleteUnreferencedSoftware() : Box[Seq[String]] = { | ||
val t1 = System.currentTimeMillis | ||
for { | ||
allSoftwares <- readOnlySoftware.getAllSoftwareIds() | ||
t2 = System.currentTimeMillis() | ||
_ = logger.debug(s"All softwares id in ou=software fetched: ${allSoftwares.size} softwares id in ${t2-t1}ms") | ||
|
||
allNodesSoftwares <- readOnlySoftware.getSoftwaresForAllNodes() | ||
t3 = System.currentTimeMillis() | ||
_ = logger.debug(s"All softwares id in nodes fetched: ${allNodesSoftwares.size} softwares id in ${t3-t2}ms") | ||
|
||
|
||
extraSoftware = allSoftwares -- allNodesSoftwares | ||
_ = logger.debug(s"Found ${extraSoftware.size} unreferenced software in ou=software, going to delete them") | ||
|
||
deletedSoftware <- writeOnlySoftwareDAO.deleteSoftwares(extraSoftware.toSeq) | ||
t4 = System.currentTimeMillis() | ||
_ = logger.debug(s"Deleted ${deletedSoftware.size} software in ${t4-t3}ms") | ||
|
||
|
||
} yield { | ||
deletedSoftware.map(x => x.toString).toSeq | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...er/rudder-core/src/main/scala/com/normation/rudder/batch/PurgeUnreferencedSoftwares.scala
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,80 @@ | ||
/* | ||
************************************************************************************* | ||
* Copyright 2017 Normation SAS | ||
************************************************************************************* | ||
* | ||
* This file is part of Rudder. | ||
* | ||
* Rudder is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* In accordance with the terms of section 7 (7. Additional Terms.) of | ||
* the GNU General Public License version 3, the copyright holders add | ||
* the following Additional permissions: | ||
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source | ||
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General | ||
* Public License version 3, when you create a Related Module, this | ||
* Related Module is not considered as a part of the work and may be | ||
* distributed under the license agreement of your choice. | ||
* A "Related Module" means a set of sources files including their | ||
* documentation that, without modification of the Source Code, enables | ||
* supplementary functions or services in addition to those offered by | ||
* the Software. | ||
* | ||
* Rudder is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Rudder. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
************************************************************************************* | ||
*/ | ||
|
||
package com.normation.rudder.batch | ||
|
||
|
||
import com.normation.rudder.domain.logger.ScheduledJobLogger | ||
import com.normation.inventory.ldap.core.SoftwareService | ||
import monix.execution.Scheduler.{global => scheduler} | ||
import net.liftweb.common._ | ||
|
||
import scala.concurrent.duration._ | ||
|
||
|
||
/** | ||
* A naive scheduler which checks every updateInterval if software needs to be deleted | ||
*/ | ||
class PurgeUnreferencedSoftwares( | ||
softwareService : SoftwareService | ||
, updateInterval : FiniteDuration | ||
) { | ||
|
||
val logger = ScheduledJobLogger | ||
|
||
|
||
if (updateInterval < 1.hour) { | ||
logger.info(s"Disable automatic purge of unreferenced softwares (update interval cannot be less than 1 hour)") | ||
} else { | ||
logger.debug(s"***** starting batch that purge unreferenced softwares, every ${updateInterval.toString()} *****") | ||
scheduler.scheduleWithFixedDelay(updateInterval, updateInterval) { | ||
softwareService.deleteUnreferencedSoftware() match { | ||
case Full(softwares) => | ||
logger.info(s"Purged ${softwares.length} unreferenced softwares") | ||
if (logger.isDebugEnabled && softwares.length > 0) | ||
logger.debug(s"Purged following software: ${softwares.mkString(",")}") | ||
case e: EmptyBox => | ||
val error = (e ?~! s"Error when deleting unreferenced softwares") | ||
logger.error(error.messageChain) | ||
error.rootExceptionCause.foreach(ex => | ||
logger.error("Exception was:", ex) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
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
this returns a seq of LDAPEntries, each containing a Seq of Softwares DN
Is there a way to stream the result, and consume into a set? If not, can we paginate such a search?