96 lines
2.6 KiB
C
96 lines
2.6 KiB
C
#ifndef VIETER_JOB_QUEUE
|
|
#define VIETER_JOB_QUEUE
|
|
|
|
#include "vieter_cron.h"
|
|
#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 {
|
|
vieter_job_state_queued = 0,
|
|
vieter_job_state_ready = 1,
|
|
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
|
|
#define VIETER_JOB_STATES 4
|
|
|
|
/*
|
|
* 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 {
|
|
uint64_t id;
|
|
uint64_t next_scheduled_time;
|
|
vieter_cron_expression *schedule;
|
|
void *build_config;
|
|
vieter_job_failure_report *failure_report;
|
|
uint64_t state_transition_times[VIETER_JOB_STATES];
|
|
vieter_job_state current_state;
|
|
bool single;
|
|
bool dispatched;
|
|
} vieter_job;
|
|
|
|
/*
|
|
* 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.
|
|
*/
|
|
typedef struct vieter_job_queue vieter_job_queue;
|
|
|
|
typedef enum vieter_job_queue_error {
|
|
vieter_job_queue_ok = 0,
|
|
vieter_job_queue_not_found = 1
|
|
} vieter_job_queue_error;
|
|
|
|
/*
|
|
* Allocate and initialize a new job queue.
|
|
*/
|
|
vieter_job_queue *vieter_job_queue_init();
|
|
|
|
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 *job);
|
|
|
|
/*
|
|
* Dispatch the job with the given id, returning the pointer to the job.
|
|
* Dispatching a job removes it from its respective state's queue.
|
|
*/
|
|
vieter_job_queue_error vieter_job_queue_dispatch(vieter_job **out, uint64_t id);
|
|
|
|
/*
|
|
* 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.
|
|
*/
|
|
vieter_job_queue_error vieter_job_queue_transition(uint64_t id,
|
|
vieter_job_state new_state);
|
|
|
|
#endif
|