aboutsummaryrefslogtreecommitdiff
path: root/s3_worker.c
blob: 6482224213e0ccfa58dab4b2ed7fd6c2d4d0e18a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "s3_worker.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ezlive_config.h"
#include "task_queue.h"
#include "s3_client.h"

TaskQueue task_queue;

void exec_s3_task(void *vtask) {
    char obj_name_buf[256];
    S3Task *task = vtask;
    if (task->task_type == kUploadTask) {
        snprintf(obj_name_buf, 255, "%s%s", ezlive_config->s3_path, task->remote_name);
        S3Client_put(task->local_file, obj_name_buf);
        remove(task->local_file);
    } else if (task->task_type == kDeleteTask) {
        snprintf(obj_name_buf, 255, "%s%s", ezlive_config->s3_path, task->remote_name);
        S3Client_delete(obj_name_buf);
    } else if (task->task_type == kClearTask) {
        S3Client_clear();
    } else {
        fprintf(stderr, "unknown task type.\n");
    }
    free(task->local_file);
    free(task->remote_name);
    free(task);
}

void s3_worker_init() {
    S3Client_init();
    TaskQueue_init(&task_queue, 128);
}

void s3_worker_push(S3Task task) {
    S3Task *ptask = malloc(sizeof(S3Task));
    *ptask = task;
    ptask->local_file = ptask->local_file;
    ptask->remote_name = ptask->remote_name;
    TaskQueue_push(&task_queue, exec_s3_task, ptask);
}

void* s3_worker_main(void *ctx) {
    while (1) {
        TaskFn task_fn;
        void *arg;
        TaskQueue_pop(&task_queue, &task_fn, &arg);
        (*task_fn)(arg);
    }
}

S3Task s3_upload_task(const char *local, const char *remote) {
    return (S3Task) {
        .task_type = kUploadTask,
        .local_file = strdup(local),
        .remote_name = strdup(remote),
    };
}

S3Task s3_delete_task(const char *name) {
    return (S3Task) {
        .task_type = kDeleteTask,
        .local_file = NULL,
        .remote_name = strdup(name),
    };
}

S3Task s3_clear_task() {
    return (S3Task) {
        .task_type = kClearTask,
        .local_file = NULL,
        .remote_name = NULL,
    };
}