001/* 002 * Stallion Core: A Modern Web Framework 003 * 004 * Copyright (C) 2015 - 2016 Stallion Software LLC. 005 * 006 * This program is free software: you can redistribute it and/or modify it under the terms of the 007 * GNU General Public License as published by the Free Software Foundation, either version 2 of 008 * the License, or (at your option) any later version. This program is distributed in the hope that 009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 011 * License for more details. You should have received a copy of the GNU General Public License 012 * along with this program. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>. 013 * 014 * 015 * 016 */ 017 018package io.stallion.asyncTasks; 019 020import io.stallion.dataAccess.db.DB; 021import io.stallion.services.Log; 022import io.stallion.settings.Settings; 023import io.stallion.utils.GeneralUtils; 024 025import static io.stallion.utils.Literals.or; 026 027 028public class AsyncDbCoordinator extends AsyncFileCoordinator { 029 030 @Override 031 public void updateTask(AsyncTask task, boolean executeAtChanged) { 032 AsyncTaskController.instance().save(task); 033 } 034 035 @Override 036 public void saveNewTask(AsyncTask task) { 037 Log.info("Adding task to the database: id={0} handler={1} customKey={2}", task.getId(), task.getHandlerName(), task.getCustomKey()); 038 039 // If we are running in localMode, we only want to run tasks created in localMode, we don't want to run tasks created 040 // on production on our local machine, or tasks created on our local machine on production 041 String localMode = ""; 042 if (Settings.instance().getLocalMode()) { 043 task.setLocalMode(or(System.getenv("USER"), GeneralUtils.slugify(Settings.instance().getTargetFolder()))); 044 } 045 AsyncTaskController.instance().save(task); 046 } 047 048 @Override 049 public AsyncTask findAndLockNextTask(Long now) { 050 return ((AsyncTaskDbPersister)getTaskPersister()).findAndLockNextTask(now); 051 } 052 053 054 @Override 055 public boolean markCompleted(AsyncTask task) { 056 return getTaskPersister().markComplete(task); 057 } 058 059 @Override 060 public boolean markFailed(AsyncTask task, Throwable throwable) { 061 062 return getTaskPersister().markFailed(task, throwable); 063 } 064 065 @Override 066 public boolean hasTaskWithId(Long taskId) { 067 AsyncTask task = DB.instance().fetchOne(AsyncTask.class, taskId); 068 if (task == null) { 069 return false; 070 } 071 if (task.getCompletedAt() > 0) { 072 return false; 073 } 074 return true; 075 } 076}