Skip to content

Commit

Permalink
refactor(core): simplify RemoteJob user assignment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cmark committed Nov 22, 2024
1 parent 0de188e commit 820f68d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 B2i Healthcare, https://b2ihealthcare.com
* Copyright 2017-2024 B2i Healthcare, https://b2ihealthcare.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +34,6 @@
import com.b2international.snowowl.core.api.SnowowlRuntimeException;
import com.b2international.snowowl.core.events.Request;
import com.b2international.snowowl.core.identity.User;
import com.b2international.snowowl.core.identity.request.UserRequests;
import com.b2international.snowowl.core.status.Statuses;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Predicate;
Expand All @@ -51,19 +50,19 @@ public final class RemoteJob extends Job {

private final String id;
private final String key;
private final User user;
private final String description;
private final ServiceProvider context;
private final Request<ServiceProvider, ?> request;

private String response;
private String user;
private boolean autoClean;

public RemoteJob(
final String id,
final String key,
final String description,
final String user,
final User user,
final ServiceProvider context,
final Request<ServiceProvider, ?> request,
final boolean autoClean) {
Expand All @@ -82,14 +81,6 @@ protected final IStatus run(IProgressMonitor monitor) {
final ObjectMapper mapper = this.context.service(ObjectMapper.class);
final IProgressMonitor trackerMonitor = this.context.service(RemoteJobTracker.class).createMonitor(id, monitor);
try {
// override user when necessary
User user = context.service(User.class);
if (User.isSystem(this.user)) {
user = User.SYSTEM;
} else if (!user.getUserId().equals(this.user)) {
user = UserRequests.prepareGet(this.user).build().execute(this.context);
}

// seed the monitor instance into the current context, so the request can use it for progress reporting
final ServiceProvider context = this.context.inject()
.bind(IProgressMonitor.class, trackerMonitor)
Expand Down Expand Up @@ -166,7 +157,7 @@ String getDescription() {
}

public String getUser() {
return user;
return user.getUserId();
}

String getResponse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
*/
package com.b2international.snowowl.core.jobs;

import java.util.Objects;
import java.util.Optional;

import jakarta.validation.constraints.NotNull;

import org.eclipse.core.runtime.jobs.ILock;
import org.eclipse.core.runtime.jobs.Job;
import jakarta.validation.constraints.NotEmpty;

import com.b2international.commons.exceptions.AlreadyExistsException;
import com.b2international.commons.exceptions.BadRequestException;
Expand All @@ -30,9 +28,13 @@
import com.b2international.snowowl.core.events.Request;
import com.b2international.snowowl.core.id.IDs;
import com.b2international.snowowl.core.identity.User;
import com.b2international.snowowl.core.identity.request.UserRequests;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

/**
* @since 5.7
*/
Expand Down Expand Up @@ -122,9 +124,20 @@ public String execute(ServiceProvider context) {
}
}

final String userId = !Strings.isNullOrEmpty(user) ? user : context.service(User.class).getUserId();
User user;

// override user if requested
if (User.isSystem(this.user)) {
// hidden system jobs
user = User.SYSTEM;
} else if (!Strings.isNullOrEmpty(this.user) && !Objects.equals(this.user, context.service(User.class).getUserId())) {
// run jobs on behalf of others
user = UserRequests.prepareGet(this.user).build().execute(context);
} else {
user = context.service(User.class);
}

RemoteJob job = new RemoteJob(id, key, description, userId, context, request, autoClean);
RemoteJob job = new RemoteJob(id, key, description, user, context, request, autoClean);
job.setSystem(true);
if (schedulingRule != null) {
job.setRule(schedulingRule);
Expand Down

0 comments on commit 820f68d

Please sign in to comment.