libvieter/include/vieter_job_queue.h

128 lines
3.7 KiB
C
Raw Normal View History

#ifndef VIETER_JOB_QUEUE
#define VIETER_JOB_QUEUE
#include "vieter_cron.h"
2023-02-02 16:14:26 +01:00
#include <stdbool.h>
#include <stdint.h>
/*
* The order of these do not imply that they happen in this order. New states
* will just get added as consecutive numbers. Their values should be
* monotonically increasing values, as these will be used to index arrays, among
* other things.
*/
typedef enum vieter_job_state {
2023-02-02 16:14:26 +01:00
vieter_job_state_queued = 0,
vieter_job_state_ready = 1,
2023-02-23 09:47:47 +01:00
vieter_job_state_build_finished = 2,
vieter_job_state_failed = 3
} vieter_job_state;
// This macro should be kept in sync with the above enum
2023-02-23 09:47:47 +01:00
#define VIETER_JOB_STATES 4
#define VIETER_JOB_INITIAL_STATE vieter_job_state_queued
#define VIETER_JOB_FAILURE_STATE vieter_job_state_failed
2023-02-23 09:47:47 +01:00
/*
* Struct storing a report for why a certain job failed to be processed in the
* given state.
*/
typedef struct vieter_job_failure_report {
vieter_job_state failed_state;
char *msg;
} vieter_job_failure_report;
vieter_job_failure_report *vieter_job_failure_report_init();
void vieter_job_failure_report_free(vieter_job_failure_report **ptp);
/*
* Represents a job currently being processed in the system. A job migrates
* between different states before finally being removed from the queue.
*/
typedef struct vieter_job {
2023-02-23 09:47:47 +01:00
uint64_t id;
2023-02-02 16:14:26 +01:00
uint64_t next_scheduled_time;
2023-02-23 09:47:47 +01:00
vieter_cron_expression *schedule;
void *build_config;
vieter_job_failure_report *failure_report;
2023-02-02 16:14:26 +01:00
uint64_t state_transition_times[VIETER_JOB_STATES];
2023-02-23 09:47:47 +01:00
vieter_job_state current_state;
bool single;
2023-02-02 16:14:26 +01:00
bool dispatched;
} vieter_job;
2023-02-23 09:47:47 +01:00
/*
* Allocate a new vieter_job object.
*/
vieter_job *vieter_job_init();
void vieter_job_free(vieter_job **ptp);
/*
* Represents the actual queue managing the list of jobs.
*/
2023-02-02 16:14:26 +01:00
typedef struct vieter_job_queue vieter_job_queue;
2023-02-23 09:47:47 +01:00
typedef enum vieter_job_queue_error {
vieter_job_queue_ok = 0,
vieter_job_queue_not_present = 1,
vieter_job_queue_already_present = 2,
vieter_job_queue_state_empty = 3,
vieter_job_queue_not_dispatched = 4
2023-02-23 09:47:47 +01:00
} vieter_job_queue_error;
/*
* Allocate and initialize a new job queue.
*/
2023-02-02 16:14:26 +01:00
vieter_job_queue *vieter_job_queue_init();
/*
* Free a job queue.
*/
2023-02-23 09:47:47 +01:00
void vieter_job_queue_free(vieter_job_queue **ptp);
/*
* Insert the given job into the system.
*/
vieter_job_queue_error vieter_job_queue_insert(vieter_job_queue *queue,
vieter_job *job);
2023-02-23 09:47:47 +01:00
/*
* Pop a job from the given state's queue. The job will then be marked as
* dispatched.
2023-02-23 09:47:47 +01:00
*/
vieter_job_queue_error vieter_job_queue_pop(vieter_job **out,
vieter_job_queue *queue,
vieter_job_state state);
2023-02-23 09:47:47 +01:00
/*
* Transition the job with the given id to the new state. This sets the
* job's dispatch flag to false, and adds it to the new state's queue.
*
* NOTE: this can only be done with dispatched jobs.
2023-02-23 09:47:47 +01:00
*/
vieter_job_queue_error vieter_job_queue_transition(vieter_job_queue *queue,
uint64_t id,
2023-02-23 09:47:47 +01:00
vieter_job_state new_state);
2023-02-02 16:14:26 +01:00
/*
* Remove the given job from the job queue, returning its pointer to the caller.
*
* NOTE: this can only be done with dispatched jobs.
*/
vieter_job_queue_error
vieter_job_queue_remove(vieter_job **out, vieter_job_queue *queue, uint64_t id);
/*
* Transition a job into the failure state, and attach a failure report with the
* provided message. The message is copied, so the caller is responsible for
* freeing the provided string.
*
* NOTE: this can only be done with dispatched jobs.
*/
vieter_job_queue_error vieter_job_queue_fail(vieter_job_queue *queue,
uint64_t id, char *report_message);
#endif