public void finishedBatch() throws Exception { long successes = this.successes.get(); long failures = this.failures.get(); // reset the counters this.successes.set(0); this.failures.set(0); // get the latest notification info notification = em.get(this.notification.getUuid(), Notification.class); notification.updateStatistics(successes, failures); notification.setModified(System.currentTimeMillis()); notification.setFinished(notification.getModified()); em.update(notification); } }
user.getDynamicProperties().remove("location"); em.updateProperties(user, properties); em.update(user); app.waitForQueueDrainAndRefreshIndex();
@Override public void setOrganizationProps( UUID orgId, Map<String, Object> props ) throws Exception { EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); Group org = em.get( orgId, Group.class ); if ( org == null ) { throw new EntityNotFoundException( String.format( "Could not find organization with id %s", orgId.toString() ) ); } org.setProperties( props ); em.update(org); }
user.setProperty("expiration", pingUser.get("expiration")); try { entityManager.update(user); } catch (Exception ex) { logger.error("Error in createOrAuthenticate()", ex);
@Override public User deactivateUser( UUID applicationId, UUID userId ) throws Exception { EntityManager em = emf.getEntityManager( applicationId ); User user = em.get( userId, User.class ); if ( user == null ) { throw new ManagementException( String.format( "User with id %s does not exist in app %s", userId, applicationId ) ); } user.setActivated( false ); user.setDeactivated( System.currentTimeMillis() ); em.update( user ); // revoke all access tokens for the app revokeAccessTokensForAppUser(applicationId, userId); return user; }
entityManager.update( fileImport );
@Override protected void doJob(JobExecution jobExecution) throws Exception { logger.info( "execute ImportJob {}", jobExecution.getJobId().toString() ); try { JobData jobData = jobExecution.getJobData(); if (jobData == null) { logger.error("jobData cannot be null"); return; } // heartbeat to indicate job has started jobExecution.heartbeat(); // call the doImport method from import service which // schedules the sub-jobs i.e. parsing of files to FileImport Job importService.doImport(jobExecution); } catch ( Throwable t ) { logger.error("Error calling in importJob", t); // update import job record UUID importId = (UUID) jobExecution.getJobData().getProperty(IMPORT_ID); EntityManager mgmtApp = emf.getEntityManager(emf.getManagementAppId()); Import importEntity = mgmtApp.get(importId, Import.class); importEntity.setState(Import.State.FAILED); importEntity.setErrorMessage(t.getMessage()); mgmtApp.update(importEntity); throw t; } if (logger.isTraceEnabled()) { logger.trace("Import Service completed job"); } }
@Override protected void doJob(JobExecution jobExecution) throws Exception { logger.info("execute FileImportJob {}", jobExecution.toString()); try { JobData jobData = jobExecution.getJobData(); if (jobData == null) { logger.error("jobData cannot be null"); return; } // heartbeat to indicate job has started jobExecution.heartbeat(); // call the File Parser for the file set in job execution importService.downloadAndImportFile(jobExecution); } catch ( Throwable t ) { logger.debug("Error importing file", t); // update file import record UUID fileImportId = (UUID) jobExecution.getJobData().getProperty(FILE_IMPORT_ID); EntityManager em = emf.getEntityManager(emf.getManagementAppId()); FileImport fileImport = em.get(fileImportId, FileImport.class); fileImport.setState( FileImport.State.FAILED ); em.update( fileImport ); throw t; } logger.info("File Import Service completed job: {}", jobExecution.getJobName() ); }
/** * This method is called when the job is retried maximum times by the scheduler but still fails. * Thus the scheduler marks it as DEAD. */ @Override public void dead( final JobExecution execution ) throws Exception { // Get the root entity manager EntityManager rootEm = emf.getEntityManager( emf.getManagementAppId() ); // Mark the sub-job i.e. File Import Job as Failed FileImport fileImport = null;//importService.getFileImportEntity(execution); fileImport.setErrorMessage("The Job has been tried maximum times but still failed"); fileImport.setState(FileImport.State.FAILED); rootEm.update(fileImport); // If one file Job fails, mark the main import Job also as failed Results ImportJobResults = rootEm.getSourceEntities( fileImport, "includes", null, Level.ALL_PROPERTIES); List<Entity> importEntity = ImportJobResults.getEntities(); UUID importId = importEntity.get(0).getUuid(); Import importUG = rootEm.get(importId, Import.class); importUG.setState(Import.State.FAILED); rootEm.update(importUG); } }
/** * This method is called when the job is retried maximum times by the * scheduler but still fails. Thus the scheduler marks it as DEAD. */ @Override public void dead( final JobExecution execution ) throws Exception { // marks the job as failed as it will not be retried by the scheduler. EntityManager rootEm = emf.getEntityManager( emf.getManagementAppId()); Import importUG = importService.getImportEntity(execution); importUG.setErrorMessage("The Job has been tried maximum times but still failed"); importUG.setState(Import.State.FAILED); rootEm.update(importUG); } }
@Override public boolean updateServiceProperties(Map<String, String> properties) { EntityManager em = getEntityManager(getManagementAppId()); Query q = Query.fromQL("select *"); Results results = null; try { results = em.searchCollection( em.getApplicationRef(), "propertymaps", q); } catch (Exception ex) { logger.error("Error getting system properties", ex); return false; } org.apache.usergrid.persistence.Entity propsEntity = null; if ( !results.isEmpty() ) { propsEntity = results.getEntity(); } else { propsEntity = EntityFactory.newEntity( UUIDUtils.newTimeUUID(), "propertymap"); } // intentionally going only one-level deep into fields and treating all // values as strings because that is all we need for service properties for ( String key : properties.keySet() ) { propsEntity.setProperty(key, properties.get(key).toString()); } try { em.update( propsEntity ); } catch (Exception ex) { logger.error("Error updating service properties", ex); return false; } return true; }
@Override public void delayRetry( JobExecution execution, long delay ) { JobData data = execution.getJobData(); JobStat stat = execution.getJobStats(); try { // if it's a dead status, it's failed too many times, just kill the job if ( execution.getStatus() == Status.DEAD ) { getQm().deleteTransaction( jobQueueName, execution.getTransactionId(), null ); getEm().update( data ); getEm().update( stat ); return; } // re-schedule the job to run again in the future scheduleJob( execution.getJobName(), System.currentTimeMillis() + delay, data.getUuid(), stat.getUuid() ); // delete the pending transaction getQm().deleteTransaction( jobQueueName, execution.getTransactionId(), null ); // update the data for the next run getEm().update( data ); getEm().update( stat ); } catch ( Exception e ) { // should never happen throw new JobRuntimeException( String.format( "Unable to delete job data with id %s", data.getUuid() ), e ); } }
@Override public boolean deleteServiceProperty(String name) { EntityManager em = getEntityManager(getManagementAppId()); Query q = Query.fromQL( "select *"); Results results = null; try { results = em.searchCollection( em.getApplicationRef(), "propertymaps", q); } catch (Exception ex) { logger.error("Error getting service property for delete of property: {}", name, ex); return false; } org.apache.usergrid.persistence.Entity propsEntity = null; if ( !results.isEmpty() ) { propsEntity = results.getEntity(); } else { propsEntity = EntityFactory.newEntity( UUIDUtils.newTimeUUID(), "propertymap"); } try { ((AbstractEntity)propsEntity).clearDataset( name ); em.update( propsEntity ); } catch (Exception ex) { logger.error("Error deleting service property orgAppName: {}", name, ex); return false; } return true; }
@Test public void testSuccess() throws Exception { final EntityManagerFactory emf = mock( EntityManagerFactory.class ); final EntityManager em = mock( EntityManager.class ); when( emf.getEntityManager( emf.getManagementAppId() ) ).thenReturn( em ); final UUID importFileId = UUIDGenerator.newTimeUUID(); final FileImport fileImport = new FileImport(); fileImport.setUuid( importFileId ); when( em.get( importFileId, FileImport.class ) ).thenReturn( fileImport ); final FileImportTracker fileImportTracker = new FileImportTracker( emf, fileImport, 1000 ); final long expectedCount = 100; for ( long i = 0; i < expectedCount; i++ ) { fileImportTracker.entityWritten(); } fileImportTracker.complete(); ArgumentCaptor<FileImport> savedFileImport = ArgumentCaptor.forClass( FileImport.class ); verify( em ).update( savedFileImport.capture() ); final FileImport updated = savedFileImport.getValue(); assertSame( "Same instance should be updated", fileImport, updated ); assertEquals( "Same count expected", expectedCount, updated.getImportedEntityCount() ); assertNull( updated.getErrorMessage() ); }
@Test public void explicitFail() throws Exception { final EntityManagerFactory emf = mock( EntityManagerFactory.class ); final EntityManager em = mock( EntityManager.class ); when( emf.getEntityManager( emf.getManagementAppId() ) ).thenReturn( em ); final UUID importFileId = UUIDGenerator.newTimeUUID(); final FileImport fileImport = new FileImport(); fileImport.setUuid( importFileId ); when( em.get( importFileId, FileImport.class ) ).thenReturn( fileImport ); final FileImportTracker fileImportTracker = new FileImportTracker( emf, fileImport, 1000 ); final long expectedCount = 100; for ( long i = 0; i < expectedCount; i++ ) { fileImportTracker.entityWritten(); } fileImportTracker.fatal( "Something bad happened" ); ArgumentCaptor<FileImport> savedFileImport = ArgumentCaptor.forClass( FileImport.class ); verify( em ).update( savedFileImport.capture() ); final FileImport updated = savedFileImport.getValue(); assertSame( "Same instance should be updated", fileImport, updated ); assertEquals( "Same count expected", expectedCount, updated.getImportedEntityCount() ); assertEquals( "Fail count is 0", 0, updated.getFailedEntityCount() ); assertEquals( "Correct expected message", "Something bad happened", updated.getErrorMessage() ); assertEquals( "Expected failed state", FileImport.State.FAILED, updated.getState() ); }
em.update( user );
Entity entity = emApp2.get(uuid); entity.setProperty("fuel_source", "Hydrogen"); emApp2.update(entity);
em.update( entity1Ref );