码迷,mamicode.com
首页 > 其他好文 > 详细

给原有界面添加一个停止训练功能

时间:2018-12-07 20:55:30      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:sel   @param   char   span   response   servlet   ddt   produces   xtend   

在界面增加一个“停止训练”按钮

<td  class="center">
  <a href=‘/trainlog/setlist?eq_id=${entity.setId}&eq_orgId=${entity.orgId}‘>查看日志</a>
  <a  class="btn btn-minier" href="#" onclick="stop(‘${entity.id}‘)"  >停止训练</a>
</td>

点击“停止训练”,进到stop方法里,传递id作为参数

function stop(id){
                     $.ajax({
                         type: "POST",
                         url: ‘/traintask/stoptask‘,
                         data:   "id="+id,
                         async: false,
                         success: function(msg)
                         {
                            layer.msg(msg.msg, {
                                  icon: 1,
                                  time: 1000 //2秒关闭(如果不配置,默认是3秒)
                                }, function(){
                                    $("#listForm").submit();
                                });   
                         } 
                    });
                }
在对应路径下建方法 url: ‘/traintask/stoptask‘
@Controller
@RequestMapping("/traintask")
public class TrainTaskController extends BaseController{
    @Autowired
    private TrainTaskService trainTaskService;
 
  /**
     * 停止训练任务
     * @param id
     * @param request
     * @return
     */
    @ResponseBody 
    @RequestMapping(value="/stoptask",produces = "application/json; charset=utf-8",method={RequestMethod.POST,RequestMethod.GET})
    public Object stopTask(String id,HttpServletRequest request){   
        int flag = trainTaskService.stopTask(id);
        return  genRetCommonMsg(flag,0,request);
    }
}

controller建立stopTask方法实现停止训练,将方法放进service里,便于其他controller调用

public interface TrainTaskService extends BaseService<TrainTask,TrainTaskFilter> {
        ...
    public int stopTask (String id);
}

方法写进实现类里

@Service
public class TrainTaskServiceImpl extends BaseServiceImpl<TrainTask,TrainTaskFilter> implements TrainTaskService {
    @Autowired
    TagTypeService tagTypeService;
    @Autowired
    TrainLogService trainLogService;
    @Autowired
    TrainTaskService trainTaskService;


...

    //重构后清空日志
                TrainLogFilter filter = new TrainLogFilter();
                filter.setEq_setId(setId);
                List<TrainLog> trainLogs = trainLogService.selectAll(filter);
                for(TrainLog trainLog : trainLogs){
                    trainLog.setDelFlag(0);
                    trainLogService.save(trainLog);
                }

...
       @Transactional
       public int stopTask(String id){
           TrainTask task = trainTaskService.getById(id);
           int flag=0;
           //查看是否处于训练中,
           //处于训练中,停掉客户端,clientState记为1,训练停止后开启客户端
           //未训练中,clientState仍为0,不需停止客户端
           int clientState =0;//      
           String clientId="";

           //停掉客户端
           if(task != null && task.getDelFlag() != 0 && task.getStates() == 1){
               clientState=1;
               TrainClientFilter filter = new TrainClientFilter();
               filter.setLk_ip(task.getTrainNum().split("-")[0]);
               filter.setEq_number(task.getTrainNum().split("-")[1]);
               filter.setEq_delFlag(1);
               TrainClient client = trainClientService.selectAll(filter).get(0);
               clientId = client.getId();
               
               trainClientService.addtask(clientId, "shutdown");
           }           
           //TagType表取到setid后,改completestates为0
           String setId = task.getSetId();
           TagType set = tagTypeService.getById(setId);
           if(set != null && set.getCompleteStates() != 0){
               set.setCompleteStates(0);                   
           }else {
            System.out.println("已是停止训练状态");
           }
           task.setDelFlag(0);
       
           tagTypeService.save(set);
           trainTaskService.save(task);

           if(set.getCompleteStates()==0 && task.getDelFlag()==0){
               flag=1;
           }
           
           return flag;
       }
}

还有重构时再加上清空日志

给原有界面添加一个停止训练功能

标签:sel   @param   char   span   response   servlet   ddt   produces   xtend   

原文地址:https://www.cnblogs.com/lely/p/10084466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!