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.common.ArchivaException;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.ConfigurationEvent;
25 import org.apache.archiva.configuration.ConfigurationListener;
26 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.archiva.metadata.repository.MetadataRepository;
28 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
29 import org.apache.archiva.metadata.repository.RepositorySession;
30 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
31 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
32 import org.apache.archiva.redback.components.scheduler.CronExpressionValidator;
33 import org.apache.archiva.redback.components.scheduler.Scheduler;
34 import org.apache.archiva.redback.components.taskqueue.TaskQueue;
35 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
36 import org.apache.archiva.scheduler.repository.model.RepositoryArchivaTaskScheduler;
37 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
38 import org.apache.commons.lang.time.StopWatch;
39 import org.quartz.SchedulerException;
40 import org.quartz.impl.JobDetailImpl;
41 import org.quartz.impl.triggers.CronTriggerImpl;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.stereotype.Service;
46 import javax.annotation.PostConstruct;
47 import javax.annotation.PreDestroy;
48 import javax.inject.Inject;
49 import javax.inject.Named;
50 import java.text.ParseException;
51 import java.util.ArrayList;
52 import java.util.HashSet;
53 import java.util.List;
57 * Default implementation of a scheduling component for archiva.
59 @Service( "archivaTaskScheduler#repository" )
60 public class DefaultRepositoryArchivaTaskScheduler
61 implements RepositoryArchivaTaskScheduler, ConfigurationListener
63 private Logger log = LoggerFactory.getLogger( getClass() );
69 private Scheduler scheduler;
72 private CronExpressionValidator cronValidator;
78 @Named( value = "taskQueue#repository-scanning" )
79 private TaskQueue repositoryScanningQueue;
85 private ArchivaConfiguration archivaConfiguration;
91 @Named( value = "repositoryStatisticsManager#default" )
92 private RepositoryStatisticsManager repositoryStatisticsManager;
95 * TODO: could have multiple implementations
98 private RepositorySessionFactory repositorySessionFactory;
100 private static final String REPOSITORY_SCAN_GROUP = "rg";
102 private static final String REPOSITORY_JOB = "rj";
104 private static final String REPOSITORY_JOB_TRIGGER = "rjt";
106 static final String TASK_QUEUE = "TASK_QUEUE";
108 static final String TASK_REPOSITORY = "TASK_REPOSITORY";
110 public static final String CRON_HOURLY = "0 0 * * * ?";
112 private Set<String> jobs = new HashSet<String>();
114 private List<String> queuedRepos = new ArrayList<String>();
117 public void startup()
118 throws ArchivaException
121 StopWatch stopWatch = new StopWatch();
124 archivaConfiguration.addListener( this );
126 List<ManagedRepositoryConfiguration> repositories =
127 archivaConfiguration.getConfiguration().getManagedRepositories();
129 RepositorySession repositorySession = repositorySessionFactory.createSession();
132 MetadataRepository metadataRepository = repositorySession.getRepository();
133 for ( ManagedRepositoryConfiguration repoConfig : repositories )
135 if ( repoConfig.isScanned() )
139 scheduleRepositoryJobs( repoConfig );
141 catch ( SchedulerException e )
143 throw new ArchivaException( "Unable to start scheduler: " + e.getMessage(), e );
148 if ( !isPreviouslyScanned( repoConfig, metadataRepository ) )
150 queueInitialRepoScan( repoConfig );
153 catch ( MetadataRepositoryException e )
155 log.warn( "Unable to determine if a repository is already scanned, skipping initial scan: "
156 + e.getMessage(), e );
163 repositorySession.close();
167 log.info( "Time to initalize DefaultRepositoryArchivaTaskScheduler: {} ms", stopWatch.getTime() );
173 throws SchedulerException
175 for ( String job : jobs )
177 scheduler.unscheduleJob( job, REPOSITORY_SCAN_GROUP );
184 @SuppressWarnings( "unchecked" )
185 public boolean isProcessingRepositoryTask( String repositoryId )
187 synchronized ( repositoryScanningQueue )
189 List<RepositoryTask> queue = null;
193 queue = repositoryScanningQueue.getQueueSnapshot();
195 catch ( TaskQueueException e )
197 // not possible with plexus-taskqueue implementation, ignore
200 for ( RepositoryTask queuedTask : queue )
202 if ( queuedTask.getRepositoryId().equals( repositoryId ) )
211 public 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 boolean unQueueTask( RepositoryTask task )
255 throws TaskQueueException
257 synchronized ( repositoryScanningQueue )
259 if ( !isProcessingRepositoryTask( task ) )
261 log.info( "cannot unqueue Repository task '{}' not already queued.", task );
266 return repositoryScanningQueue.remove( task );
271 public void configurationEvent( ConfigurationEvent event )
273 if ( event.getType() == ConfigurationEvent.SAVED )
275 for ( String job : jobs )
279 scheduler.unscheduleJob( job, REPOSITORY_SCAN_GROUP );
281 catch ( SchedulerException e )
283 log.error( "Error restarting the repository scanning job after property change." );
288 List<ManagedRepositoryConfiguration> repositories =
289 archivaConfiguration.getConfiguration().getManagedRepositories();
291 for ( ManagedRepositoryConfiguration repoConfig : repositories )
293 if ( repoConfig.getRefreshCronExpression() != null )
297 scheduleRepositoryJobs( repoConfig );
299 catch ( SchedulerException e )
301 log.error( "error restarting job: " + REPOSITORY_JOB + ":" + repoConfig.getId() );
308 private boolean isPreviouslyScanned( ManagedRepositoryConfiguration repoConfig,
309 MetadataRepository metadataRepository )
310 throws MetadataRepositoryException
312 long start = System.currentTimeMillis();
314 boolean res = repositoryStatisticsManager.hasStatistics( metadataRepository, repoConfig.getId() );
316 long end = System.currentTimeMillis();
318 log.debug( "isPreviouslyScanned repo {} {} time: {} ms", repoConfig.getId(), res, ( end - start ) );
323 // MRM-848: Pre-configured repository initially appear to be empty
324 private synchronized void queueInitialRepoScan( ManagedRepositoryConfiguration repoConfig )
326 String repoId = repoConfig.getId();
327 RepositoryTask task = new RepositoryTask();
328 task.setRepositoryId( repoId );
330 if ( !queuedRepos.contains( repoId ) )
332 log.info( "Repository [" + repoId + "] is queued to be scanned as it hasn't been previously." );
336 queuedRepos.add( repoConfig.getId() );
337 this.queueTask( task );
339 catch ( TaskQueueException e )
341 log.error( "Error occurred while queueing repository [" + repoId + "] task : " + e.getMessage() );
346 private synchronized void scheduleRepositoryJobs( ManagedRepositoryConfiguration repoConfig )
347 throws SchedulerException
349 if ( repoConfig.getRefreshCronExpression() == null )
351 log.warn( "Skipping job, no cron expression for {}", repoConfig.getId() );
355 if ( !repoConfig.isScanned() )
357 log.warn( "Skipping job, repository scannable has been disabled for {}", repoConfig.getId() );
361 // get the cron string for these database scanning jobs
362 String cronString = repoConfig.getRefreshCronExpression();
364 if ( !cronValidator.validate( cronString ) )
366 log.warn( "Cron expression [{}] for repository [{}] is invalid. Defaulting to hourly.", cronString,
367 repoConfig.getId() );
368 cronString = CRON_HOURLY;
371 // setup the unprocessed artifact job
372 JobDetailImpl repositoryJob =
373 new JobDetailImpl( REPOSITORY_JOB + ":" + repoConfig.getId(), REPOSITORY_SCAN_GROUP,
374 RepositoryTaskJob.class );
376 repositoryJob.getJobDataMap().put( TASK_QUEUE, repositoryScanningQueue );
377 repositoryJob.getJobDataMap().put( TASK_REPOSITORY, repoConfig.getId() );
381 CronTriggerImpl trigger =
382 new CronTriggerImpl( REPOSITORY_JOB_TRIGGER + ":" + repoConfig.getId(), REPOSITORY_SCAN_GROUP,
385 jobs.add( REPOSITORY_JOB + ":" + repoConfig.getId() );
386 scheduler.scheduleJob( repositoryJob, trigger );
388 catch ( ParseException e )
390 log.error( "ParseException in repository scanning cron expression, disabling repository scanning for '"
391 + repoConfig.getId() + "': " + e.getMessage() );