mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2025-07-23 12:51:40 +02:00
Refactor job repository API. Cleanup.
This commit is contained in:
@@ -13,7 +13,34 @@ type JobRepository struct {
|
||||
DB *sqlx.DB
|
||||
}
|
||||
|
||||
func (r *JobRepository) FindJobById(
|
||||
// Find executes a SQL query to find a specific batch job.
|
||||
// The job is queried using the batch job id, the cluster name,
|
||||
// and the start time of the job in UNIX epoch time seconds.
|
||||
// It returns a pointer to a schema.Job data structure and an error variable.
|
||||
// If the job was not found nil is returned for the job pointer.
|
||||
func (r *JobRepository) Find(
|
||||
jobId int64,
|
||||
cluster string,
|
||||
startTime int64) (*schema.Job, error) {
|
||||
qb := sq.Select(schema.JobColumns...).From("job").
|
||||
Where("job.job_id = ?", jobId).
|
||||
Where("job.cluster = ?", cluster).
|
||||
Where("job.start_time = ?", startTime)
|
||||
|
||||
sql, args, err := qb.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
job, err := schema.ScanJob(r.DB.QueryRowx(sql, args...))
|
||||
return job, err
|
||||
}
|
||||
|
||||
// FindById executes a SQL query to find a specific batch job.
|
||||
// The job is queried using the database id.
|
||||
// It returns a pointer to a schema.Job data structure and an error variable.
|
||||
// If the job was not found nil is returned for the job pointer.
|
||||
func (r *JobRepository) FindById(
|
||||
jobId int64) (*schema.Job, error) {
|
||||
sql, args, err := sq.Select(schema.JobColumns...).
|
||||
From("job").Where("job.id = ?", jobId).ToSql()
|
||||
@@ -25,36 +52,7 @@ func (r *JobRepository) FindJobById(
|
||||
return job, err
|
||||
}
|
||||
|
||||
// func (r *JobRepository) FindJobsByFilter( ) ([]*schema.Job, int, error) {
|
||||
|
||||
// }
|
||||
|
||||
func (r *JobRepository) FindJobByIdWithUser(
|
||||
jobId int64,
|
||||
username string) (*schema.Job, error) {
|
||||
|
||||
sql, args, err := sq.Select(schema.JobColumns...).
|
||||
From("job").
|
||||
Where("job.id = ?", jobId).
|
||||
Where("job.user = ?", username).ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
job, err := schema.ScanJob(r.DB.QueryRowx(sql, args...))
|
||||
return job, err
|
||||
}
|
||||
|
||||
func (r *JobRepository) JobExists(
|
||||
jobId int64,
|
||||
cluster string,
|
||||
startTime int64) (rows *sql.Rows, err error) {
|
||||
rows, err = r.DB.Query(`SELECT job.id FROM job WHERE job.job_id = ? AND job.cluster = ? AND job.start_time = ?`,
|
||||
jobId, cluster, startTime)
|
||||
return
|
||||
}
|
||||
|
||||
func (r *JobRepository) Add(job schema.JobMeta) (res sql.Result, err error) {
|
||||
func (r *JobRepository) Start(job schema.JobMeta) (res sql.Result, err error) {
|
||||
res, err = r.DB.NamedExec(`INSERT INTO job (
|
||||
job_id, user, project, cluster, `+"`partition`"+`, array_job_id, num_nodes, num_hwthreads, num_acc,
|
||||
exclusive, monitoring_status, smt, job_state, start_time, duration, resources, meta_data
|
||||
@@ -66,39 +64,6 @@ func (r *JobRepository) Add(job schema.JobMeta) (res sql.Result, err error) {
|
||||
}
|
||||
|
||||
func (r *JobRepository) Stop(
|
||||
jobId int64,
|
||||
cluster string,
|
||||
startTime int64) (job *schema.Job, err error) {
|
||||
var sql string
|
||||
var args []interface{}
|
||||
qb := sq.Select(schema.JobColumns...).From("job").
|
||||
Where("job.job_id = ?", jobId).
|
||||
Where("job.cluster = ?", cluster)
|
||||
if startTime != 0 {
|
||||
qb = qb.Where("job.start_time = ?", startTime)
|
||||
}
|
||||
sql, args, err = qb.ToSql()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
job, err = schema.ScanJob(r.DB.QueryRowx(sql, args...))
|
||||
return
|
||||
}
|
||||
|
||||
func (r *JobRepository) StopById(id string) (job *schema.Job, err error) {
|
||||
var sql string
|
||||
var args []interface{}
|
||||
qb := sq.Select(schema.JobColumns...).From("job").
|
||||
Where("job.id = ?", id)
|
||||
sql, args, err = qb.ToSql()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
job, err = schema.ScanJob(r.DB.QueryRowx(sql, args...))
|
||||
return
|
||||
}
|
||||
|
||||
func (r *JobRepository) Close(
|
||||
jobId int64,
|
||||
duration int32,
|
||||
state schema.JobState,
|
||||
@@ -136,31 +101,6 @@ func (r *JobRepository) Close(
|
||||
}
|
||||
}
|
||||
|
||||
func (r *JobRepository) findById(id string) (job *schema.Job, err error) {
|
||||
var sql string
|
||||
var args []interface{}
|
||||
sql, args, err = sq.Select(schema.JobColumns...).From("job").Where("job.id = ?", id).ToSql()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
job, err = schema.ScanJob(r.DB.QueryRowx(sql, args...))
|
||||
return
|
||||
}
|
||||
|
||||
func (r *JobRepository) Exists(jobId int64) bool {
|
||||
rows, err := r.DB.Query(`SELECT job.id FROM job WHERE job.job_id = ?`, jobId)
|
||||
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if rows.Next() {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *JobRepository) AddTag(jobId int64, tagId int64) error {
|
||||
_, err := r.DB.Exec(`INSERT INTO jobtag (job_id, tag_id) VALUES (?, ?)`, jobId, tagId)
|
||||
return err
|
||||
|
Reference in New Issue
Block a user