Search in sources :

Example 1 with JobInfo

use of com.flink.platform.dao.entity.JobInfo in project flink-platform-backend by itinycheng.

the class JobRunner method execute.

@Override
public void execute(JobExecutionContext context) {
    JobDetail detail = context.getJobDetail();
    JobDataMap jobDataMap = detail.getJobDataMap();
    JobKey key = detail.getKey();
    Long jobId = Long.parseLong(key.getName());
    try {
        // Avoid preforming the same job multiple times at the same time.
        Long previous = RUNNER_MAP.putIfAbsent(jobId, System.currentTimeMillis());
        if (previous != null && previous > 0) {
            log.warn("The job: {} is already running, start time: {}", jobId, previous);
            return;
        }
        // Step 1: get job info
        JobInfo jobInfo = jobInfoService.getOne(new QueryWrapper<JobInfo>().lambda().eq(JobInfo::getId, jobId).eq(JobInfo::getStatus, JobStatus.ONLINE));
        if (jobInfo == null) {
            log.warn("The job:{} is no longer exists or not in ready/scheduled status.", jobId);
            return;
        }
        // Step 2: build cluster url, set localhost as default url if not specified.
        String routeUrl = jobInfo.getRouteUrl();
        routeUrl = HttpUtil.getUrlOrDefault(routeUrl);
        // Step 3: send http request.
        Map<String, Object> wrappedMap = jobDataMap.getWrappedMap();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(APPLICATION_JSON);
        String httpUri = routeUrl + String.format(JOB_PROCESS_REST_PATH, jobId);
        ResultInfo<Long> response = restTemplate.exchange(httpUri, HttpMethod.POST, new HttpEntity<>(wrappedMap, headers), new ParameterizedTypeReference<ResultInfo<Long>>() {
        }).getBody();
        log.info("The job: {} is processed, job run result: {}", jobId, response);
    } catch (Exception e) {
        log.error("Cannot exec job: {}", jobId, e);
    } finally {
        RUNNER_MAP.remove(jobId);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) JobDataMap(org.quartz.JobDataMap) HttpEntity(org.springframework.http.HttpEntity) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) JobDetail(org.quartz.JobDetail) JobKey(org.quartz.JobKey) JobInfo(com.flink.platform.dao.entity.JobInfo) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference)

Example 2 with JobInfo

use of com.flink.platform.dao.entity.JobInfo in project flink-platform-backend by itinycheng.

the class JobExecuteThread method call.

@Override
public JobResponse call() {
    Long jobId = jobVertex.getJobId();
    Long jobRunId = jobVertex.getJobRunId();
    try {
        // Step 1: get job info
        JobInfo jobInfo = jobInfoService.getOne(new QueryWrapper<JobInfo>().lambda().eq(JobInfo::getId, jobId).eq(JobInfo::getStatus, JobStatus.ONLINE));
        if (jobInfo == null) {
            log.warn("The job:{} is no longer exists or not in ready/scheduled status.", jobId);
            return new JobResponse(jobId, jobRunId, NOT_EXIST);
        }
        // Step 2: build route url, set localhost as default url if not specified.
        String routeUrl = jobInfo.getRouteUrl();
        routeUrl = HttpUtil.getUrlOrDefault(routeUrl);
        // Step 3: process job and get jobRun.
        JobRunInfo jobRunInfo;
        if (jobRunId != null) {
            jobRunInfo = jobRunInfoService.getById(jobRunId);
            log.info("Job:{} already submitted, runId = {}.", jobId, jobRunId);
        } else {
            jobRunInfo = processRemoteJob(routeUrl, jobId);
        }
        if (jobRunInfo == null) {
            log.warn("The jobRun:{} is no longer exists.", jobRunId);
            return new JobResponse(jobId, jobRunId, NOT_EXIST);
        }
        // Step 4: Update jobRunId in Memory.
        jobRunId = jobRunInfo.getId();
        // Step 5: Wait for job complete and get final status.
        ExecutionStatus status = jobRunInfo.getStatus();
        if (status == null || !status.isTerminalState()) {
            StatusInfo statusInfo = waitForComplete(routeUrl, jobRunInfo);
            if (statusInfo != null) {
                status = statusInfo.getStatus();
                updateJobRunInfo(jobRunId, statusInfo.getStatus(), statusInfo.getEndTime());
            }
        }
        return new JobResponse(jobId, jobRunId, status);
    } catch (Exception e) {
        log.error("Submit job and wait for complete failed.", e);
        updateJobRunInfo(jobRunId, ERROR, LocalDateTime.now());
        return new JobResponse(jobId, jobRunId, ERROR);
    }
}
Also used : JobInfo(com.flink.platform.dao.entity.JobInfo) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ExecutionStatus(com.flink.platform.common.enums.ExecutionStatus) StatusInfo(com.flink.platform.web.monitor.StatusInfo) CustomizeStatusInfo(com.flink.platform.web.monitor.CustomizeStatusInfo) JobRunInfo(com.flink.platform.dao.entity.JobRunInfo)

Example 3 with JobInfo

use of com.flink.platform.dao.entity.JobInfo in project flink-platform-backend by itinycheng.

the class JobInfoController method update.

@PostMapping(value = "/update")
public ResultInfo<JobInfo> update(@RequestBody JobInfoRequest jobInfoRequest) {
    String errorMsg = jobInfoRequest.validateOnUpdate();
    if (StringUtils.isNotBlank(errorMsg)) {
        return failure(ERROR_PARAMETER, errorMsg);
    }
    JobInfo jobInfo = jobInfoRequest.getJobInfo();
    jobInfoService.updateById(jobInfo);
    return ResultInfo.success(jobInfo);
}
Also used : JobInfo(com.flink.platform.dao.entity.JobInfo) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with JobInfo

use of com.flink.platform.dao.entity.JobInfo in project flink-platform-backend by itinycheng.

the class JobInfoController method create.

@PostMapping(value = "/create")
public ResultInfo<JobInfo> create(@RequestBody JobInfoRequest jobInfoRequest) {
    String errorMsg = jobInfoRequest.validateOnCreate();
    if (StringUtils.isNotBlank(errorMsg)) {
        return failure(ERROR_PARAMETER, errorMsg);
    }
    JobInfo jobInfo = jobInfoRequest.getJobInfo();
    jobInfo.setId(null);
    jobInfo.setStatus(JobStatus.ONLINE);
    jobInfoService.save(jobInfo);
    return ResultInfo.success(jobInfo);
}
Also used : JobInfo(com.flink.platform.dao.entity.JobInfo) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 5 with JobInfo

use of com.flink.platform.dao.entity.JobInfo in project flink-platform-backend by itinycheng.

the class UserGroupController method createOrUpdate.

@PostMapping(value = "/createOrUpdate")
public ResultInfo<Long> createOrUpdate(@RequestBody UserGroupRequest userGroupRequest) {
    try {
        ResultInfo<String> sqlInfo = insertSelect(userGroupRequest.getSelect());
        String sql = sqlInfo.getData();
        JobInfo jobInfo = new JobInfo();
        BeanUtils.copyProperties(userGroupRequest, jobInfo);
        jobInfo.setSubject(sql);
        jobInfo.setType(JobType.FLINK_SQL);
        jobInfo.setDeployMode(DeployMode.FLINK_YARN_PER);
        jobInfo.setExecMode(ExecutionMode.BATCH);
        LongArrayList longs = new LongArrayList();
        longs.add(1L);
        jobInfo.setCatalogs(longs);
        jobInfo.setStatus(JobStatus.ONLINE);
        boolean bool = jobInfoService.saveOrUpdate(jobInfo);
        if (bool && userGroupRequest.getId() == null) {
            JobQuartzInfo jobQuartzInfo = new JobQuartzInfo(jobInfo);
            quartzService.runOnce(jobQuartzInfo);
            quartzService.addJobToQuartz(jobQuartzInfo);
        }
        return ResultInfo.success(jobInfo.getId());
    } catch (Exception e) {
        log.error("create or update user group failed", e);
        return ResultInfo.failure(ResponseStatus.SERVICE_ERROR);
    }
}
Also used : JobInfo(com.flink.platform.dao.entity.JobInfo) LongArrayList(com.flink.platform.dao.entity.LongArrayList) JobQuartzInfo(com.flink.platform.web.entity.JobQuartzInfo) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

JobInfo (com.flink.platform.dao.entity.JobInfo)6 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)3 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 ExecutionStatus (com.flink.platform.common.enums.ExecutionStatus)2 JobRunInfo (com.flink.platform.dao.entity.JobRunInfo)2 SUCCESS (com.flink.platform.common.enums.ExecutionStatus.SUCCESS)1 JobStatus (com.flink.platform.common.enums.JobStatus)1 JobType (com.flink.platform.common.enums.JobType)1 JobCommandGenException (com.flink.platform.common.exception.JobCommandGenException)1 JsonUtil (com.flink.platform.common.util.JsonUtil)1 LongArrayList (com.flink.platform.dao.entity.LongArrayList)1 JobInfoService (com.flink.platform.dao.service.JobInfoService)1 JobRunInfoService (com.flink.platform.dao.service.JobRunInfoService)1 CommandBuilder (com.flink.platform.web.command.CommandBuilder)1 CommandExecutor (com.flink.platform.web.command.CommandExecutor)1 FlinkCommand (com.flink.platform.web.command.FlinkCommand)1 JobCallback (com.flink.platform.web.command.JobCallback)1 JobCommand (com.flink.platform.web.command.JobCommand)1 JobQuartzInfo (com.flink.platform.web.entity.JobQuartzInfo)1 SqlVar (com.flink.platform.web.enums.SqlVar)1