private void save() { try (Connection con = DatabaseFactory.getInstance().getConnection(); PreparedStatement ps = con.prepareStatement( "INSERT INTO `buylists`(`buylist_id`, `item_id`, `count`, `next_restock_time`) VALUES(?, ?, ?, ?) ON DUPLICATE KEY UPDATE `count` = ?, `next_restock_time` = ?")) { ps.setInt(1, getBuyListId()); ps.setInt(2, getItemId()); ps.setLong(3, getCount()); ps.setLong(5, getCount()); if ((_restockTask != null) && (_restockTask.getDelay(TimeUnit.MILLISECONDS) > 0)) { final long nextRestockTime = System.currentTimeMillis() + _restockTask.getDelay(TimeUnit.MILLISECONDS); ps.setLong(4, nextRestockTime); ps.setLong(6, nextRestockTime); } else { ps.setLong(4, 0); ps.setLong(6, 0); } ps.executeUpdate(); } catch (Exception e) { _log.log( Level.WARNING, "Failed to save Product buylist_id:" + getBuyListId() + " item_id:" + getItemId(), e); } }
public final void addPacket(EffectInfoPacketList list) { if (!_inUse || !getShowIcon()) return; switch (_state) { case CREATED: case FINISHING: return; } switch (_skill.getId()) { case 2031: case 2032: case 2037: return; } switch (getEffectType()) { case SIGNET_GROUND: return; } final EffectTask task = _currentTask; final ScheduledFuture<?> future = _currentFuture; if (task == null || future == null) return; int time; if (task._rate > 0) time = getRemainingTaskTime() * 1000; else time = (int) future.getDelay(TimeUnit.MILLISECONDS); time = (time < 0 ? -1 : time / 1000); list.addEffect(_template.iconId, _skill.getLevel(), time); }
public void pause() { if (future == null) { throw new IllegalStateException("Reminder was not started"); } elapsed = future.getDelay(TimeUnit.SECONDS); future.cancel(true); future = null; }
public long getElapsed() { if (elapsed > 0) { return elapsed; } if (future != null) { return future.getDelay(TimeUnit.SECONDS); } return 0; }
private void sideboard(UUID playerId, Deck deck) throws MageException { for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) { if (entry.getValue().equals(playerId)) { User user = UserManager.getInstance().getUser(entry.getKey()); int remaining = (int) futureTimeout.getDelay(TimeUnit.SECONDS); if (user != null) { user.ccSideboard(deck, table.getId(), remaining, options.isLimited()); } break; } } }
public void run() { if (_log.shouldLog(Log.DEBUG)) _log.debug("Running: " + this); long before = System.currentTimeMillis(); long delay = 0; synchronized (this) { if (_rescheduleAfterRun) throw new IllegalStateException("rescheduleAfterRun cannot be true here"); switch (_state) { case CANCELLED: return; // goodbye case IDLE: // fall through case RUNNING: throw new IllegalStateException("not possible to be in " + _state); case SCHEDULED: // proceed, switch to IDLE in case I need to reschedule _state = TimedEventState.IDLE; } // if I was rescheduled by the user, re-submit myself to the executor. int difference = (int) (_nextRun - before); // careful with long uptimes if (difference > _fuzz) { schedule(difference); return; } // else proceed to run _state = TimedEventState.RUNNING; } // cancel()-ing after this point only works if the event supports it explicitly // none of these _future checks should be necessary anymore if (_future != null) delay = _future.getDelay(TimeUnit.MILLISECONDS); else if (_log.shouldLog(Log.WARN)) _log.warn(_pool + " wtf, no _future " + this); // This can be an incorrect warning especially after a schedule(0) if (_log.shouldLog(Log.WARN) && delay > 100) _log.warn(_pool + " wtf, early execution " + delay + ": " + this); else if (_log.shouldLog(Log.WARN) && delay < -1000) _log.warn(" wtf, late execution " + (0 - delay) + ": " + this + _pool.debug()); try { timeReached(); } catch (Throwable t) { _log.log( Log.CRIT, _pool + ": Timed task " + this + " exited unexpectedly, please report", t); } finally { // must be in finally synchronized (this) { switch (_state) { case SCHEDULED: // fall through case IDLE: throw new IllegalStateException("can't be " + _state); case CANCELLED: break; // nothing case RUNNING: _state = TimedEventState.IDLE; // do we need to reschedule? if (_rescheduleAfterRun) { _rescheduleAfterRun = false; schedule(_nextRun - System.currentTimeMillis()); } } } } long time = System.currentTimeMillis() - before; if (time > 500 && _log.shouldLog(Log.WARN)) _log.warn(_pool + " wtf, event execution took " + time + ": " + this); if (_log.shouldLog(Log.INFO)) { // this call is slow - iterates through a HashMap - // would be better to have a local AtomicLong if we care long completed = _pool.getCompletedTaskCount(); if (completed % 250 == 0) _log.info(_pool.debug()); } }
@Override public long getDelay(TimeUnit unit) { return scheduledDelegate.getDelay(unit); }
@Override public String getStatus() { StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); if (executor == null) { out.println("Jobs execution pool:"); out.println("~~~~~~~~~~~~~~~~~~~"); out.println("(not yet started)"); return sw.toString(); } out.println("Jobs execution pool:"); out.println("~~~~~~~~~~~~~~~~~~~"); out.println("Pool size: " + executor.getPoolSize()); out.println("Active count: " + executor.getActiveCount()); out.println("Scheduled task count: " + executor.getTaskCount()); out.println("Queue size: " + executor.getQueue().size()); SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); if (!scheduledJobs.isEmpty()) { out.println(); out.println("Scheduled jobs (" + scheduledJobs.size() + "):"); out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~"); for (Job job : scheduledJobs) { out.print(job.getClass().getName()); if (job.getClass().isAnnotationPresent(OnApplicationStart.class)) { OnApplicationStart appStartAnnotation = job.getClass().getAnnotation(OnApplicationStart.class); out.print( " run at application start" + (appStartAnnotation.async() ? " (async)" : "") + "."); } if (job.getClass().isAnnotationPresent(On.class)) { String cron = job.getClass().getAnnotation(On.class).value(); if (cron != null && cron.startsWith("cron.")) { cron = Play.configuration.getProperty(cron); } out.print(" run with cron expression " + cron + "."); } if (job.getClass().isAnnotationPresent(Every.class)) { out.print(" run every " + job.getClass().getAnnotation(Every.class).value() + "."); } if (job.lastRun > 0) { out.print(" (last run at " + df.format(new Date(job.lastRun))); if (job.wasError) { out.print(" with error)"); } else { out.print(")"); } } else { out.print(" (has never run)"); } out.println(); } } if (!executor.getQueue().isEmpty()) { out.println(); out.println("Waiting jobs:"); out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~"); for (Object o : executor.getQueue()) { ScheduledFuture task = (ScheduledFuture) o; out.println( Java.extractUnderlyingCallable((FutureTask) task) + " will run in " + task.getDelay(TimeUnit.SECONDS) + " seconds"); } } return sw.toString(); }
public int getRemainingTime() { return (int) futureTimeout.getDelay(TimeUnit.SECONDS); }