1

How do I trigger an action to be performed (set the property to something) on a node after a particular time in neo4j?

I know about the graphaware's neo4j-expire, but it only deletes nodes or relationships when the time is up, which is not what I want?

6
  • You need apoc.periodic.schedule procedure: neo4j-contrib.github.io/neo4j-apoc-procedures/#_job_management Commented Oct 10, 2018 at 15:20
  • I think apoc.periodic.schedule will run the statement multiple times, right? I want the statement to run once when it's time Commented Oct 10, 2018 at 15:48
  • 1
    The task runs every second, you check the onset of the desired time, if it has arrived: launch the query and delete the task (apoc.periodic.cancel). Commented Oct 10, 2018 at 16:01
  • 1
    You can also use a combination of apoc.date.expire and apoc.trigger procedures. Commented Oct 10, 2018 at 16:06
  • apoc.periodic.cancel - Maybe I have 10000 (or more) nodes that needs to be invalidated after different time-ups, what about performance issues? Commented Oct 10, 2018 at 18:31

1 Answer 1

1

You can use a combination of apoc.date.expireIn and apoc.trigger procedures. For example first add trigger:

CALL apoc.trigger.add('doVertexTask', ' UNWIND {deletedRelationships} AS dRel WITH dRel WHERE type(dRel) = "taskRelation" WITH endNode(dRel) AS vertexNode WHERE "Vertex" IN labels(vertexNode) SET vertexNode.prop = rand() RETURN true', {phase: "before"}) 

Then add the data and task:

MERGE (A:Vertex {id: 1}) CREATE (T:TASK) CREATE (T)-[:taskRelation]->(A) WITH A, T CALL apoc.date.expireIn(T, 10, 's') RETURN A, T 
Sign up to request clarification or add additional context in comments.

5 Comments

I tried it, but no property (SET vertexNode.prop = rand()) was set on node A:Vertex after 10 seconds.
It works for me. Check that all necessary configuration settings have been made (neo4.conf) for apoc.trigger and apoc.date.expireIn.
It finally work using a combination of apoc.trigger.add and apoc.date.expireIn when I enabled those procedures. Thanks, but one more thing, I don't understand this line UNWIND {deletedRelationships} AS dRel. I thought the :taskRelation relationship will be deleted, but it was not deleted because when I MATCH the relationship, it still shows me the :Task and :Vertex node connected even after the prop was set. That's also what I need, but I just want to understand what triggered that action.
@Emmanuel This is strange, because the expired node must be removed. Try look in the logs - if there is any error messages.
Yeah, that's true, the expired node was removed. Now I understand.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.