/** * doImport called when "eventSubmit_doBatch_Archive" is in the request parameters to archive a * bunch of sites that match the criteria NOTE. This performs exactly as per a normal archive. Ie * if you archive a site twice, you get two copies of the resources. A change needs to be made to * the archive service to clean out the archives before each run. */ public void doBatch_Archive(RunData data, Context context) { SessionState state = ((JetspeedRunData) data).getPortletSessionState(((JetspeedRunData) data).getJs_peid()); if (!securityService.isSuperUser()) { addAlert(state, rb.getString("archive.batch.auth")); return; } final String selectedTerm = (String) state.getAttribute("selectedTerm"); final List<SparseSite> sites = (List<SparseSite>) state.getAttribute("sites"); final Session currentSession = sessionManager.getCurrentSession(); // need to pass this into the new thread final User currentUser = userDirectoryService.getCurrentUser(); // need to pass this into the new thread // do the archive in a new thread Runnable backgroundRunner = new Runnable() { public void run() { try { archiveSites(sites, selectedTerm, currentSession, currentUser); } catch (IllegalStateException e) { throw e; } catch (Exception e) { log.error("Batch Archive background runner thread died: " + e, e); } } }; Thread backgroundThread = new Thread(backgroundRunner); backgroundThread.setDaemon(true); backgroundThread.start(); state.setAttribute(STATE_MODE, BATCH_MODE); }
/** * Process that archives the sites * * @param sites list of SparseSite * @throws InterruptedException */ private void archiveSites( List<SparseSite> sites, String selectedTerm, Session currentSession, User currentUser) throws InterruptedException { if (isLocked()) { throw new IllegalStateException( "Cannot run batch archive, an archive job is already in progress"); } batchArchiveStarted = System.currentTimeMillis(); batchArchiveMessage = rb.getFormattedMessage( "archive.batch.term.text.statusmessage.start", new Object[] {sites.size(), selectedTerm, 0}); batchArchiveStatus = "RUNNING"; log.info( "Batch archive started for term: " + selectedTerm + ". Archiving " + sites.size() + " sites."); Session threadSession = sessionManager.getCurrentSession(); if (threadSession == null) { threadSession = sessionManager.startSession(); } threadSession.setUserId(currentUser.getId()); threadSession.setActive(); sessionManager.setCurrentSession(threadSession); authzGroupService.refreshUser(currentUser.getId()); // counters so we can run this in batches if we have a number of sites to process int archiveCount = 0; try { for (SparseSite s : sites) { log.info("Processing site: " + s.getTitle()); // archive the site archiveService.archive(s.getId()); // compress it // TODO check return value? do we care? try { archiveService.archiveAndZip(s.getId()); } catch (IOException e) { e.printStackTrace(); } archiveCount++; // update message if (archiveCount % 1 == 0) { int percentComplete = (int) (archiveCount * 100) / sites.size(); batchArchiveMessage = rb.getFormattedMessage( "archive.batch.term.text.statusmessage.update", new Object[] {sites.size(), selectedTerm, archiveCount, percentComplete}); } // sleep if we need to and keep sessions alive if (archiveCount > 0 && archiveCount % NUM_SITES_PER_BATCH == 0) { log.info("Sleeping for " + PAUSE_TIME_MS + "ms"); Thread.sleep(PAUSE_TIME_MS); threadSession.setActive(); currentSession.setActive(); } // check timeout if (!isLocked()) { throw new RuntimeException("Timeout occurred while running batch archive"); } } // complete batchArchiveMessage = rb.getFormattedMessage( "archive.batch.term.text.statusmessage.complete", new Object[] {sites.size(), selectedTerm}); log.info("Batch archive complete."); } finally { // reset batchArchiveStatus = STATUS_COMPLETE; batchArchiveStarted = 0; threadSession.clear(); threadSession.invalidate(); } }