@Test public void testUnschedule() { AmazonDynamoDB dynamoDB = mock(AmazonDynamoDB.class); doThrow(new ConditionalCheckFailedException("hello")).when(dynamoDB).updateItem(any(UpdateItemRequest.class)); Scheduler scheduler = new Scheduler(dynamoDB); assertFalse(scheduler.unschedule("fault-id-1")); dynamoDB = mock(AmazonDynamoDB.class); doAnswer(i -> { UpdateItemRequest request = i.getArgument(0); assertThat(request.getKey().get("id").getS(), is("fault-id-2")); assertThat(request.getConditionExpression(), is("attribute_exists(id)")); assertThat(request.getUpdateExpression(), is("SET #ttl = :ttl, #status = :status")); Map<String, String> names = request.getExpressionAttributeNames(); assertThat(names.size(), is(2)); assertThat(names.get("#ttl"), is("ttl")); assertThat(names.get("#status"), is("status")); Map<String, AttributeValue> values = request.getExpressionAttributeValues(); assertThat(values.size(), is(2)); assertThat(values.get(":status").getS(), is("INACTIVE")); assertTrue(Long.valueOf(values.get(":ttl").getN()) <= Instant.now().getEpochSecond()); return null; }).when(dynamoDB).updateItem(any(UpdateItemRequest.class)); scheduler = new Scheduler(dynamoDB); assertTrue(scheduler.unschedule("fault-id-2")); }
public boolean unschedule(String id) { Map<String, AttributeValue> key = new HashMap<>(); key.put("id", new AttributeValue().withS(id)); String condition = "attribute_exists(id)"; String update = "SET #ttl = :ttl, #status = :status"; Map<String, String> names = new HashMap<>(); names.put("#ttl", "ttl"); names.put("#status", "status"); Map<String, AttributeValue> values = new HashMap<>(); values.put(":ttl", new AttributeValue().withN(String.valueOf(Instant.now().getEpochSecond()))); values.put(":status", new AttributeValue().withS("INACTIVE")); UpdateItemRequest request = new UpdateItemRequest().withTableName(Prodigy.configuration().scheduler()) .withKey(key).withConditionExpression(condition).withUpdateExpression(update) .withExpressionAttributeNames(names).withExpressionAttributeValues(values); try { dynamoDB.updateItem(request); return true; } catch (ConditionalCheckFailedException e) { log.warn(e.getMessage(), e); return false; } }
Scheduler { public boolean unschedule(String id) { Map<String, AttributeValue> key = new HashMap<>(); key.put("id", new AttributeValue().withS(id)); String condition = "attribute_exists(id)"; String update = "SET #ttl = :ttl, #status = :status"; Map<String, String> names = new HashMap<>(); names.put("#ttl", "ttl"); names.put("#status", "status"); Map<String, AttributeValue> values = new HashMap<>(); values.put(":ttl", new AttributeValue().withN(String.valueOf(Instant.now().getEpochSecond()))); values.put(":status", new AttributeValue().withS("INACTIVE")); UpdateItemRequest request = new UpdateItemRequest().withTableName(Prodigy.configuration().scheduler()) .withKey(key).withConditionExpression(condition).withUpdateExpression(update) .withExpressionAttributeNames(names).withExpressionAttributeValues(values); try { dynamoDB.updateItem(request); return true; } catch (ConditionalCheckFailedException e) { log.warn(e.getMessage(), e); return false; } } }
Scheduler { public boolean unschedule(String id) { Map<String, AttributeValue> key = new HashMap<>(); key.put("id", new AttributeValue().withS(id)); String condition = "attribute_exists(id)"; String update = "SET #ttl = :ttl, #status = :status"; Map<String, String> names = new HashMap<>(); names.put("#ttl", "ttl"); names.put("#status", "status"); Map<String, AttributeValue> values = new HashMap<>(); values.put(":ttl", new AttributeValue().withN(String.valueOf(Instant.now().getEpochSecond()))); values.put(":status", new AttributeValue().withS("INACTIVE")); UpdateItemRequest request = new UpdateItemRequest().withTableName(Prodigy.configuration().scheduler()) .withKey(key).withConditionExpression(condition).withUpdateExpression(update) .withExpressionAttributeNames(names).withExpressionAttributeValues(values); try { dynamoDB.updateItem(request); return true; } catch (ConditionalCheckFailedException e) { log.warn(e.getMessage(), e); return false; } } Scheduler();  Scheduler(AmazonDynamoDB dynamoDB); }
Scheduler { public boolean unschedule(String id) { Map<String, AttributeValue> key = new HashMap<>(); key.put("id", new AttributeValue().withS(id)); String condition = "attribute_exists(id)"; String update = "SET #ttl = :ttl, #status = :status"; Map<String, String> names = new HashMap<>(); names.put("#ttl", "ttl"); names.put("#status", "status"); Map<String, AttributeValue> values = new HashMap<>(); values.put(":ttl", new AttributeValue().withN(String.valueOf(Instant.now().getEpochSecond()))); values.put(":status", new AttributeValue().withS("INACTIVE")); UpdateItemRequest request = new UpdateItemRequest().withTableName(Prodigy.configuration().scheduler()) .withKey(key).withConditionExpression(condition).withUpdateExpression(update) .withExpressionAttributeNames(names).withExpressionAttributeValues(values); try { dynamoDB.updateItem(request); return true; } catch (ConditionalCheckFailedException e) { log.warn(e.getMessage(), e); return false; } } Scheduler();  Scheduler(AmazonDynamoDB dynamoDB); boolean schedule(Fault fault); boolean unschedule(String id); Entry get(String id); List<Entry> list(); }
Scheduler { public boolean unschedule(String id) { Map<String, AttributeValue> key = new HashMap<>(); key.put("id", new AttributeValue().withS(id)); String condition = "attribute_exists(id)"; String update = "SET #ttl = :ttl, #status = :status"; Map<String, String> names = new HashMap<>(); names.put("#ttl", "ttl"); names.put("#status", "status"); Map<String, AttributeValue> values = new HashMap<>(); values.put(":ttl", new AttributeValue().withN(String.valueOf(Instant.now().getEpochSecond()))); values.put(":status", new AttributeValue().withS("INACTIVE")); UpdateItemRequest request = new UpdateItemRequest().withTableName(Prodigy.configuration().scheduler()) .withKey(key).withConditionExpression(condition).withUpdateExpression(update) .withExpressionAttributeNames(names).withExpressionAttributeValues(values); try { dynamoDB.updateItem(request); return true; } catch (ConditionalCheckFailedException e) { log.warn(e.getMessage(), e); return false; } } Scheduler();  Scheduler(AmazonDynamoDB dynamoDB); boolean schedule(Fault fault); boolean unschedule(String id); Entry get(String id); List<Entry> list();  }