brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From drigodwin <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #822: New periodic and scheduled effector polic...
Date Thu, 14 Sep 2017 13:17:50 GMT
Github user drigodwin commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/822#discussion_r138886787
  
    --- Diff: policy/src/main/java/org/apache/brooklyn/policy/action/PeriodicEffectorPolicy.java
---
    @@ -0,0 +1,151 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *      http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.policy.action;
    +
    +import java.util.Map;
    +import java.util.concurrent.TimeUnit;
    +import java.util.concurrent.atomic.AtomicBoolean;
    +
    +import org.apache.brooklyn.api.effector.Effector;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.policy.Policy;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.entity.trait.Startable;
    +import org.apache.brooklyn.core.sensor.Sensors;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.time.Duration;
    +import org.apache.brooklyn.util.time.DurationPredicates;
    +import org.apache.brooklyn.util.time.Time;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.annotations.Beta;
    +import com.google.common.base.Preconditions;
    +import com.google.common.reflect.TypeToken;
    +
    +/**
    + * A {@link Policy} that executes an {@link Effector} at specific intervals.
    + * <p>
    + * The following example shows a pair of policies that resize a cluster
    + * from one to ten entities during the day  and back to one at night,:
    + * <pre>{@code
    + * brooklyn.policies:
    + *   - type: org.apache.brooklyn.policy.action.ScheduledEffectorPolicy
    + *     brooklyn.config:
    + *       effector: resize
    + *       args:
    + *         desiredSize: 10
    + *       period: 1 day
    + *       time: 08:00:00
    + *   - type: org.apache.brooklyn.policy.action.ScheduledEffectorPolicy
    + *     brooklyn.config:
    + *       effector: resize
    + *       args:
    + *         desiredSize: 1
    + *       period: 1 day
    + *       time: 18:00:00
    + * }</pre>
    + */
    +@Beta
    +public class PeriodicEffectorPolicy extends AbstractScheduledEffectorPolicy {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PeriodicEffectorPolicy.class);
    +
    +    public static final ConfigKey<Boolean> RUNNING = ConfigKeys.builder(Boolean.class)
    +            .name("running")
    +            .description("Set if the executor has started")
    +            .defaultValue(Boolean.FALSE)
    +            .build();
    +
    +    public static final ConfigKey<Duration> PERIOD = ConfigKeys.builder(Duration.class)
    +            .name("period")
    +            .description("The duration between executions of this policy")
    +            .constraint(DurationPredicates.positive())
    +            .defaultValue(Duration.hours(1))
    +            .build();
    +
    +    public static final ConfigKey<AttributeSensor<Boolean>> START_SENSOR
= ConfigKeys.builder(new TypeToken<AttributeSensor<Boolean>>() { })
    +            .name("start.sensor")
    +            .description("The sensor which should trigger starting the periodic execution
scheduler")
    +            .defaultValue(Startable.SERVICE_UP)
    +            .build();
    +
    +    protected AtomicBoolean running = new AtomicBoolean(false);
    +
    +    public PeriodicEffectorPolicy() {
    +        this(MutableMap.<String,Object>of());
    +    }
    +
    +    public PeriodicEffectorPolicy(Map<String,?> props) {
    +        super(props);
    +    }
    +
    +    @Override
    +    public void setEntity(final EntityLocal entity) {
    +        super.setEntity(entity);
    +
    +        AttributeSensor<Boolean> sensor = config().get(START_SENSOR);
    +        subscriptions().subscribe(entity, sensor, handler);
    +    }
    +
    +    @Override
    +    public void rebind() {
    +        // XXX Will the AtomicBoolean field be saved during rebind?
    --- End diff --
    
    Probably resolve this question before merging...


---

Mime
View raw message