Skip to content

Commit

Permalink
initial agent deploy changes
Browse files Browse the repository at this point in the history
  • Loading branch information
benbot committed Oct 25, 2023
1 parent 709c6ed commit e9de120
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
18 changes: 18 additions & 0 deletions apps/server/migrations/20231018135155_addAgentReleaseTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Knex } from "knex";


export async function up(knex: Knex): Promise<void> {
return knex.schema.createTable('AgentReleases', function(table) {
table.increments('id').primary(); // Creates a regular id field as primary key, auto-increments
table.integer('agent_id').unsigned().notNullable(); // Field referencing 'agents' table
table.foreign('agent_id').references('id').inTable('agents'); // Establishes the foreign key relationship
table.string('version', 255).notNullable(); // String field for version tag
table.timestamp('createdAt').defaultTo(knex.fn.now()); // 'createdAt' field with the current timestamp
});
}


export async function down(knex: Knex): Promise<void> {
return knex.schema.dropTable('AgentReleases');
}

12 changes: 12 additions & 0 deletions apps/server/migrations/20231025125524_addFrozenFlagToAgents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Knex } from "knex";


export async function up(knex: Knex): Promise<void> {

Check warning on line 4 in apps/server/migrations/20231025125524_addFrozenFlagToAgents.ts

View workflow job for this annotation

GitHub Actions / ESLint

'knex' is defined but never used

Check warning on line 4 in apps/server/migrations/20231025125524_addFrozenFlagToAgents.ts

View workflow job for this annotation

GitHub Actions / ESLint

'knex' is defined but never used
table.boolean('frozen').defaultTo(false).notNullable();
}


export async function down(knex: Knex): Promise<void> {

Check warning on line 9 in apps/server/migrations/20231025125524_addFrozenFlagToAgents.ts

View workflow job for this annotation

GitHub Actions / ESLint

'knex' is defined but never used

Check warning on line 9 in apps/server/migrations/20231025125524_addFrozenFlagToAgents.ts

View workflow job for this annotation

GitHub Actions / ESLint

'knex' is defined but never used
table.dropColumn('frozen');
}

50 changes: 50 additions & 0 deletions packages/core/server/src/services/agents/agents.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ export class AgentService<
})
}



async get(agentId: string, params: ServiceParams) {
const { versionTag } = params

Check warning on line 47 in packages/core/server/src/services/agents/agents.class.ts

View workflow job for this annotation

GitHub Actions / ESLint

'versionTag' is assigned a value but never used

Check warning on line 47 in packages/core/server/src/services/agents/agents.class.ts

View workflow job for this annotation

GitHub Actions / ESLint

'versionTag' is assigned a value but never used
const db = app.get('dbClient')

const agents = db('agents')
.select('agents.*')
.innerJoin('AgentReleases', 'agents.id', 'AgentReleases.agent_id')
.groupBy('agentId')

return agents
}

async find(params?: ServiceParams): Promise<Agent[] | Paginated<Agent>> {
// Modify the query to exclude agents with the frozen flag set
const { query = {} } = params || {};
query.frozen = false; // Only fetch agents where frozen is false

// Call the original find method with the modified query
return super.find({ ...params, query });
}

// we use this ping to avoid firing a patched event on the agent
// every time the agent is pinged
async ping(agentId: string) {
Expand All @@ -65,6 +88,33 @@ export class AgentService<
return { jobId: job.id }
}

/**
* Creates a new agent by copying data from the provided agentId, then associates it with a version tag in the AgentReleases table.
* @param agentId - the ID of the agent to copy from
* @param versionTag - the version tag to associate with the newly created agent
*/
async copyAndRelease(agentId: string, versionTag: string): Promise<{agent: Agent, release: any}> {
// Get the agent by its agentId
const existingAgent = await this.app.service('agents').get(agentId);
if (!existingAgent) {
throw new Error(`Agent with ID ${agentId} not found.`);
}

// Copy data from the fetched agent, omitting the ID field to create a new agent
const { id, ...agentData } = existingAgent;

Check warning on line 104 in packages/core/server/src/services/agents/agents.class.ts

View workflow job for this annotation

GitHub Actions / ESLint

'id' is assigned a value but never used

Check warning on line 104 in packages/core/server/src/services/agents/agents.class.ts

View workflow job for this annotation

GitHub Actions / ESLint

'id' is assigned a value but never used
const newAgent = await this.app.service('agents').create(agentData);

// Add the new agent's ID and the provided version tag to the AgentReleases table
const db = this.app.get('dbClient');
const release = await db('AgentReleases').insert({
agent_id: newAgent.id,
version: versionTag,
});

return { agent: newAgent, release };
}


async create(
data: AgentData | AgentData[] | any
): Promise<Agent | Agent[] | any> {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/server/src/services/agents/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export const agent = (app: Application) => {
events: AGENT_EVENTS,
})

app.use('/agents/release', new AgentService(getOptions(app), app), {
async create(data, params) {

Check warning on line 44 in packages/core/server/src/services/agents/agents.ts

View workflow job for this annotation

GitHub Actions / ESLint

'params' is defined but never used

Check warning on line 44 in packages/core/server/src/services/agents/agents.ts

View workflow job for this annotation

GitHub Actions / ESLint

'params' is defined but never used
const agentService = app.service('agents');
return await agentService.copyAndRelease(data.agentId, data.versionTag);
},
})

const pubSub = app.get<'pubsub'>('pubsub')

// this handles relaying all agent messages up to connected clients.
Expand Down

0 comments on commit e9de120

Please sign in to comment.