Skip to content

Commit

Permalink
#1379 TimeAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 22, 2022
1 parent 758f32c commit 6062592
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/rultor/agents/Agents.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ public Agent agent(final Talk talk, final Profile profile)
new DismountDaemon(TimeUnit.DAYS.toMinutes(5L)),
new DropsDaemon(TimeUnit.DAYS.toMinutes(1L)),
new MkdirDaemon(),
new StartsDaemon(profile),
new TimedAgent(new StartsDaemon(profile)),
new KillsDaemon(TimeUnit.HOURS.toMinutes(2L)),
new StopsDaemon(),
new EndsDaemon(),
new TimedAgent(new StopsDaemon()),
new TimedAgent(new EndsDaemon()),
new EndsRequest(),
new SafeAgent(
new Tweets(
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/com/rultor/agents/TimedAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2009-2022 Yegor Bugayenko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rultor.agents;

import com.jcabi.log.Logger;
import com.rultor.spi.Agent;
import com.rultor.spi.Talk;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
* Aggent that tracks time and complains if too slow.
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 1.59
*/
public final class TimedAgent implements Agent {

/**
* Limit in seconds.
*/
private static final long LIMIT = TimeUnit.SECONDS.toMillis(10L);

/**
* Agent.
*/
private final transient Agent origin;

/**
* Ctor.
* @param agent Original agent
*/
public TimedAgent(final Agent agent) {
this.origin = agent;
}

@Override
public void execute(final Talk talk) throws IOException {
final long start = System.currentTimeMillis();
this.origin.execute(talk);
final long msec = System.currentTimeMillis() - start;
if (msec > TimedAgent.LIMIT) {
Logger.error(
this, "%s#execute() took %[ms]s, it's too long!",
this.origin.getClass().getCanonicalName(),
msec
);
}
}
}

0 comments on commit 6062592

Please sign in to comment.