Skip to content

Commit

Permalink
mxqkill: Use cancel_job()
Browse files Browse the repository at this point in the history
Use the function canel_job() introduced by the previous commit to cancel
a single job.
  • Loading branch information
donald committed Dec 28, 2023
1 parent 57c2214 commit 0e29847
Showing 1 changed file with 2 additions and 122 deletions.
124 changes: 2 additions & 122 deletions mxqkill.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,92 +210,12 @@ static void set_job_cancelled(struct mx_mysql *mysql, uint64_t job_id) {
mx_mysql_statement_close(&stmt);
}

__attribute__ ((unused))
static void cancel_job(struct mx_mysql *mysql, uint64_t job_id, uint64_t user_uid) {

verify_job_owner(mysql, job_id, user_uid);
set_job_cancelled(mysql, job_id);
}

static int update_job_status_cancelling_by_job_id_for_user(struct mx_mysql *mysql, uint64_t job_id, uint64_t user_uid)
{
struct mx_mysql_stmt *stmt = NULL;
unsigned long long num_rows = 0;
int res;

assert(job_id);

stmt = mx_mysql_statement_prepare(mysql,
"UPDATE mxq_job AS j"
" LEFT JOIN mxq_group AS g"
" ON j.group_id = g.group_id"
" SET"
" job_status = " status_str(MXQ_JOB_STATUS_CANCELLING)
" WHERE job_id = ?"
" AND user_uid = ?"
" AND job_status IN (" status_str(MXQ_JOB_STATUS_INQ) "," status_str(MXQ_JOB_STATUS_ASSIGNED) ")"
" AND host_hostname = ''"
" AND server_id = ''"
" AND host_pid = 0"
);
if (!stmt) {
mx_log_err("mx_mysql_statement_prepare(): %m");
return -(errno=EIO);
}

mx_mysql_statement_param_bind(stmt, 0, uint64, &(job_id));
mx_mysql_statement_param_bind(stmt, 1, uint64, &(user_uid));

res = mx_mysql_statement_execute(stmt, &num_rows);

if (res < 0)
mx_log_err("mx_mysql_statement_execute(): %m");

mx_mysql_statement_close(&stmt);

if (res < 0)
return -(errno=-res);

return (int)num_rows;
}

static int update_job_status_cancelled_by_job_id(struct mx_mysql *mysql, uint64_t job_id)
{
struct mx_mysql_stmt *stmt = NULL;
unsigned long long num_rows = 0;
int res;

assert(job_id);

stmt = mx_mysql_statement_prepare(mysql,
"UPDATE mxq_job SET"
" job_status = " status_str(MXQ_JOB_STATUS_CANCELLED)
" WHERE job_id = ?"
" AND job_status = " status_str(MXQ_JOB_STATUS_CANCELLING)
" AND host_hostname = ''"
" AND server_id = ''"
" AND host_pid = 0"
);
if (!stmt) {
mx_log_err("mx_mysql_statement_prepare(): %m");
return -(errno=EIO);
}

mx_mysql_statement_param_bind(stmt, 0, uint64, &(job_id));

res = mx_mysql_statement_execute(stmt, &num_rows);

if (res < 0)
mx_log_err("mx_mysql_statement_execute(): %m");

mx_mysql_statement_close(&stmt);

if (res < 0)
return -(errno=-res);

return (int)num_rows;
}

int main(int argc, char *argv[])
{
struct mx_mysql *mysql = NULL;
Expand Down Expand Up @@ -477,48 +397,8 @@ int main(int argc, char *argv[])
mx_log_info("MySQL: Connection to database established.");

if (arg_job_id) {
int res1, res2;

res1 = update_job_status_cancelling_by_job_id_for_user(mysql, arg_job_id, passwd->pw_uid);
res2 = update_job_status_cancelled_by_job_id(mysql, arg_job_id);

mx_mysql_finish(&mysql);
mx_log_info("MySQL: Connection to database closed.");

if (res1 == -ENOENT)
res1=0;

if (res2 == -ENOENT)
res1=0;

if (res1 < 0)
mx_log_err("setting status of job %lu to CANCELLING failed: %s", arg_job_id, strerror(-res1));

if (res2 < 0)
mx_log_err("setting status of job %lu to CANCELLED failed: %s", arg_job_id, strerror(-res2));

if (res2 > 0) {
mx_log_notice("Job %lu cancelled!", arg_job_id);
return 0;
}

if (res1 > 0) {
mx_log_notice("Updated status of job %lu to CANCELLING.", arg_job_id);
if (res2 == 0) {
mx_log_warning("Updating status of job %lu to CANCELLED failed. Job vanished. Please retry.", arg_job_id);
return 2;
}
return 1;
}

if (res1 == 0 && res2 == 0) {
mx_log_notice("No queued job with job_id=%lu for user %s(%d) found in q.", arg_job_id, passwd->pw_name, passwd->pw_uid);
mx_log_warning("Killing a single job is not implemented yet.");
mx_log_warning("See https://github.molgen.mpg.de/mariux64/mxq/issues/4 for more details.");
return 0;
}

return 1;
cancel_job(mysql, arg_job_id, passwd->pw_uid);
return 0;
}

if (arg_group_id) {
Expand Down

0 comments on commit 0e29847

Please sign in to comment.