1 package org.apache.archiva.scheduler.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.metadata.repository.MetadataRepository;
23 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
26 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
27 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
28 import org.apache.maven.archiva.common.ArchivaException;
29 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
30 import org.apache.maven.archiva.configuration.ConfigurationEvent;
31 import org.apache.maven.archiva.configuration.ConfigurationListener;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.codehaus.plexus.taskqueue.TaskQueue;
34 import org.codehaus.plexus.taskqueue.TaskQueueException;
35 import org.codehaus.redback.components.scheduler.CronExpressionValidator;
36 import org.codehaus.redback.components.scheduler.Scheduler;
37 import org.quartz.CronTrigger;
38 import org.quartz.JobDataMap;
39 import org.quartz.JobDetail;
40 import org.quartz.SchedulerException;
41 import org.quartz.impl.JobDetailImpl;
42 import org.quartz.impl.triggers.CronTriggerImpl;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.stereotype.Service;
47 import javax.annotation.PostConstruct;
48 import javax.annotation.PreDestroy;
49 import javax.inject.Inject;
50 import javax.inject.Named;
51 import java.text.ParseException;
52 import java.util.ArrayList;
53 import java.util.HashSet;
54 import java.util.List;
58 * Default implementation of a scheduling component for archiva.
60 * plexus.component role="org.apache.archiva.scheduler.ArchivaTaskScheduler" role-hint="repository"
62 @Service( "archivaTaskScheduler#repository" )
63 public class RepositoryArchivaTaskScheduler
64 implements ArchivaTaskScheduler<RepositoryTask>, ConfigurationListener
66 private Logger log = LoggerFactory.getLogger( RepositoryArchivaTaskScheduler.class );
72 private Scheduler scheduler;
75 CronExpressionValidator cronValidator;
78 * plexus.requirement role-hint="repository-scanning"
81 @Named( value = "taskQueue#repository-scanning" )
82 private TaskQueue repositoryScanningQueue;
88 private ArchivaConfiguration archivaConfiguration;
94 @Named( value = "repositoryStatisticsManager#default" )
95 private RepositoryStatisticsManager repositoryStatisticsManager;
98 * TODO: could have multiple implementations
103 private RepositorySessionFactory repositorySessionFactory;
105 private static final String REPOSITORY_SCAN_GROUP = "rg";
107 private static final String REPOSITORY_JOB = "rj";
109 private static final String REPOSITORY_JOB_TRIGGER = "rjt";
111 static final String TASK_QUEUE = "TASK_QUEUE";
113 static final String TASK_REPOSITORY = "TASK_REPOSITORY";
115 public static final String CRON_HOURLY = "0 0 * * * ?";
117 private Set<String> jobs = new HashSet<String>();
119 private List<String> queuedRepos = new ArrayList<String>();
122 public void startup()
123 throws ArchivaException
125 archivaConfiguration.addListener( this );
127 List<ManagedRepositoryConfiguration> repositories =
128 archivaConfiguration.getConfiguration().getManagedRepositories();
130 RepositorySession repositorySession = repositorySessionFactory.createSession();
133 MetadataRepository metadataRepository = repositorySession.getRepository();
134 for ( ManagedRepositoryConfiguration repoConfig : repositories )
136 if ( repoConfig.isScanned() )
140 scheduleRepositoryJobs( repoConfig );
142 catch ( SchedulerException e )
144 throw new ArchivaException( "Unable to start scheduler: " + e.getMessage(), e );
149 if ( !isPreviouslyScanned( repoConfig, metadataRepository ) )
151 queueInitialRepoScan( repoConfig );
154 catch ( MetadataRepositoryException e )
156 log.warn( "Unable to determine if a repository is already scanned, skipping initial scan: "
157 + e.getMessage(), e );
164 repositorySession.close();
172 throws SchedulerException
174 for ( String job : jobs )
176 scheduler.unscheduleJob( job, REPOSITORY_SCAN_GROUP );
183 @SuppressWarnings( "unchecked" )
184 public boolean isProcessingRepositoryTask( String repositoryId )
186 synchronized ( repositoryScanningQueue )
188 List<RepositoryTask> queue = null;
192 queue = repositoryScanningQueue.getQueueSnapshot();
194 catch ( TaskQueueException e )
196 // not possible with plexus-taskqueue implementation, ignore
199 for ( RepositoryTask queuedTask : queue )
201 if ( queuedTask.getRepositoryId().equals( repositoryId ) )
210 @SuppressWarnings( "unchecked" )
211 private boolean isProcessingRepositoryTask( RepositoryTask task )
213 synchronized ( repositoryScanningQueue )
215 List<RepositoryTask> queue = null;
219 queue = repositoryScanningQueue.getQueueSnapshot();
221 catch ( TaskQueueException e )
223 // not possible with plexus-taskqueue implementation, ignore
226 for ( RepositoryTask queuedTask : queue )
228 if ( task.equals( queuedTask ) )
237 public void queueTask( RepositoryTask task )
238 throws TaskQueueException
240 synchronized ( repositoryScanningQueue )
242 if ( isProcessingRepositoryTask( task ) )
244 log.debug( "Repository task '{}' is already queued. Skipping task.", task );
248 // add check if the task is already queued if it is a file scan
249 repositoryScanningQueue.put( task );
254 public void configurationEvent( ConfigurationEvent event )
256 if ( event.getType() == ConfigurationEvent.SAVED )
258 for ( String job : jobs )
262 scheduler.unscheduleJob( job, REPOSITORY_SCAN_GROUP );
264 catch ( SchedulerException e )
266 log.error( "Error restarting the repository scanning job after property change." );
271 List<ManagedRepositoryConfiguration> repositories =
272 archivaConfiguration.getConfiguration().getManagedRepositories();
274 for ( ManagedRepositoryConfiguration repoConfig : repositories )
276 if ( repoConfig.getRefreshCronExpression() != null )
280 scheduleRepositoryJobs( repoConfig );
282 catch ( SchedulerException e )
284 log.error( "error restarting job: " + REPOSITORY_JOB + ":" + repoConfig.getId() );
291 @SuppressWarnings( "unchecked" )
292 private boolean isPreviouslyScanned( ManagedRepositoryConfiguration repoConfig,
293 MetadataRepository metadataRepository )
294 throws MetadataRepositoryException
296 return repositoryStatisticsManager.getLastStatistics( metadataRepository, repoConfig.getId() ) != null;
299 // MRM-848: Pre-configured repository initially appear to be empty
300 private synchronized void queueInitialRepoScan( ManagedRepositoryConfiguration repoConfig )
302 String repoId = repoConfig.getId();
303 RepositoryTask task = new RepositoryTask();
304 task.setRepositoryId( repoId );
306 if ( !queuedRepos.contains( repoId ) )
308 log.info( "Repository [" + repoId + "] is queued to be scanned as it hasn't been previously." );
312 queuedRepos.add( repoConfig.getId() );
313 this.queueTask( task );
315 catch ( TaskQueueException e )
317 log.error( "Error occurred while queueing repository [" + repoId + "] task : " + e.getMessage() );
322 private synchronized void scheduleRepositoryJobs( ManagedRepositoryConfiguration repoConfig )
323 throws SchedulerException
325 if ( repoConfig.getRefreshCronExpression() == null )
327 log.warn( "Skipping job, no cron expression for " + repoConfig.getId() );
331 if ( !repoConfig.isScanned() )
333 log.warn( "Skipping job, repository scannable has been disabled for " + repoConfig.getId() );
337 // get the cron string for these database scanning jobs
338 String cronString = repoConfig.getRefreshCronExpression();
340 if ( !cronValidator.validate( cronString ) )
342 log.warn( "Cron expression [" + cronString + "] for repository [" + repoConfig.getId()
343 + "] is invalid. Defaulting to hourly." );
344 cronString = CRON_HOURLY;
347 // setup the unprocessed artifact job
348 JobDetailImpl repositoryJob =
349 new JobDetailImpl( REPOSITORY_JOB + ":" + repoConfig.getId(), REPOSITORY_SCAN_GROUP, RepositoryTaskJob.class );
351 repositoryJob.getJobDataMap().put( TASK_QUEUE, repositoryScanningQueue );
352 repositoryJob.getJobDataMap().put( TASK_REPOSITORY, repoConfig.getId() );
356 CronTriggerImpl trigger =
357 new CronTriggerImpl( REPOSITORY_JOB_TRIGGER + ":" + repoConfig.getId(), REPOSITORY_SCAN_GROUP, cronString );
359 jobs.add( REPOSITORY_JOB + ":" + repoConfig.getId() );
360 scheduler.scheduleJob( repositoryJob, trigger );
362 catch ( ParseException e )
364 log.error( "ParseException in repository scanning cron expression, disabling repository scanning for '"
365 + repoConfig.getId() + "': " + e.getMessage() );