Search in sources :

Example 1 with LogTask

use of com.publiccms.entities.log.LogTask in project PublicCMS-preview by sanluan.

the class LogTaskDirective method execute.

@Override
public void execute(RenderHandler handler) throws IOException, Exception {
    Long id = handler.getLong("id");
    SysSite site = getSite(handler);
    if (CommonUtils.notEmpty(id)) {
        LogTask entity = service.getEntity(id);
        if (null != entity && site.getId() == entity.getSiteId()) {
            handler.put("object", entity).render();
        }
    } else {
        Long[] ids = handler.getLongArray("ids");
        if (CommonUtils.notEmpty(ids)) {
            List<LogTask> entityList = service.getEntitys(ids);
            Map<String, LogTask> map = new LinkedHashMap<>();
            for (LogTask entity : entityList) {
                if (site.getId() == entity.getSiteId()) {
                    map.put(String.valueOf(entity.getId()), entity);
                }
            }
            handler.put("map", map).render();
        }
    }
}
Also used : SysSite(com.publiccms.entities.sys.SysSite) LogTask(com.publiccms.entities.log.LogTask) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with LogTask

use of com.publiccms.entities.log.LogTask in project PublicCMS-preview by sanluan.

the class ScheduledJob method executeInternal.

@Override
public void executeInternal(JobExecutionContext context) throws JobExecutionException {
    Integer taskId = (Integer) context.getJobDetail().getJobDataMap().get(ScheduledTask.ID);
    SysTask task = BeanComponent.getSysTaskService().getEntity(taskId);
    if (null != task) {
        if (ScheduledTask.TASK_STATUS_READY == task.getStatus() && BeanComponent.getSysTaskService().updateStatusToRunning(task.getId())) {
            LogTask entity = new LogTask(task.getSiteId(), task.getId(), new Date(), false);
            BeanComponent.getLogTaskService().save(entity);
            boolean success = false;
            String result;
            try {
                success = true;
                Map<String, Object> map = new HashMap<>();
                map.put("task", task);
                SysSite site = BeanComponent.getSiteService().getEntity(task.getSiteId());
                AbstractFreemarkerView.exposeSite(map, site);
                String fulllPath = SiteComponent.getFullFileName(site, task.getFilePath());
                result = FreeMarkerUtils.generateStringByFile(fulllPath, BeanComponent.getTemplateComponent().getTaskConfiguration(), map);
            } catch (IOException | TemplateException e) {
                result = e.getMessage();
            }
            entity.setEndtime(new Date());
            entity.setSuccess(success);
            entity.setResult(result);
            BeanComponent.getLogTaskService().update(entity.getId(), entity, ignoreProperties);
            BeanComponent.getSysTaskService().updateStatus(task.getId(), ScheduledTask.TASK_STATUS_READY);
        }
    } else {
        BeanComponent.getScheduledTask().delete(taskId);
    }
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) SysTask(com.publiccms.entities.sys.SysTask) IOException(java.io.IOException) Date(java.util.Date) SysSite(com.publiccms.entities.sys.SysSite) LogTask(com.publiccms.entities.log.LogTask)

Example 3 with LogTask

use of com.publiccms.entities.log.LogTask in project PublicCMS-preview by sanluan.

the class ScheduledTask method create.

/**
 * 创建任务计划
 *
 * @param site
 * @param id
 * @param cronExpression
 */
public void create(SysSite site, Integer id, String cronExpression) {
    if (CommonUtils.notEmpty(id) && CommonUtils.notEmpty(cronExpression)) {
        Date startTime = CommonUtils.getDate();
        String taskName = getTaskName(id);
        TriggerKey triggerKey = TriggerKey.triggerKey(taskName);
        try {
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(site.getId() % 60 + BLANK_SPACE + cronExpression);
            if (null == trigger) {
                JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class).withIdentity(taskName).build();
                jobDetail.getJobDataMap().put(ID, id);
                trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(scheduleBuilder).startNow().build();
                scheduler.scheduleJob(jobDetail, trigger);
            } else {
                trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).startNow().build();
                scheduler.rescheduleJob(triggerKey, trigger);
            }
        } catch (SchedulerException e) {
            sysTaskService.updateStatus(id, TASK_STATUS_ERROR);
            logTaskService.save(new LogTask(site.getId(), id, startTime, CommonUtils.getDate(), false, e.getMessage()));
        }
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) CronTrigger(org.quartz.CronTrigger) CronScheduleBuilder(org.quartz.CronScheduleBuilder) JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException) Date(java.util.Date) LogTask(com.publiccms.entities.log.LogTask)

Aggregations

LogTask (com.publiccms.entities.log.LogTask)3 SysSite (com.publiccms.entities.sys.SysSite)2 Date (java.util.Date)2 SysTask (com.publiccms.entities.sys.SysTask)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 CronScheduleBuilder (org.quartz.CronScheduleBuilder)1 CronTrigger (org.quartz.CronTrigger)1 JobDetail (org.quartz.JobDetail)1 SchedulerException (org.quartz.SchedulerException)1 TriggerKey (org.quartz.TriggerKey)1