use of org.atlasapi.messaging.ResourceUpdatedMessage in project atlas-deer by atlasapi.
the class AbstractContentStore method sendResourceUpdatedMessages.
private void sendResourceUpdatedMessages(Iterable<ResourceUpdatedMessage> messages) {
Map<Id, Id> resourceGraphIds = getResourceGraphIds(messages);
for (ResourceUpdatedMessage message : messages) {
try {
// Downstream workers are processing the entire equivalence graph for every
// updated content. Therefore we want to use the graph ID as the partition key
// to ensure each graph is only processed by a single worker thread.
// We default to using the updated resource id as the partition key if we can't
// resolve the resource's graph.
Id resourceId = message.getUpdatedResource().getId();
Id partitionId = resourceGraphIds.containsKey(resourceId) ? resourceGraphIds.get(resourceId) : resourceId;
sender.sendMessage(message, Longs.toByteArray(partitionId.longValue()));
} catch (Exception e) {
log.error("Failed to send message " + message.getUpdatedResource().toString(), e);
}
}
}
use of org.atlasapi.messaging.ResourceUpdatedMessage in project atlas-deer by atlasapi.
the class AbstractContentStore method createEntityUpdatedMessages.
private <C extends Content> Iterable<ResourceUpdatedMessage> createEntityUpdatedMessages(WriteResult<C, Content> result) {
C writtenResource = result.getResource();
ImmutableList.Builder<ResourceUpdatedMessage> messages = ImmutableList.builder();
messages.add(new ResourceUpdatedMessage(UUID.randomUUID().toString(), Timestamp.of(result.getWriteTime()), writtenResource.toRef()));
if (writtenResource instanceof Container && containerSummaryChanged((Container) writtenResource, result.getPrevious())) {
for (ItemRef itemRef : ((Container) writtenResource).getItemRefs()) {
messages.add(new ResourceUpdatedMessage(UUID.randomUUID().toString(), Timestamp.of(DateTime.now(DateTimeZone.UTC)), itemRef));
}
}
if (writtenResource instanceof Series) {
Series series = (Series) writtenResource;
BrandRef brandRef = series.getBrandRef();
if (brandRef != null) {
messages.add(new ResourceUpdatedMessage(UUID.randomUUID().toString(), Timestamp.of(result.getWriteTime()), brandRef));
}
}
if (writtenResource instanceof Item && ((Item) writtenResource).getContainerRef() != null) {
messages.add(new ResourceUpdatedMessage(UUID.randomUUID().toString(), Timestamp.of(result.getWriteTime()), ((Item) writtenResource).getContainerRef()));
}
if (writtenResource instanceof Episode && ((Episode) writtenResource).getSeriesRef() != null) {
messages.add(new ResourceUpdatedMessage(UUID.randomUUID().toString(), Timestamp.of(result.getWriteTime()), ((Episode) writtenResource).getSeriesRef()));
}
return messages.build();
}
use of org.atlasapi.messaging.ResourceUpdatedMessage in project atlas-deer by atlasapi.
the class CassandraContentStoreIT method testWritingItemWritesRefIntoParent.
@Test
public void testWritingItemWritesRefIntoParent() throws Exception {
when(clock.now()).thenReturn(new DateTime(DateTimeZones.UTC));
when(idGenerator.generateRaw()).thenReturn(1234L).thenReturn(1235L);
Brand brand = create(new Brand());
store.writeContent(brand);
Brand resolvedBrand = (Brand) resolve(1234L);
assertThat(resolvedBrand.getItemRefs(), is(empty()));
Item item = create(new Item());
item.setContainer(resolvedBrand);
store.writeContent(item);
Item resolvedItem = (Item) resolve(1235L);
assertThat(resolvedItem.getContainerRef().getId().longValue(), is(1234L));
ArgumentCaptor<ResourceUpdatedMessage> captor = ArgumentCaptor.forClass(ResourceUpdatedMessage.class);
verify(sender, times(3)).sendMessage(captor.capture(), any(byte[].class));
List<ResourceUpdatedMessage> values = captor.getAllValues();
Long numBrandUpdates = values.stream().filter(msg -> msg.getUpdatedResource().getId().longValue() == 1234L).collect(Collectors.counting());
Long numItemUpdates = values.stream().filter(msg -> msg.getUpdatedResource().getId().longValue() == 1235L).collect(Collectors.counting());
assertThat(numBrandUpdates, is(2L));
assertThat(numItemUpdates, is(1L));
}
use of org.atlasapi.messaging.ResourceUpdatedMessage in project atlas-deer by atlasapi.
the class CassandraContentStoreIT method testUpdatingContainerUpdatesContainerSummaryInChildItemAndSendsResourceUpdatedMessage.
@Test
public void testUpdatingContainerUpdatesContainerSummaryInChildItemAndSendsResourceUpdatedMessage() throws Exception {
DateTime now = new DateTime(DateTimeZones.UTC);
Brand brand = create(new Brand());
when(clock.now()).thenReturn(now);
when(idGenerator.generateRaw()).thenReturn(1234L);
WriteResult<Brand, Content> brandWriteResult = store.writeContent(brand);
Series series1 = create(new Series());
series1.setBrand(brandWriteResult.getResource());
when(clock.now()).thenReturn(now.plusHours(1));
when(idGenerator.generateRaw()).thenReturn(1235L);
WriteResult<Series, Content> series1WriteResult = store.writeContent(series1);
Episode episode = create(new Episode());
episode.setContainer(brandWriteResult.getResource());
episode.setSeries(series1WriteResult.getResource());
when(clock.now()).thenReturn(now.plusHours(2));
when(idGenerator.generateRaw()).thenReturn(1237L);
store.writeContent(episode);
when(hasher.hash(argThat(isA(Content.class)))).thenReturn("different").thenReturn("differentAgain");
Episode resolvedEpisode = (Episode) resolve(1237L);
assertThat(resolvedEpisode.getFirstSeen(), is(now.plusHours(2)));
assertThat(resolvedEpisode.getLastUpdated(), is(now.plusHours(2)));
assertThat(resolvedEpisode.getThisOrChildLastUpdated(), is(now.plusHours(2)));
assertThat(resolvedEpisode.getContainerRef().getId().longValue(), is(1234L));
assertThat(resolvedEpisode.getSeriesRef().getId().longValue(), is(1235L));
assertThat(resolvedEpisode.getContainerSummary().getTitle(), is("Brand"));
brand.setTitle("NewBrand");
store.writeContent(brand);
Brand resolvedBrand = (Brand) resolve(1234L);
assertThat(resolvedBrand.getTitle(), is("NewBrand"));
ArgumentCaptor<ResourceUpdatedMessage> captor = ArgumentCaptor.forClass(ResourceUpdatedMessage.class);
verify(sender, times(8)).sendMessage(captor.capture(), any(byte[].class));
List<ResourceUpdatedMessage> messagesSent = captor.getAllValues();
assertThat(messagesSent.size(), is(8));
assertTrue(Iterables.tryFind(messagesSent, msg -> msg.getUpdatedResource().getId().longValue() == 1234L).isPresent());
assertTrue(Iterables.tryFind(messagesSent, msg -> msg.getUpdatedResource().getId().longValue() == 1235L).isPresent());
assertTrue(Iterables.tryFind(messagesSent, msg -> msg.getUpdatedResource().getId().longValue() == 1237L).isPresent());
Episode newResolvedEpisode = (Episode) resolve(1237L);
assertThat(newResolvedEpisode.getContainerSummary().getTitle(), is("NewBrand"));
}
use of org.atlasapi.messaging.ResourceUpdatedMessage in project atlas-deer by atlasapi.
the class CassandraContentStoreIT method testWriteSendsResourceUpdatedMessage.
@Test
public void testWriteSendsResourceUpdatedMessage() throws Exception {
Content content = create(new Item());
content.setTitle("title");
DateTime now = new DateTime(DateTimeZones.UTC);
when(clock.now()).thenReturn(now);
when(idGenerator.generateRaw()).thenReturn(1234L);
WriteResult<Content, Content> writeResult = store.writeContent(content);
assertTrue(writeResult.written());
assertThat(writeResult.getResource().getId().longValue(), is(1234L));
assertFalse(writeResult.getPrevious().isPresent());
ArgumentCaptor<ResourceUpdatedMessage> messageCaptor = ArgumentCaptor.forClass(ResourceUpdatedMessage.class);
verify(sender).sendMessage(messageCaptor.capture(), eq(Longs.toByteArray(1234L)));
assertThat(messageCaptor.getValue().getUpdatedResource().getId().longValue(), is(1234L));
}
Aggregations