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();
}
}
}
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);
}
}
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()));
}
}
}
Aggregations