执行以groovy编写的管理脚本时遇到问题。最初的目标是从所有存储库中删除所有zip文件。我的方法是遍历所有存储库,在单个事务中查找包含“ zip”扩展名的组件,而在下一个事务中删除它们。
我写了以下脚本来实现这种方法
import org.sonatype.nexus.repository.Repository
import org.sonatype.nexus.repository.storage.Component
import org.sonatype.nexus.repository.storage.Query
import org.sonatype.nexus.repository.storage.StorageFacet
import org.sonatype.nexus.repository.storage.StorageTx
// get repos
final Iterable<Repository> repositories = repository.repositoryManager.browse()
// iterate
repositories.each {
Repository repo ->
if ("hosted".equals(repo.type.toString()) &&
"maven2".equals(repo.format.toString())) {
log.info("Found hosted repository " + repo.name)
managePossibleZips(repo)
}
}
/**
* Manage zip files for given repository
* @param repo
* @return
*/
def managePossibleZips(Repository repo) {
// get storage facet
final StorageFacet storageFacet = repo.facet(StorageFacet)
// find zip files
final Iterable<Component> components = findZips(storageFacet, repo)
// erase zip files
if (0 == components.size) {
log.info("No zip files in this repository")
} else {
eraseZips(storageFacet, components)
}
}
/**
* Find components, that contain extension 'zip', based on supplied repository and storageFacet
*
* @param storageFacet the storage facet
* @param repo the supplied repository
* @return Iterable < Component > List of components containing zip files
*/
def findZips(StorageFacet storageFacet, Repository repo) {
// get transaction
final StorageTx transaction = storageFacet.txSupplier().get()
// tx
transaction.begin()
// find components containing zip files
Iterable<Component> components = transaction.findComponents(
Query.builder().where('extension =').param('zip').build(), [repo]
)
// commit
transaction.commit()
// close
transaction.close()
// return components
return components
}
/**
* Erase supplied components
*
* @param storageFacet the storage facet
* @param components list of components to be erased
*/
def eraseZips(StorageFacet storageFacet, Iterable<Component> components) {
components.each { Component component ->
// get transaction
final StorageTx transaction = storageFacet.txSupplier().get()
// tx
transaction.begin()
// find components containing zip files
transaction.deleteComponent(component);
// commit
transaction.commit()
// close
transaction.close()
// return components
}
}
log.info("All done, bye bye")
在尝试执行脚本时,系统日志中会弹出以下错误
2018-10-09 10:53:07,809+0200 INFO [qtp402299672-4809] admin org.sonatype.nexus.quartz.internal.task.QuartzTaskInfo - Task 'nx32' [script] runNow
2018-10-09 10:53:07,809+0200 INFO [qtp402299672-4809] admin org.sonatype.nexus.quartz.internal.task.QuartzTaskInfo - Task 'nx32' [script] state change WAITING -> RUNNING
2018-10-09 10:53:07,821+0200 INFO [quartz-5-thread-18] *SYSTEM org.sonatype.nexus.internal.script.ScriptTask - Task log: /opt/sonatype-work/nexus3/log/tasks/script-20181009105307817.log
2018-10-09 10:53:08,174+0200 INFO [quartz-5-thread-18] *SYSTEM org.sonatype.nexus.internal.script.ScriptTask - Found hosted repository ps-myrepo-mvn
2018-10-09 10:53:08,175+0200 WARN [quartz-5-thread-18] *SYSTEM org.sonatype.nexus.quartz.internal.task.QuartzTaskJob - Task d446ea78-eeb0-4b17-be55-3e611e696762 : 'nx32' [script] execution failure
javax.script.ScriptException: javax.script.ScriptException: java.lang.IllegalArgumentException: Nested DB TX!
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158)
at org.sonatype.nexus.internal.script.ScriptServiceImpl.eval(ScriptServiceImpl.java:153)
at org.sonatype.nexus.internal.script.ScriptServiceImpl.eval(ScriptServiceImpl.java:162)
at org.sonatype.nexus.internal.script.ScriptTask.execute(ScriptTask.java:78)
at org.sonatype.nexus.scheduling.TaskSupport.call(TaskSupport.java:93)
at org.sonatype.nexus.quartz.internal.task.QuartzTaskJob.doExecute(QuartzTaskJob.java:145)
at org.sonatype.nexus.quartz.internal.task.QuartzTaskJob.execute(QuartzTaskJob.java:108)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.sonatype.nexus.thread.internal.MDCAwareRunnable.run(MDCAwareRunnable.java:40)
at org.apache.shiro.subject.support.SubjectRunnable.doRun(SubjectRunnable.java:120)
at org.apache.shiro.subject.support.SubjectRunnable.run(SubjectRunnable.java:108)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: javax.script.ScriptException: java.lang.IllegalArgumentException: Nested DB TX!
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)
... 15 common frames omitted
Caused by: java.lang.IllegalArgumentException: Nested DB TX!
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:135)
at org.sonatype.nexus.repository.storage.StorageTxImpl.<init>(StorageTxImpl.java:154)
at org.sonatype.nexus.repository.storage.StorageFacetImpl.openStorageTx(StorageFacetImpl.java:263)
at org.sonatype.nexus.repository.storage.StorageFacetImpl.lambda$0(StorageFacetImpl.java:145)
at com.google.common.base.Supplier$get.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at Script25.findZips(Script25.groovy:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:151)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl$2.invokeMethod(GroovyScriptEngineImpl.java:301)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
at Script25.managePossibleZips(Script25.groovy:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
at Script25$_run_closure1.doCall(Script25.groovy:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2040)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2025)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2066)
at org.codehaus.groovy.runtime.dgm$162.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at Script25.run(Script25.groovy:11)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
... 16 common frames omitted
答案 0 :(得分:1)
尝试将交易包装在try-catch块中,并始终关闭交易,如下所示:
try {
tx.begin()
components = tx.countComponents(Query.builder().where('1').eq(1).build(), [repo])
tx.commit()
} catch (Exception e) {
log.warn("Transaction failed {}", e.toString())
tx.rollback()
} finally {
tx.close()
}
您还可能需要重新启动Nexus,以摆脱锁定的事务。否则,每个带有事务的脚本现在都会产生Nested DB TX异常
编辑:我的代码中的countComponents()函数只是一个示例