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.CronScheduleBuilder;
40 import org.quartz.CronTrigger;
41 import org.quartz.JobBuilder;
42 import org.quartz.JobDataMap;
43 import org.quartz.JobDetail;
44 import org.quartz.SchedulerException;
45 import org.quartz.TriggerBuilder;
46 import org.quartz.impl.JobDetailImpl;
47 import org.quartz.impl.triggers.CronTriggerImpl;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.stereotype.Service;
52 import javax.annotation.PostConstruct;
53 import javax.annotation.PreDestroy;
54 import javax.inject.Inject;
55 import javax.inject.Named;
56 import java.text.ParseException;
57 import java.util.ArrayList;
58 import java.util.HashSet;
59 import java.util.List;
63 * Default implementation of a scheduling component for archiva.
65 @Service( "archivaTaskScheduler#repository" )
66 public class DefaultRepositoryArchivaTaskScheduler
67 implements RepositoryArchivaTaskScheduler, ConfigurationListener
69 private Logger log = LoggerFactory.getLogger( getClass() );
72 private Scheduler scheduler;
75 private CronExpressionValidator cronValidator;
78 @Named( value = "taskQueue#repository-scanning" )
79 private TaskQueue repositoryScanningQueue;
82 private ArchivaConfiguration archivaConfiguration;
85 @Named( value = "repositoryStatisticsManager#default" )
86 private RepositoryStatisticsManager repositoryStatisticsManager;
89 * TODO: could have multiple implementations
92 private RepositorySessionFactory repositorySessionFactory;
94 private static final String REPOSITORY_SCAN_GROUP = "rg";
96 private static final String REPOSITORY_JOB = "rj";
98 private static final String REPOSITORY_JOB_TRIGGER = "rjt";
100 static final String TASK_QUEUE = "TASK_QUEUE";
102 static final String TASK_REPOSITORY = "TASK_REPOSITORY";
104 public static final String CRON_HOURLY = "0 0 * * * ?";
106 private Set<String> jobs = new HashSet<String>();
108 private List<String> queuedRepos = new ArrayList<String>();
111 public void startup()
112 throws ArchivaException
115 StopWatch stopWatch = new StopWatch();
118 archivaConfiguration.addListener( this );
120 List<ManagedRepositoryConfiguration> repositories =
121 archivaConfiguration.getConfiguration().getManagedRepositories();
123 RepositorySession repositorySession = repositorySessionFactory.createSession();
126 MetadataRepository metadataRepository = repositorySession.getRepository();
127 for ( ManagedRepositoryConfiguration repoConfig : repositories )
129 if ( repoConfig.isScanned() )
133 scheduleRepositoryJobs( repoConfig );
135 catch ( SchedulerException e )
137 throw new ArchivaException( "Unable to start scheduler: " + e.getMessage(), e );
142 if ( !isPreviouslyScanned( repoConfig, metadataRepository ) )
144 queueInitialRepoScan( repoConfig );
147 catch ( MetadataRepositoryException e )
149 log.warn( "Unable to determine if a repository is already scanned, skipping initial scan: {}",
157 repositorySession.close();
161 log.info( "Time to initalize DefaultRepositoryArchivaTaskScheduler: {} ms", stopWatch.getTime() );
167 throws SchedulerException
169 for ( String job : jobs )
171 scheduler.unscheduleJob( job, REPOSITORY_SCAN_GROUP );
178 @SuppressWarnings( "unchecked" )
179 public boolean isProcessingRepositoryTask( String repositoryId )
181 synchronized ( repositoryScanningQueue )
183 List<RepositoryTask> queue = null;
187 queue = repositoryScanningQueue.getQueueSnapshot();
189 catch ( TaskQueueException e )
191 // not possible with plexus-taskqueue implementation, ignore
194 for ( RepositoryTask queuedTask : queue )
196 if ( queuedTask.getRepositoryId().equals( repositoryId ) )
205 public boolean isProcessingRepositoryTask( RepositoryTask task )
207 synchronized ( repositoryScanningQueue )
209 List<RepositoryTask> queue = null;
213 queue = repositoryScanningQueue.getQueueSnapshot();
215 catch ( TaskQueueException e )
217 // not possible with plexus-taskqueue implementation, ignore
220 for ( RepositoryTask queuedTask : queue )
222 if ( task.equals( queuedTask ) )
231 public void queueTask( RepositoryTask task )
232 throws TaskQueueException
234 synchronized ( repositoryScanningQueue )
236 if ( isProcessingRepositoryTask( task ) )
238 log.debug( "Repository task '{}' is already queued. Skipping task.", task );
242 // add check if the task is already queued if it is a file scan
243 repositoryScanningQueue.put( task );
248 public boolean unQueueTask( RepositoryTask task )
249 throws TaskQueueException
251 synchronized ( repositoryScanningQueue )
253 if ( !isProcessingRepositoryTask( task ) )
255 log.info( "cannot unqueue Repository task '{}' not already queued.", task );
260 return repositoryScanningQueue.remove( task );
265 public void configurationEvent( ConfigurationEvent event )
267 if ( event.getType() == ConfigurationEvent.SAVED )
269 for ( String job : jobs )
273 scheduler.unscheduleJob( job, REPOSITORY_SCAN_GROUP );
275 catch ( SchedulerException e )
277 log.error( "Error restarting the repository scanning job after property change." );
282 List<ManagedRepositoryConfiguration> repositories =
283 archivaConfiguration.getConfiguration().getManagedRepositories();
285 for ( ManagedRepositoryConfiguration repoConfig : repositories )
287 if ( repoConfig.getRefreshCronExpression() != null )
291 scheduleRepositoryJobs( repoConfig );
293 catch ( SchedulerException e )
295 log.error( "error restarting job: '{}' : '{}'", REPOSITORY_JOB, repoConfig.getId() );
302 private boolean isPreviouslyScanned( ManagedRepositoryConfiguration repoConfig,
303 MetadataRepository metadataRepository )
304 throws MetadataRepositoryException
306 long start = System.currentTimeMillis();
308 boolean res = repositoryStatisticsManager.hasStatistics( metadataRepository, repoConfig.getId() );
310 long end = System.currentTimeMillis();
312 log.debug( "isPreviouslyScanned repo {} {} time: {} ms", repoConfig.getId(), res, ( end - start ) );
317 // MRM-848: Pre-configured repository initially appear to be empty
318 private synchronized void queueInitialRepoScan( ManagedRepositoryConfiguration repoConfig )
320 String repoId = repoConfig.getId();
321 RepositoryTask task = new RepositoryTask();
322 task.setRepositoryId( repoId );
324 if ( !queuedRepos.contains( repoId ) )
326 log.info( "Repository [{}] is queued to be scanned as it hasn't been previously.", repoId );
330 queuedRepos.add( repoConfig.getId() );
331 this.queueTask( task );
333 catch ( TaskQueueException e )
335 log.error( "Error occurred while queueing repository [{}] task : {}", e.getMessage(), repoId );
340 private synchronized void scheduleRepositoryJobs( ManagedRepositoryConfiguration repoConfig )
341 throws SchedulerException
343 if ( repoConfig.getRefreshCronExpression() == null )
345 log.warn( "Skipping job, no cron expression for {}", repoConfig.getId() );
349 if ( !repoConfig.isScanned() )
351 log.warn( "Skipping job, repository scannable has been disabled for {}", repoConfig.getId() );
355 // get the cron string for these database scanning jobs
356 String cronString = repoConfig.getRefreshCronExpression();
358 if ( !cronValidator.validate( cronString ) )
360 log.warn( "Cron expression [{}] for repository [{}] is invalid. Defaulting to hourly.", cronString,
361 repoConfig.getId() );
362 cronString = CRON_HOURLY;
365 JobDataMap jobDataMap = new JobDataMap( );
366 jobDataMap.put( TASK_QUEUE, repositoryScanningQueue );
367 jobDataMap.put( TASK_REPOSITORY, repoConfig.getId() );
369 // setup the unprocessed artifact job
370 JobDetail repositoryJob = JobBuilder.newJob( RepositoryTaskJob.class )
371 .withIdentity( REPOSITORY_JOB + ":" + repoConfig.getId(), REPOSITORY_SCAN_GROUP )
372 .setJobData( jobDataMap )
377 CronTrigger trigger = TriggerBuilder.newTrigger()
378 .withIdentity( REPOSITORY_JOB_TRIGGER + ":" + repoConfig.getId(), REPOSITORY_SCAN_GROUP )
379 .withSchedule( CronScheduleBuilder.cronSchedule( cronString ) )
382 jobs.add( REPOSITORY_JOB + ":" + repoConfig.getId() );
383 scheduler.scheduleJob( repositoryJob, trigger );
385 catch ( RuntimeException e )
388 "ParseException in repository scanning cron expression, disabling repository scanning for '': {}",
389 repoConfig.getId(), e.getMessage() );