Skip to content

Commit

Permalink
SAK-45692 Ensure fileconversion jobs are only queued once (sakaiproje…
Browse files Browse the repository at this point in the history
…ct#9408)

* SAK-45692 Ensure fileconversion jobs are only queued once

https://jira.sakaiproject.org/browse/SAK-45692

* Made REFERENCE a unique key
  • Loading branch information
adrianfish authored Jun 24, 2021
1 parent 2df610a commit 7c21ff3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class FileConversionQueueItem implements PersistableEntity<Long> {
@EqualsAndHashCode.Include
private Long id;

@Column(name = "REFERENCE", length = 255, nullable = false)
@Column(name = "REFERENCE", length = 255, nullable = false, unique = true)
private String reference;

@Enumerated(EnumType.STRING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
public interface FileConversionServiceRepository extends SpringCrudRepository<FileConversionQueueItem, Long> {

List<FileConversionQueueItem> findByStatus(FileConversionQueueItem.Status status);
List<FileConversionQueueItem> findByReference(String reference);
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ public void submit(String ref) {

log.debug("submit({})", ref);

if (repository.findByReference(ref).size() > 0) {
log.debug("{} has already been queued for conversion.", ref);
return;
}

FileConversionQueueItem newItem = new FileConversionQueueItem();
newItem.setReference(ref);
newItem.setStatus(FileConversionQueueItem.Status.NOT_STARTED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ public class FileConversionServiceRepositoryImpl extends SpringCrudRepositoryImp
public List<FileConversionQueueItem> findByStatus(FileConversionQueueItem.Status status) {

Session session = sessionFactory.getCurrentSession();
List<FileConversionQueueItem> items
= session.createCriteria(FileConversionQueueItem.class)
return session.createCriteria(FileConversionQueueItem.class)
.add(Restrictions.eq("status", status)).list();
return items;
}

@Transactional
public List<FileConversionQueueItem> findByReference(String reference) {

Session session = sessionFactory.getCurrentSession();
return session.createCriteria(FileConversionQueueItem.class)
.add(Restrictions.eq("reference", reference)).list();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.runner.RunWith;
import org.sakaiproject.content.api.ContentHostingService;
import org.sakaiproject.content.api.FileConversionService;
import org.sakaiproject.content.api.persistence.FileConversionServiceRepository;
import org.sakaiproject.content.impl.test.FileConversionServiceTestConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -41,6 +42,9 @@ public class FileConversionServiceTest extends AbstractTransactionalJUnit4Spring
@Autowired
private FileConversionService fileConversionService;

@Autowired
private FileConversionServiceRepository repository;

@Test
public void testCanConvert() {

Expand All @@ -51,4 +55,17 @@ public void testCanConvert() {
Assert.isTrue(fileConversionService.canConvert(ContentHostingService.PPT_MIMETYPE), "PPT is one of the default convertable types");
Assert.isTrue(fileConversionService.canConvert(ContentHostingService.PPTX_MIMETYPE), "PPTX is one of the default convertable types");
}

@Test
public void noMultipleSubmits() {

String ref = "xyz";
Assert.isTrue(repository.findByReference(ref).size() == 0);
fileConversionService.submit(ref);
Assert.isTrue(repository.findByReference(ref).size() == 1);
fileConversionService.submit(ref);
Assert.isTrue(repository.findByReference(ref).size() == 1);
fileConversionService.submit(ref);
Assert.isTrue(repository.findByReference(ref).size() == 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public FileConversionServiceRepository fileConversionServiceRepository(SessionFa

FileConversionServiceRepositoryImpl repository = new FileConversionServiceRepositoryImpl();
repository.setSessionFactory(sessionFactory);

return repository;
}

Expand Down

0 comments on commit 7c21ff3

Please sign in to comment.