2 * The Submit Daemon: Tasks
4 * (c) 2007 Martin Mares <mj@ucw.cz>
9 #include "lib/fastbuf.h"
10 #include "lib/stkstring.h"
11 #include "sherlock/object.h"
24 clist_init(&task_list);
28 static struct cf_section task_conf = {
31 CF_STRING("Name", PTR_TO(struct task, name)),
36 struct cf_section tasks_conf = {
37 CF_INIT(tasks_conf_init),
39 CF_LIST("Task", &task_list, &task_conf),
47 CLIST_FOR_EACH(struct task *, t, task_list)
48 if (!strcasecmp(t->name, name))
54 user_exists_p(byte *user)
56 byte *fn = stk_printf("solutions/%s/status", user);
58 return !stat(fn, &st) && S_ISREG(st.st_mode);
62 task_lock_status(struct conn *c)
64 ASSERT(!c->task_lock_fd);
65 if ((c->task_lock_fd = open(stk_printf("solutions/%s/status.lock", c->user), O_RDWR | O_CREAT | O_TRUNC, 0666)) < 0)
66 die("Cannot create task lock: %m");
73 if (fcntl(c->task_lock_fd, F_SETLKW, &fl) < 0)
74 die("Cannot lock status file: %m");
76 struct fastbuf *fb = bopen_try(stk_printf("solutions/%s/status", c->user), O_RDONLY, 4096);
77 c->task_status = obj_new(c->pool);
80 obj_read(fb, c->task_status);
86 task_unlock_status(struct conn *c, uns write_back)
88 ASSERT(c->task_lock_fd);
89 ASSERT(c->task_status);
93 struct fastbuf *fb = bopen_tmp(4096);
94 obj_write(fb, c->task_status, BUCKET_TYPE_PLAIN);
96 bconfig(fb, BCONFIG_IS_TEMP_FILE, 0);
97 byte *name = stk_printf("solutions/%s/status", c->user);
98 if (rename(fb->name, name) < 0)
99 die("Unable to rename %s to %s: %m", fb->name, name);
105 .l_whence = SEEK_SET,
109 if (fcntl(c->task_lock_fd, F_SETLKW, &fl) < 0)
110 die("Cannot unlock status file: %m");
112 c->task_status = NULL;
116 task_status_find_task(struct conn *c, struct task *t)
118 for (struct oattr *a = obj_find_attr(c->task_status, 'T' + OBJ_ATTR_SON); a; a=a->same)
120 struct odes *o = a->son;
121 byte *name = obj_find_aval(o, 'T');
122 if (!strcmp(name, t->name))
125 struct odes *o = obj_add_son(c->task_status, 'T' + OBJ_ATTR_SON);
126 obj_set_attr(o, 'T', t->name);
131 task_submit(struct conn *c, struct task *t, struct fastbuf *fb, byte *filename)
133 byte *dir = stk_printf("solutions/%s/%s", c->user, t->name);
134 byte *name = stk_printf("%s/%s", dir, filename);