Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: have chance to lost trigger at server restart #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ public List<OperableTrigger> getTriggersForJob(JobKey jobKey, T jedis) throws Jo
* @throws org.quartz.JobPersistenceException if the unset operation failed
*/
public abstract boolean unsetTriggerState(final String triggerHashKey, T jedis) throws JobPersistenceException;
public abstract boolean unsetTriggerState(final String triggerHashKey, RedisTriggerState excludeState, T jedis) throws JobPersistenceException;


/**
* Set a trigger state by adding the trigger to the relevant sorted set, using its next fire time as the score.
Expand All @@ -339,8 +341,12 @@ public List<OperableTrigger> getTriggersForJob(JobKey jobKey, T jedis) throws Jo
public boolean setTriggerState(final RedisTriggerState state, final double score, final String triggerHashKey, T jedis) throws JobPersistenceException{
boolean success = false;
if(state != null){
unsetTriggerState(triggerHashKey, jedis);
//BUG FIX, delete all the key first, and then add new may leading to lost trigger, if the service restart at that moment
//So we need to add first, and delete later -gs
//unsetTriggerState(triggerHashKey, jedis);
success = jedis.zadd(redisSchema.triggerStateKey(state), score, triggerHashKey) == 1;
unsetTriggerState(triggerHashKey, state, jedis);

}
return success;
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/joelinn/quartz/jobstore/RedisClusterStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,26 @@ public boolean unsetTriggerState(String triggerHashKey, JedisClusterCommandsWrap
return removed;
}

@Override
public boolean unsetTriggerState(String triggerHashKey, RedisTriggerState excludeState, JedisClusterCommandsWrapper jedis) throws JobPersistenceException {
boolean removed = false;
List<Long> responses = new ArrayList<>(RedisTriggerState.values().length);
for (RedisTriggerState state : RedisTriggerState.values()) {
if (state.name().equalsIgnoreCase(excludeState.name())){
continue;
}
responses.add(jedis.zrem(redisSchema.triggerStateKey(state), triggerHashKey));
}
for (Long response : responses) {
removed = response == 1;
if (removed) {
jedis.del(redisSchema.triggerLockKey(redisSchema.triggerKey(triggerHashKey)));
break;
}
}
return removed;
}

/**
* Store a {@link Calendar}
*
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/net/joelinn/quartz/jobstore/RedisStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,29 @@ public boolean unsetTriggerState(final String triggerHashKey, Jedis jedis) throw
return removed;
}

@Override
public boolean unsetTriggerState(final String triggerHashKey,RedisTriggerState excludeState, Jedis jedis) throws JobPersistenceException {
boolean removed = false;
Pipeline pipe = jedis.pipelined();
List<Response<Long>> responses = new ArrayList<>(RedisTriggerState.values().length);
for (RedisTriggerState state : RedisTriggerState.values()) {
if (state.name().equalsIgnoreCase(excludeState.name())){
continue;
}
responses.add(pipe.zrem(redisSchema.triggerStateKey(state), triggerHashKey));
}

pipe.sync();
for (Response<Long> response : responses) {
removed = response.get() == 1;
if(removed){
jedis.del(redisSchema.triggerLockKey(redisSchema.triggerKey(triggerHashKey)));
break;
}
}
return removed;
}

/**
* Store a {@link org.quartz.Calendar}
* @param name the name of the calendar
Expand Down