Skip to content

Commit

Permalink
fix: prod crashes with areas
Browse files Browse the repository at this point in the history
  • Loading branch information
EdenComp authored and RezaRahemtola committed Oct 25, 2023
1 parent e4dbe17 commit ae3bba8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
16 changes: 12 additions & 4 deletions backend/back/src/workflows/workflows.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import UpdateWorkflowDto from "./dto/update-workflow.dto";
import UserConnection from "../connections/entities/user-connection.entity";
import { JobsIdentifiers } from "../types/jobIds";
import { JobsService } from "../jobs/jobs.service";
import { JobsType } from "../types/jobs";
import { JobParamsClasses, JobsType } from "../types/jobs";
import Service from "../services/entities/service.entity";
import { ConnectionsService } from "../connections/connections.service";
import { ServiceName } from "../services/services.service";
import { UniqueJobParams } from "../types/jobParams";
import { plainToInstance } from "class-transformer";

@Injectable()
export class WorkflowsService {
Expand Down Expand Up @@ -340,7 +342,11 @@ export class WorkflowsService {
return dbReactions;
}

private async getNeededNewScopes(userId: string, serviceId: ServiceName, neededScopeIds: string[]) {
private async getNeededNewScopes(
userId: string,
serviceId: ServiceName,
neededScopeIds: string[],
): Promise<string[] | null> {
const service = await this.workflowRepository.manager.findOneBy(Service, { id: serviceId });
if (!service) throw new NotFoundException(`Service ${serviceId} not found.`);
if (!service.needConnection) return [];
Expand Down Expand Up @@ -389,13 +395,15 @@ export class WorkflowsService {
areaServiceId,
workflowArea.area.serviceScopesNeeded.map(({ id }) => id),
);
if (neededNewScopes.length > 0) {
if (neededNewScopes && neededNewScopes.length > 0) {
throw new BadRequestException(
`You need to connect to ${areaServiceId} with scopes ${neededNewScopes.join(", ")}.`,
);
}
const jobType = `${areaServiceId}-${areaId}`;
parameters.workflowStepId = id;
if (plainToInstance(JobParamsClasses[jobType], parameters) instanceof UniqueJobParams) {
parameters.workflowStepId = id;
}
workflowArea.parameters = await this.jobsService.convertParams(jobType as JobsType, parameters).catch((err) => {
throw new BadRequestException(`Invalid parameters for workflow area ${id} (${jobType}): ${err.message}`);
});
Expand Down
5 changes: 0 additions & 5 deletions backend/back/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2264,11 +2264,6 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

crypto@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/crypto/-/crypto-1.0.1.tgz#2af1b7cad8175d24c8a1b0778255794a21803037"
integrity sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==

date-fns@^2.29.3:
version "2.30.0"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.30.0.tgz#f367e644839ff57894ec6ac480de40cae4b0f4d0"
Expand Down
17 changes: 5 additions & 12 deletions backend/supervisor/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"log"
"regexp"
)

type JobManager struct {
Expand All @@ -24,6 +25,7 @@ type Job struct {
}

var instance *JobManager
var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9 ]+`)

func InitJobManager(cli *client.Client, callbackUrl string, env string) {
instance = &JobManager{cli, map[string]Job{}, callbackUrl, env == "production", 0}
Expand Down Expand Up @@ -119,16 +121,6 @@ func (jm *JobManager) KillJob(identifier string) error {
return err
}

if !jm.production {
err = jm.dockerClient.ContainerRemove(context.Background(), job.containerID, types.ContainerRemoveOptions{
Force: true,
})
if err != nil {
log.Printf("Error when removing job %s: %v\n", identifier, err)
return err
}
}

delete(jm.jobs, identifier)
log.Printf("Job %s killed\n", identifier)
return nil
Expand Down Expand Up @@ -170,10 +162,11 @@ func (jm *JobManager) cleanContainers() error {
}

func (jm *JobManager) generateContainerName(identifier string) string {
name := nonAlphanumericRegex.ReplaceAllString(identifier, "-")
if jm.production {
return identifier
return name
}
return fmt.Sprintf("%s-%d", identifier, jm.jobsCount)
return fmt.Sprintf("%s-%d", name, jm.jobsCount)
}

func isSupervisorContainer(image string) bool {
Expand Down
1 change: 1 addition & 0 deletions backend/workers/github/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/
node_modules/
proto/
.env

0 comments on commit ae3bba8

Please sign in to comment.