tstep.c 2.7 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define _DEFAULT_SOURCE
S
log  
Shengliang Guan 已提交
17
#include "tlog.h"
S
Shengliang Guan 已提交
18
#include "taoserror.h"
S
Shengliang Guan 已提交
19 20
#include "tstep.h"

S
Shengliang Guan 已提交
21
typedef struct {
22 23 24 25 26 27
  char *    name;
  InitFp    initFp;
  CleanupFp cleanupFp;
} SStep;

typedef struct SSteps {
28 29 30
  int32_t  cursize;
  int32_t  maxsize;
  SStep *  steps;
31 32 33 34
  ReportFp reportFp;
} SSteps;

SSteps *taosStepInit(int32_t maxsize, ReportFp fp) {
S
Shengliang Guan 已提交
35
  SSteps *steps = calloc(1, sizeof(SSteps));
S
Shengliang Guan 已提交
36 37 38 39
  if (steps == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
S
Shengliang Guan 已提交
40 41 42

  steps->maxsize = maxsize;
  steps->cursize = 0;
43 44
  steps->steps = calloc(maxsize, sizeof(SStep));
  steps->reportFp = fp;
S
Shengliang Guan 已提交
45 46 47 48

  return steps;
}

49
int32_t taosStepAdd(struct SSteps *steps, char *name, InitFp initFp, CleanupFp cleanupFp) {
S
Shengliang Guan 已提交
50 51 52 53 54
  if (steps == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

S
Shengliang Guan 已提交
55 56
  if (steps->cursize >= steps->maxsize) {
    uError("failed to add step since up to the maxsize");
S
Shengliang Guan 已提交
57
    terrno = TSDB_CODE_OUT_OF_RANGE;
S
Shengliang Guan 已提交
58 59 60
    return -1;
  }

61
  SStep step = {.name = name, .initFp = initFp, .cleanupFp = cleanupFp};
62
  steps->steps[steps->cursize++] = step;
S
Shengliang Guan 已提交
63 64 65 66 67
  return 0;
}

static void taosStepCleanupImp(SSteps *steps, int32_t pos) {
  for (int32_t s = pos; s >= 0; s--) {
68
    SStep *step = steps->steps + s;
S
Shengliang Guan 已提交
69 70
    uDebug("step:%s will cleanup", step->name);
    if (step->cleanupFp != NULL) {
71
      (*step->cleanupFp)();
S
Shengliang Guan 已提交
72 73 74 75 76
    }
  }
}

int32_t taosStepExec(SSteps *steps) {
S
Shengliang Guan 已提交
77 78 79 80
  if (steps == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }
S
Shengliang Guan 已提交
81 82

  for (int32_t s = 0; s < steps->cursize; s++) {
83
    SStep *step = steps->steps + s;
S
Shengliang Guan 已提交
84 85
    if (step->initFp == NULL) continue;

86 87
    if (steps->reportFp != NULL) {
      (*steps->reportFp)(step->name, "start initialize");
S
Shengliang Guan 已提交
88 89
    }

90
    int32_t code = (*step->initFp)();
S
Shengliang Guan 已提交
91 92 93 94 95 96 97 98
    if (code != 0) {
      uDebug("step:%s will cleanup", step->name);
      taosStepCleanupImp(steps, s);
      return code;
    }

    uInfo("step:%s is initialized", step->name);

99 100
    if (steps->reportFp != NULL) {
      (*steps->reportFp)(step->name, "initialize completed");
S
Shengliang Guan 已提交
101 102 103 104 105 106 107 108 109 110
    }
  }

  return 0;
}

void taosStepCleanup(SSteps *steps) {
  if (steps == NULL) return;
  taosStepCleanupImp(steps, steps->cursize - 1);
}