Skip to content

Commit

Permalink
Create FileKeys in the background as a single operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylangisc committed Nov 26, 2024
1 parent 0579cdf commit e6ff767
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,80 @@ public AbstractSchedulerFeature(final long period) {

@Override
public Void repeat(final SessionPool pool, final PasswordCallback callback) {
scheduler.repeat(() -> {
scheduler.repeat(new PoolOperator(pool, callback), period, TimeUnit.MILLISECONDS);
return null;
}

public Void single(final Session<?> session, final PasswordCallback callback, final Path file) {
scheduler.schedule(new SessionOperator(session, callback, file), 0L, TimeUnit.MILLISECONDS);
return null;
}

protected abstract R operate(Session<?> session, PasswordCallback callback, Path file) throws BackgroundException;

@Override
public void shutdown() {
log.debug("Shutting down scheduler thread pool");
scheduler.shutdown();
}

private class PoolOperator implements Runnable {
private final SessionPool pool;
private final PasswordCallback callback;

public PoolOperator(final SessionPool pool, final PasswordCallback callback) {
this.pool = pool;
this.callback = callback;
}

@Override
public void run() {
try {
final Session<?> session = pool.borrow(BackgroundActionState.running);
try {
this.operate(session, callback, null);
AbstractSchedulerFeature.this.operate(session, callback, null);
}
finally {
pool.release(session, null);
}
}
catch(LoginFailureException | ConnectionCanceledException e) {
log.warn("Cancel processing scheduled task after failure {}", e.getMessage());
this.shutdown();
AbstractSchedulerFeature.this.shutdown();
}
catch(BackgroundException e) {
log.warn("Failure processing scheduled task. {}", e.getMessage(), e);
}
catch(Exception e) {
log.error("Failure processing scheduled task {}", e.getMessage(), e);
this.shutdown();
AbstractSchedulerFeature.this.shutdown();
}
}, period, TimeUnit.MILLISECONDS);
return null;
}
}

protected abstract R operate(Session<?> session, PasswordCallback callback, Path file) throws BackgroundException;
private class SessionOperator implements Runnable {
private final Session<?> session;
private final PasswordCallback callback;
private final Path file;

@Override
public void shutdown() {
log.debug("Shutting down scheduler thread pool");
scheduler.shutdown();
public SessionOperator(final Session<?> session, final PasswordCallback callback, final Path file) {
this.session = session;
this.callback = callback;
this.file = file;
}

@Override
public void run() {
try {
AbstractSchedulerFeature.this.operate(session, callback, file);
}
catch(BackgroundException e) {
log.warn("Failure processing scheduled task. {}", e.getMessage(), e);
}
catch(Exception e) {
log.error("Failure processing scheduled task {}", e.getMessage(), e);
AbstractSchedulerFeature.this.shutdown();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void post(final Transfer.Type type, final Map<TransferItem, TransferStatu
final Path container = containerService.getContainer(file);
if(rooms.get(container)) {
log.debug("Run missing file keys for {}", file);
background.operate(session, callback, file);
background.single(session, callback, file);
}
}
}
Expand Down

0 comments on commit e6ff767

Please sign in to comment.