pass pend reason back to controller

This commit is contained in:
Rongsong Shen 2025-12-23 15:29:26 +08:00
parent cd0bca3138
commit 5560ae91da
16 changed files with 349 additions and 123 deletions

View file

@ -79,7 +79,7 @@ void ordq_fini(ordq_t* q)
mylock_fini(&(q->lock));
}
static ordq_item_t* ordq_get_(ordq_t* q, ordq_skip_fn_t skipfn, va_list ap)
static ordq_item_t* ordq_get_(ordq_t* q, ordq_match_fn_t matchfn, va_list ap)
{
ordq_item_t** p = NULL;
ordq_item_t* it = NULL;
@ -90,15 +90,15 @@ static ordq_item_t* ordq_get_(ordq_t* q, ordq_skip_fn_t skipfn, va_list ap)
va_list ap1;
int rc = 0;
if (skipfn == NULL) {
if (matchfn == NULL) {
break;
}
va_copy(ap1, ap);
rc = skipfn((*p)->data, ap1);
rc = matchfn((*p)->data, ap1);
va_end(ap1);
if (!rc) {
if (rc) {
break;
}
@ -114,41 +114,68 @@ static ordq_item_t* ordq_get_(ordq_t* q, ordq_skip_fn_t skipfn, va_list ap)
return it;
}
ordq_item_t* ordq_get(ordq_t* q, ordq_skip_fn_t skipfn, ...)
ordq_item_t* ordq_get(ordq_t* q, ordq_match_fn_t matchfn, ...)
{
ordq_item_t* it;
va_list ap;
va_start(ap, skipfn);
va_start(ap, matchfn);
mylock_lock(&(q->lock));
it = ordq_get_(q, skipfn, ap);
it = ordq_get_(q, matchfn, ap);
mylock_unlock(&(q->lock));
va_end(ap);
return it;
}
ordq_item_t* ordq_peek(ordq_t* q, ordq_skip_fn_t skipfn, ...)
int ordq_del(ordq_t* q, ordq_match_fn_t matchfn, ...)
{
ordq_item_t* it = NULL;
int count = 0;
va_list ap, ap1;
va_start(ap, matchfn);
va_copy(ap1, ap);
mylock_lock(&(q->lock));
while ((it = ordq_get_(q, matchfn, ap1)) != NULL) {
if (q->killer != NULL) {
q->killer(it->data);
}
free(it);
count++;
va_copy(ap1, ap);
}
mylock_unlock(&(q->lock));
va_end(ap1);
va_end(ap);
return count;
}
ordq_item_t* ordq_peek(ordq_t* q, ordq_match_fn_t matchfn, ...)
{
ordq_item_t* it = NULL;
va_list ap;
va_start(ap, skipfn);
va_start(ap, matchfn);
mylock_lock(&(q->lock));
it = q->head;
while (it != NULL) {
int rc = 0;
va_list ap1;
if (skipfn == NULL) {
if (matchfn == NULL) {
break;
}
va_copy(ap1, ap);
rc = skipfn(it->data, ap1);
rc = matchfn(it->data, ap1);
va_end(ap1);
if (!rc) {
if (rc) {
break;
}

View file

@ -1,4 +1,5 @@
#include <errno.h>
#include <stdlib.h>
#include "preason.h"
#include "utils.h"
@ -149,3 +150,23 @@ int add_job_preason(xce_job_preason_t* reasons, int code, char* host, char* res)
return rc;
}
xce_pendreason_t *make_pendreason(uint64_t jobid) {
xce_pendreason_t *reason = NULL;
xce_job_preason_t *preason = NULL;
preason = (xce_job_preason_t *)calloc(1, sizeof(*preason));
reason = (xce_pendreason_t*)calloc(1, sizeof(*reason));
if ((preason == NULL) || (reason == NULL)) {
FREEUP(reason);
FREEUP(preason);
return NULL;
}
reason->jobid = jobid;
reason->reasons = preason;
return reason;
}

View file

@ -47,14 +47,14 @@ static void test_ordq_get(void)
return;
}
static int test_skip(void* data, va_list ap)
static int test_match(void* data, va_list ap)
{
int val = (int)(long)data;
return (val % 2 != 0);
return (val % 2 == 0);
}
static void test_ordq_get_skip(void)
static void test_ordq_get_match(void)
{
int rc;
ordq_t q;
@ -73,7 +73,7 @@ static void test_ordq_get_skip(void)
j = 0;
nc = 0;
for (i = 0; i < 5; i++) {
it = ordq_get(&q, test_skip);
it = ordq_get(&q, test_match);
if (it == NULL) {
break;
@ -125,14 +125,14 @@ static void test_ordq_peek(void)
return;
}
static int test_skip2(void* data, va_list ap)
static int test_match2(void* data, va_list ap)
{
int val = (int)(long)data;
return (val % 3 != 0);
return (val % 3 == 0);
}
static void test_ordq_peek_skip(void)
static void test_ordq_peek_match(void)
{
int rc;
ordq_t q;
@ -150,7 +150,7 @@ static void test_ordq_peek_skip(void)
for (i = 0; i < 8; i++) {
it = ordq_peek(
&q, test_skip2); /* ordq_peek() will not remove item from q */
&q, test_match2); /* ordq_peek() will not remove item from q */
TEST_ASSERT_TRUE((it != NULL) && ((int)(long)it->data == val[4]));
}
@ -160,12 +160,42 @@ static void test_ordq_peek_skip(void)
return;
}
static void test_ordq_del(void)
{
int rc;
ordq_t q;
ordq_item_t* it;
int keys[8] = { 9, 8, 1, 4, 7, 2, 6, 2 };
int i;
int count;
rc = ordq_init(&q, NULL, (ordq_cmp_fn_t)long_cmp);
TEST_ASSERT_TRUE(rc >= 0);
for (i = 0; i < 8; i++) {
ordq_put(&q, (void*)(long)(keys[i]));
}
count = ordq_del(&q, test_match2);
TEST_ASSERT_TRUE(count == 2);
while ((it = ordq_get(&q, NULL)) != NULL) {
TEST_ASSERT_TRUE((int)(long)it->data % 3 != 0);
free(it);
}
ordq_fini(&q);
return;
}
void test_ordq(void)
{
printf("\n>> ordq test\n\n");
RUN_TEST(test_ordq_get);
RUN_TEST(test_ordq_peek);
RUN_TEST(test_ordq_get_skip);
RUN_TEST(test_ordq_peek_skip);
RUN_TEST(test_ordq_get_match);
RUN_TEST(test_ordq_peek_match);
RUN_TEST(test_ordq_del);
}