Search in sources :

Example 6 with RejectedTaskReason

use of com.serotonin.timer.RejectedTaskReason in project ma-modules-public by infiniteautomation.

the class MangoTaskTemporaryResourceManager method scheduleTask.

private void scheduleTask(TemporaryResource<T, AbstractRestV2Exception> resource) {
    TaskData tasks = (TaskData) resource.getData();
    // TODO Mango 3.4 keep user inside the resource isntead of user id?
    // maybe change the user inside DataPointRestController bulk operation lambda function to get user from background context
    User user = UserDao.instance.get(resource.getUserId());
    if (user == null) {
        AccessDeniedException error = new AccessDeniedException();
        resource.safeError(error);
        return;
    }
    tasks.mainTask = new HighPriorityTask("Temporary resource " + resource.getResourceType() + " " + resource.getId()) {

        @Override
        public void run(long runtime) {
            try {
                BackgroundContext.set(user);
                resource.getTask().run(resource);
            } catch (Exception e) {
                AbstractRestV2Exception error = MangoTaskTemporaryResourceManager.this.mapException(e);
                resource.safeError(error);
            } finally {
                BackgroundContext.remove();
            }
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
            super.rejected(reason);
            TranslatableMessage msg = null;
            switch(reason.getCode()) {
                case RejectedTaskReason.POOL_FULL:
                    msg = new TranslatableMessage("rest.error.rejectedTaskPoolFull");
                    break;
                case RejectedTaskReason.TASK_QUEUE_FULL:
                    msg = new TranslatableMessage("rest.error.rejectedTaskQueueFull");
                    break;
                case RejectedTaskReason.CURRENTLY_RUNNING:
                    msg = new TranslatableMessage("rest.error.rejectedTaskAlreadyRunning");
                    break;
            }
            ServerErrorException ex = msg == null ? new ServerErrorException() : new ServerErrorException(msg);
            AbstractRestV2Exception error = MangoTaskTemporaryResourceManager.this.mapException(ex);
            resource.safeError(error);
        }
    };
    Common.backgroundProcessing.execute(tasks.mainTask);
    this.scheduleTimeout(resource);
}
Also used : AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) HighPriorityTask(com.serotonin.m2m2.util.timeout.HighPriorityTask) User(com.serotonin.m2m2.vo.User) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason)

Example 7 with RejectedTaskReason

use of com.serotonin.timer.RejectedTaskReason in project ma-core-public by infiniteautomation.

the class FixedRateTest method main.

public static void main(String[] args) {
    RealTimeTimer timer = new RealTimeTimer();
    timer.setTimeSource(new TimeSource() {

        int count = 10;

        @Override
        public long currentTimeMillis() {
            long time;
            if (count-- > 0)
                time = System.currentTimeMillis();
            else
                time = System.currentTimeMillis() - 1000001000;
            System.out.println("Returning " + new Date(time));
            return time;
        }
    });
    timer.init();
    long period = 5000;
    long delay = period - (System.currentTimeMillis() % period);
    FixedRateTrigger trigger = new FixedRateTrigger(delay, period);
    TimerTask task = new TimerTask(trigger, "Test Timer Task") {

        @Override
        public void run(long runtime) {
            System.out.println("executed at " + new Date(runtime));
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
            System.out.println("task rejected: " + reason.getDescription());
        }
    };
    timer.schedule(task);
    try {
        synchronized (timer) {
            timer.wait(30000);
        }
    } catch (InterruptedException e) {
    // no op
    }
    timer.cancel();
}
Also used : FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) TimerTask(com.serotonin.timer.TimerTask) RealTimeTimer(com.serotonin.timer.RealTimeTimer) TimeSource(com.serotonin.timer.TimeSource) Date(java.util.Date) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason)

Example 8 with RejectedTaskReason

use of com.serotonin.timer.RejectedTaskReason in project ma-core-public by infiniteautomation.

the class OrderedRealTimeTimerTest method main.

public static void main(String[] args) {
    OrderedRealTimeTimer timer = new OrderedRealTimeTimer();
    ThreadPoolExecutor executor = new OrderedThreadPoolExecutor(0, 100, 30L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), false, timer.getTimeSource());
    timer.init(executor);
    FixedRateTrigger trigger = new FixedRateTrigger(0, 5);
    TimerTask task = new TimerTask(trigger, "task1", "1", 5) {

        private int count = 0;

        @Override
        public void run(long runtime) {
            count++;
            System.out.println("Run: " + count);
            System.out.println("Run at:" + runtime);
            System.out.println("");
            try {
                Thread.sleep(4);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
            System.out.println("Rejected");
        }
    };
    try {
        Thread.sleep(100);
        timer.schedule(task);
        Thread.sleep(1000000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : OrderedRealTimeTimer(com.serotonin.timer.OrderedRealTimeTimer) FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) TimerTask(com.serotonin.timer.TimerTask) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) OrderedThreadPoolExecutor(com.serotonin.timer.OrderedThreadPoolExecutor) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason)

Aggregations

RejectedTaskReason (com.serotonin.timer.RejectedTaskReason)8 TimeoutClient (com.serotonin.m2m2.util.timeout.TimeoutClient)3 TimeoutTask (com.serotonin.m2m2.util.timeout.TimeoutTask)3 FixedRateTrigger (com.serotonin.timer.FixedRateTrigger)3 OrderedThreadPoolExecutor (com.serotonin.timer.OrderedThreadPoolExecutor)3 Date (java.util.Date)3 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 MangoThreadFactory (com.serotonin.m2m2.rt.maint.MangoThreadFactory)2 OrderedRealTimeTimer (com.serotonin.timer.OrderedRealTimeTimer)2 RealTimeTimer (com.serotonin.timer.RealTimeTimer)2 TaskWrapper (com.serotonin.timer.TaskWrapper)2 TimerTask (com.serotonin.timer.TimerTask)2 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)2 IntegerMonitor (com.infiniteautomation.mango.monitor.IntegerMonitor)1 ValueMonitorOwner (com.infiniteautomation.mango.monitor.ValueMonitorOwner)1 AbstractRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception)1 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)1 ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)1 HighPriorityTask (com.serotonin.m2m2.util.timeout.HighPriorityTask)1