tstep.c 2.6 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
#include "os.h"
S
Shengliang Guan 已提交
18
#include "ulog.h"
S
Shengliang Guan 已提交
19 20
#include "tstep.h"

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
typedef struct SStepObj {
  char *    name;
  void **   self;
  InitFp    initFp;
  CleanupFp cleanupFp;
} SStep;

typedef struct SSteps {
  int32_t cursize;
  int32_t maxsize;
  SStep * steps;
  ReportFp reportFp;
} SSteps;

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

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

  return steps;
}

47 48
int32_t taosStepAdd(struct SSteps *steps, char *name, void **obj, InitFp initFp, CleanupFp cleanupFp) {
  if (steps == NULL) return -1;
S
Shengliang Guan 已提交
49 50 51 52 53
  if (steps->cursize >= steps->maxsize) {
    uError("failed to add step since up to the maxsize");
    return -1;
  }

54 55
  SStep step = {.name = name, .self = obj, .initFp = initFp, .cleanupFp = cleanupFp};
  steps->steps[steps->cursize++] = step;
S
Shengliang Guan 已提交
56 57 58 59 60
  return 0;
}

static void taosStepCleanupImp(SSteps *steps, int32_t pos) {
  for (int32_t s = pos; s >= 0; s--) {
61
    SStep *step = steps->steps + s;
S
Shengliang Guan 已提交
62 63 64 65 66 67 68 69 70 71 72
    uDebug("step:%s will cleanup", step->name);
    if (step->cleanupFp != NULL) {
      (*step->cleanupFp)(step->self);
    }
  }
}

int32_t taosStepExec(SSteps *steps) {
  if (steps == NULL) return -1;

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

76 77
    if (steps->reportFp != NULL) {
      (*steps->reportFp)(step->name, "start initialize");
S
Shengliang Guan 已提交
78 79
    }

80
    int32_t code = (*step->initFp)(step->self);
S
Shengliang Guan 已提交
81 82 83 84 85 86 87 88
    if (code != 0) {
      uDebug("step:%s will cleanup", step->name);
      taosStepCleanupImp(steps, s);
      return code;
    }

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

89 90
    if (steps->reportFp != NULL) {
      (*steps->reportFp)(step->name, "initialize completed");
S
Shengliang Guan 已提交
91 92 93 94 95 96 97 98 99 100
    }
  }

  return 0;
}

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