Example usage for org.joda.time DateTime DateTime

List of usage examples for org.joda.time DateTime DateTime

Introduction

In this page you can find the example usage for org.joda.time DateTime DateTime.

Prototype

public DateTime() 

Source Link

Document

Constructs an instance set to the current system millisecond time using ISOChronology in the default time zone.

Usage

From source file:azkaban.flow.ComposedExecutableFlow.java

License:Apache License

private void callCallbacks(final List<FlowCallback> callbackList, final Status status) {
    if (endTime == null) {
        endTime = new DateTime();
    }/*www  . ja  v  a 2  s.com*/

    for (FlowCallback callback : callbackList) {
        try {
            callback.completed(status);
        } catch (RuntimeException t) {
            // TODO: Figure out how to use the logger to log that a callback threw an exception.
        }
    }
}

From source file:azkaban.flow.FlowUtils.java

License:Apache License

public static Props addCommonFlowProperties(final Props parentProps, final ExecutableFlowBase flow) {
    final Props props = new Props(parentProps);

    props.put(CommonJobProperties.FLOW_ID, flow.getFlowId());
    props.put(CommonJobProperties.EXEC_ID, flow.getExecutionId());
    props.put(CommonJobProperties.PROJECT_ID, flow.getProjectId());
    props.put(CommonJobProperties.PROJECT_NAME, flow.getProjectName());
    props.put(CommonJobProperties.PROJECT_VERSION, flow.getVersion());
    props.put(CommonJobProperties.FLOW_UUID, UUID.randomUUID().toString());
    props.put(CommonJobProperties.PROJECT_LAST_CHANGED_BY, flow.getLastModifiedByUser());
    props.put(CommonJobProperties.PROJECT_LAST_CHANGED_DATE, flow.getLastModifiedTimestamp());
    props.put(CommonJobProperties.SUBMIT_USER, flow.getExecutableFlow().getSubmitUser());

    final DateTime loadTime = new DateTime();

    props.put(CommonJobProperties.FLOW_START_TIMESTAMP, loadTime.toString());
    props.put(CommonJobProperties.FLOW_START_YEAR, loadTime.toString("yyyy"));
    props.put(CommonJobProperties.FLOW_START_MONTH, loadTime.toString("MM"));
    props.put(CommonJobProperties.FLOW_START_DAY, loadTime.toString("dd"));
    props.put(CommonJobProperties.FLOW_START_HOUR, loadTime.toString("HH"));
    props.put(CommonJobProperties.FLOW_START_MINUTE, loadTime.toString("mm"));
    props.put(CommonJobProperties.FLOW_START_SECOND, loadTime.toString("ss"));
    props.put(CommonJobProperties.FLOW_START_MILLISSECOND, loadTime.toString("SSS"));
    props.put(CommonJobProperties.FLOW_START_TIMEZONE, loadTime.toString("ZZZZ"));

    return props;
}

From source file:azkaban.flow.GroupedExecutableFlow.java

License:Apache License

public GroupedExecutableFlow(String id, ExecutableFlow... flows) {
    this.id = id;
    this.flows = flows;
    this.sortedFlows = Arrays.copyOf(this.flows, this.flows.length);
    Arrays.sort(this.sortedFlows, new Comparator<ExecutableFlow>() {
        @Override/*from   www.  j av  a  2 s . com*/
        public int compare(ExecutableFlow o1, ExecutableFlow o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });

    String[] names = new String[flows.length];
    for (int i = 0; i < flows.length; i++) {
        names[i] = flows[i].getName();
    }
    name = StringUtils.join(names, " + ");

    jobState = Status.READY;
    updateState();
    callbacksToCall = new ArrayList<FlowCallback>();

    theGroupCallback = new GroupedFlowCallback();

    switch (jobState) {
    case SUCCEEDED:
    case COMPLETED:
    case FAILED:
        DateTime theStartTime = new DateTime();
        DateTime theEndTime = new DateTime(0);
        for (ExecutableFlow flow : flows) {
            final DateTime subFlowStartTime = flow.getStartTime();
            if (theStartTime.isAfter(subFlowStartTime)) {
                theStartTime = subFlowStartTime;
            }

            final DateTime subFlowEndTime = flow.getEndTime();
            if (subFlowEndTime != null && subFlowEndTime.isAfter(theEndTime)) {
                theEndTime = subFlowEndTime;
            }
        }

        setAndVerifyParentProps();
        startTime = theStartTime;
        endTime = theEndTime;
        break;
    default:
        // Check for Flows that are "RUNNING"
        boolean allRunning = true;
        List<ExecutableFlow> runningFlows = new ArrayList<ExecutableFlow>();
        DateTime thisStartTime = null;

        for (ExecutableFlow flow : flows) {
            if (flow.getStatus() != Status.RUNNING) {
                allRunning = false;

                final DateTime subFlowStartTime = flow.getStartTime();
                if (subFlowStartTime != null && subFlowStartTime.isBefore(thisStartTime)) {
                    thisStartTime = subFlowStartTime;
                }
            } else {
                runningFlows.add(flow);
            }
        }

        if (allRunning) {
            jobState = Status.RUNNING;
        }

        for (ExecutableFlow runningFlow : runningFlows) {
            final DateTime subFlowStartTime = runningFlow.getStartTime();
            if (subFlowStartTime != null && subFlowStartTime.isBefore(thisStartTime)) {
                thisStartTime = subFlowStartTime;
            }
        }
        setAndVerifyParentProps();

        startTime = thisStartTime;
        endTime = null;

        // Make sure everything is initialized before leaking the pointer to "this".
        // This is just installing the callback in an already running flow.
        for (ExecutableFlow runningFlow : runningFlows) {
            runningFlow.execute(parentProps, theGroupCallback);
        }
    }
}

From source file:azkaban.flow.GroupedExecutableFlow.java

License:Apache License

@Override
public void execute(Props parentProps, final FlowCallback callback) {
    if (parentProps == null) {
        parentProps = new Props();
    }/* ww w  .  ja v a 2  s  . c  o m*/

    synchronized (sync) {
        if (this.parentProps == null) {
            this.parentProps = parentProps;
        } else if (jobState != Status.COMPLETED && !this.parentProps.equalsProps(parentProps)) {
            throw new IllegalArgumentException(String.format(
                    "%s.execute() called with multiple differing parentProps objects.  "
                            + "Call reset() before executing again with a different Props object. this.parentProps[%s], parentProps[%s]",
                    getClass().getSimpleName(), this.parentProps, parentProps));
        }

        switch (jobState) {
        case READY:
            jobState = Status.RUNNING;
            callbacksToCall.add(callback);
            break;
        case RUNNING:
            callbacksToCall.add(callback);
            return;
        case COMPLETED:
        case SUCCEEDED:
        case IGNORED:
            callback.completed(Status.SUCCEEDED);
            return;
        case FAILED:
            callback.completed(Status.FAILED);
            return;
        }
    }

    if (startTime == null) {
        startTime = new DateTime();
    }

    for (ExecutableFlow flow : flows) {
        if (jobState != Status.FAILED) {
            try {
                flow.execute(this.parentProps, theGroupCallback);
            } catch (RuntimeException e) {
                final List<FlowCallback> callbacks;
                synchronized (sync) {
                    jobState = Status.FAILED;
                    callbacks = callbacksToCall;
                }

                callCallbacks(callbacks, Status.FAILED);

                throw e;
            }
        }
    }
}

From source file:azkaban.flow.GroupedExecutableFlow.java

License:Apache License

private void callCallbacks(final List<FlowCallback> callbacksList, final Status status) {
    if (endTime == null) {
        endTime = new DateTime();
    }// w ww  .ja  v  a2  s . c  om

    for (FlowCallback callback : callbacksList) {
        try {
            callback.completed(status);
        } catch (RuntimeException t) {
            // TODO: Figure out how to use the logger to log that a callback threw an exception.
        }
    }
}

From source file:azkaban.flow.IndividualJobExecutableFlow.java

License:Apache License

@Override
public void execute(Props parentProps, FlowCallback callback) {
    if (parentProps == null) {
        parentProps = new Props();
    }/*  ww  w .  ja v a2s .c o  m*/

    synchronized (sync) {
        if (this.parentProps == null) {
            this.parentProps = parentProps;
        } else if (jobState != Status.COMPLETED && !this.parentProps.equalsProps(parentProps)) {
            throw new IllegalArgumentException(String.format(
                    "%s.execute() called with multiple differing parentProps objects. "
                            + "Call reset() before executing again with a different Props object. this.parentProps[%s], parentProps[%s]",
                    getClass().getSimpleName(), this.parentProps, parentProps));
        }

        switch (jobState) {
        case READY:
            jobState = Status.RUNNING;
            startTime = new DateTime();
            callbacksToCall.add(callback);
            break;
        case RUNNING:
            callbacksToCall.add(callback);
            return;
        case IGNORED:
            jobState = Status.COMPLETED;
        case COMPLETED:
        case SUCCEEDED:
            callback.completed(Status.SUCCEEDED);
            return;
        case FAILED:
            callback.completed(Status.FAILED);
            return;
        }
    }

    try {
        // Only one thread should ever be able to get to this point because of management of jobState
        // Thus, this should only ever get called once before the job finishes (at which point it could be reset)
        job = jobManager.loadJob(getName(), parentProps, true);
    } catch (Exception e) {
        logger.warn(String.format("Exception thrown while creating job[%s]", getName()), e);
        job = null;
    }

    if (job == null) {
        logger.warn(String.format(
                "Job[%s] doesn't exist, but was supposed to run. Perhaps someone changed the flow?",
                getName()));

        final List<FlowCallback> callbackList;

        synchronized (sync) {
            jobState = Status.FAILED;
            callbackList = callbacksToCall; // Get the reference before leaving the synchronized
        }
        callCallbacks(callbackList, jobState);
        return;
    }

    Thread theThread = new Thread(new Runnable() {
        @Override
        public void run() {
            final List<FlowCallback> callbackList;

            MonitorImpl.getInternalMonitorInterface().jobEvent(job, System.currentTimeMillis(),
                    JobAction.START_WORKFLOW_JOB, JobState.NOP);

            try {
                job.run();
            } catch (Exception e) {
                synchronized (sync) {
                    jobState = Status.FAILED;
                    returnProps = new Props();
                    exceptions.put(getName(), e);
                    callbackList = callbacksToCall; // Get the reference before leaving the synchronized
                }

                MonitorImpl.getInternalMonitorInterface().jobEvent(job, System.currentTimeMillis(),
                        JobAction.END_WORKFLOW_JOB, JobState.FAILED);

                callCallbacks(callbackList, jobState);

                throw new RuntimeException(e);
            }

            synchronized (sync) {
                jobState = Status.SUCCEEDED;
                returnProps = job.getJobGeneratedProperties();
                callbackList = callbacksToCall; // Get the reference before leaving the synchronized
            }

            MonitorImpl.getInternalMonitorInterface().jobEvent(job, System.currentTimeMillis(),
                    JobAction.END_WORKFLOW_JOB, JobState.SUCCESSFUL);
            returnProps.logProperties(String.format("Return props for job[%s]", getName()));

            callCallbacks(callbackList, jobState);
        }
    }, String.format("%s thread-%s", job.getId(), threadCounter.getAndIncrement()));

    Job currJob = job;
    while (true) {
        if (currJob instanceof DelegatingJob) {
            currJob = ((DelegatingJob) currJob).getInnerJob();
        } else {
            break;
        }
    }

    theThread.start();
}

From source file:azkaban.flow.IndividualJobExecutableFlow.java

License:Apache License

private void callCallbacks(final List<FlowCallback> callbackList, final Status status) {
    if (endTime == null) {
        endTime = new DateTime();
    }//from   w  ww. ja  v a  2 s.  c o m

    Thread callbackThread = new Thread(new Runnable() {
        @Override
        public void run() {
            for (FlowCallback callback : callbackList) {
                try {
                    callback.completed(status);
                } catch (RuntimeException t) {
                    logger.error(String.format("Exception thrown while calling callback. job[%s]", getName()),
                            t);
                }
            }
        }
    }, String.format("%s-callback", Thread.currentThread().getName()));

    callbackThread.start();
}

From source file:azkaban.migration.scheduler.Schedule.java

License:Apache License

private DateTime getNextRuntime(long scheduleTime, DateTimeZone timezone, ReadablePeriod period) {
    DateTime now = new DateTime();
    DateTime date = new DateTime(scheduleTime).withZone(timezone);
    int count = 0;
    while (!now.isBefore(date)) {
        if (count > 100000) {
            throw new IllegalStateException("100000 increments of period did not get to present time.");
        }/*from   w w w.  j a  va2  s  .  c o m*/

        if (period == null) {
            break;
        } else {
            date = date.plus(period);
        }

        count += 1;
    }

    return date;
}

From source file:azkaban.scheduler.ScheduledJob.java

License:Apache License

/**
 * Calculates the next runtime by adding the period.
 * /*  w  w w  .  j a  v a 2 s. c  om*/
 * @param scheduledDate
 * @param period
 * @return
 */
private DateTime getNextRuntime(DateTime scheduledDate, ReadablePeriod period) {
    DateTime now = new DateTime();
    DateTime date = new DateTime(scheduledDate);
    int count = 0;
    while (!now.isBefore(date)) {
        if (count > 100000) {
            throw new IllegalStateException("100000 increments of period did not get to present time.");
        }

        if (period == null) {
            break;
        } else {
            date = date.plus(period);
        }

        count += 1;
    }

    return date;
}

From source file:azkaban.utils.PropsUtils.java

License:Apache License

public static Props addCommonFlowProperties(Props parentProps, final ExecutableFlowBase flow) {
    Props props = new Props(parentProps);

    props.put(CommonJobProperties.FLOW_ID, flow.getFlowId());
    props.put(CommonJobProperties.EXEC_ID, flow.getExecutionId());
    props.put(CommonJobProperties.PROJECT_ID, flow.getProjectId());
    props.put(CommonJobProperties.PROJECT_NAME, flow.getProjectName());
    props.put(CommonJobProperties.PROJECT_VERSION, flow.getVersion());
    props.put(CommonJobProperties.FLOW_UUID, UUID.randomUUID().toString());
    props.put(CommonJobProperties.PROJECT_LAST_CHANGED_BY, flow.getLastModifiedByUser());
    props.put(CommonJobProperties.PROJECT_LAST_CHANGED_DATE, flow.getLastModifiedTimestamp());

    DateTime loadTime = new DateTime();

    props.put(CommonJobProperties.FLOW_START_TIMESTAMP, loadTime.toString());
    props.put(CommonJobProperties.FLOW_START_YEAR, loadTime.toString("yyyy"));
    props.put(CommonJobProperties.FLOW_START_MONTH, loadTime.toString("MM"));
    props.put(CommonJobProperties.FLOW_START_DAY, loadTime.toString("dd"));
    props.put(CommonJobProperties.FLOW_START_HOUR, loadTime.toString("HH"));
    props.put(CommonJobProperties.FLOW_START_MINUTE, loadTime.toString("mm"));
    props.put(CommonJobProperties.FLOW_START_SECOND, loadTime.toString("ss"));
    props.put(CommonJobProperties.FLOW_START_MILLISSECOND, loadTime.toString("SSS"));
    props.put(CommonJobProperties.FLOW_START_TIMEZONE, loadTime.toString("ZZZZ"));

    return props;
}