Skip to content

Commit d095f2f

Browse files
committed
create agent
1 parent 720d188 commit d095f2f

File tree

11 files changed

+122
-8
lines changed

11 files changed

+122
-8
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.flowci.agent;
2+
3+
import io.swagger.v3.oas.annotations.tags.Tag;
4+
import lombok.AllArgsConstructor;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
@RequestMapping("/v2/agents")
10+
@Tag(name = "agent")
11+
@AllArgsConstructor
12+
public class AgentController {
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.flowci.agent.business;
2+
3+
import com.flowci.agent.model.Agent;
4+
5+
import java.util.List;
6+
7+
public interface CreateAgent {
8+
Agent invoke(String alias, List<String> tags);
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.flowci.agent.business;
2+
3+
import com.flowci.agent.model.Agent;
4+
import com.flowci.agent.repo.AgentRepo;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.util.List;
10+
11+
@Slf4j
12+
@Component
13+
@RequiredArgsConstructor
14+
public class CreateAgentImpl implements CreateAgent {
15+
16+
private final AgentRepo agentRepo;
17+
18+
@Override
19+
public Agent invoke(String alias, List<String> tags) {
20+
var agent = new Agent();
21+
agent.setAlias(alias);
22+
agent.setTags(tags.toArray(new String[0]));
23+
return agentRepo.save(agent);
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.flowci.agent.events;
2+
3+
import lombok.Getter;
4+
import org.springframework.context.ApplicationEvent;
5+
6+
@Getter
7+
public class AgentIdleEvent extends ApplicationEvent {
8+
9+
private final Long agentId;
10+
11+
public AgentIdleEvent(Object source, Long agentId) {
12+
super(source);
13+
this.agentId = agentId;
14+
}
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.flowci.agent.model;
2+
3+
import com.flowci.common.model.EntityBase;
4+
import io.hypersistence.utils.hibernate.type.array.StringArrayType;
5+
import jakarta.persistence.*;
6+
import lombok.Data;
7+
import lombok.EqualsAndHashCode;
8+
9+
@Data
10+
@EqualsAndHashCode(callSuper = false, of = {"id"})
11+
@Entity
12+
@Table(name = "agents")
13+
public class Agent extends EntityBase {
14+
15+
public enum Status {
16+
OFFLINE,
17+
ONLINE_BUSY,
18+
ONLINE_IDLE,
19+
DISABLED,
20+
}
21+
22+
@Id
23+
private Long id;
24+
25+
private String alias;
26+
27+
// generated
28+
private String token;
29+
30+
// updated when agent online
31+
private String os;
32+
33+
@org.hibernate.annotations.Type(StringArrayType.class)
34+
private String[] tags;
35+
36+
@Enumerated(EnumType.STRING)
37+
private Status status;
38+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package com.flowci.agent;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.flowci.agent.repo;
2+
3+
import com.flowci.agent.model.Agent;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface AgentRepo extends JpaRepository<Agent, Long> {
9+
}

src/main/java/com/flowci/build/model/Build.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@Data
1717
@EqualsAndHashCode(callSuper = false, of = {"id"})
1818
@Entity
19-
@Table(name = "build")
19+
@Table(name = "builds")
2020
public class Build extends EntityBase {
2121

2222
public enum Trigger {
@@ -51,7 +51,7 @@ public enum Status {
5151

5252
@Id
5353
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "build_id_gen")
54-
@SequenceGenerator(name = "build_id_gen", sequenceName = "build_id_sequence", allocationSize = 1)
54+
@SequenceGenerator(name = "build_id_gen", sequenceName = "builds_id_sequence", allocationSize = 1)
5555
private Long id;
5656

5757
private Long flowId;

src/main/java/com/flowci/build/model/BuildYaml.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@Data
1414
@Entity
15-
@Table(name = "build_yaml")
15+
@Table(name = "builds_yaml")
1616
@EqualsAndHashCode(callSuper = false, of = "id")
1717
public class BuildYaml extends EntityBase {
1818

src/main/resources/db/migration/V1__Init.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CREATE TABLE flows_user
4141

4242
-- build related tables --
4343

44-
CREATE TABLE build
44+
CREATE TABLE builds
4545
(
4646
id BIGSERIAL PRIMARY KEY,
4747
flow_id BIGINT NOT NULL,
@@ -60,8 +60,8 @@ CREATE TABLE build
6060
UNIQUE (flow_id, build_date, build_sequence)
6161
);
6262

63-
DROP SEQUENCE IF EXISTS build_id_sequence;
64-
CREATE SEQUENCE build_id_sequence START 10000 INCREMENT 1 OWNED BY build.id;
63+
DROP SEQUENCE IF EXISTS builds_id_sequence;
64+
CREATE SEQUENCE builds_id_sequence START 10000 INCREMENT 1 OWNED BY builds.id;
6565

6666
-- trigger function to create build_date, build_sequence and build_alias on insert
6767
CREATE OR REPLACE FUNCTION auto_build_sequence() RETURNS trigger AS
@@ -84,11 +84,11 @@ $build_sequence$ LANGUAGE plpgsql;
8484
-- add trigger on insert
8585
CREATE TRIGGER build_sequence_trigger
8686
BEFORE INSERT
87-
ON "build"
87+
ON "builds"
8888
FOR EACH ROW
8989
EXECUTE PROCEDURE auto_build_sequence();
9090

91-
CREATE TABLE build_yaml
91+
CREATE TABLE builds_yaml
9292
(
9393
id BIGINT PRIMARY KEY,
9494
variables json NOT NULL,

0 commit comments

Comments
 (0)