Search in sources :

Example 1 with ScheduledTask

use of com.zimbra.cs.mailbox.ScheduledTask in project zm-mailbox by Zimbra.

the class TestScheduledTaskManager method testTaskProperties.

@Test
public void testTaskProperties() throws Exception {
    checkNumPersistedTasks(0);
    // Schedule a single-execution task
    ScheduledTask task = new TestTask();
    long now = System.currentTimeMillis();
    task.setExecTime(new Date(now + Constants.MILLIS_PER_MINUTE));
    task.setMailboxId(mbox.getId());
    task.setProperty("prop1", "value1");
    task.setProperty("prop2", "value2");
    task.setProperty("prop3", null);
    ScheduledTaskManager.schedule(task);
    // Make sure the task is persisted
    checkNumPersistedTasks(1);
    // Check properties
    List<ScheduledTask> tasks = DbScheduledTask.getTasks(TestTask.class.getName(), mbox.getId());
    assertEquals(1, tasks.size());
    task = tasks.get(0);
    assertEquals("value1", task.getProperty("prop1"));
    assertEquals("value2", task.getProperty("prop2"));
    assertEquals(null, task.getProperty("prop3"));
    // Cancel task
    ScheduledTaskManager.cancel(TestTask.class.getName(), task.getName(), mbox.getId(), true);
    checkNumPersistedTasks(0);
}
Also used : DbScheduledTask(com.zimbra.cs.db.DbScheduledTask) ScheduledTask(com.zimbra.cs.mailbox.ScheduledTask) Date(java.util.Date) Test(org.junit.Test)

Example 2 with ScheduledTask

use of com.zimbra.cs.mailbox.ScheduledTask in project zm-mailbox by Zimbra.

the class DbScheduledTask method getTasks.

/**
     * Retrieves scheduled tasks from the database.
     *
     * @param className the <tt>ScheduledTask</tt> class name, or <tt>null</tt>
     * for all classes
     * @param mailboxId the mailbox ID, or <tt>0</tt> for all mailboxes
     */
public static List<ScheduledTask> getTasks(String className, int mailboxId) throws ServiceException {
    ZimbraLog.scheduler.debug("Retrieving tasks for class %s, mailbox %d", className, mailboxId);
    List<ScheduledTask> tasks = new ArrayList<ScheduledTask>();
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = DbPool.getConnection();
        String sql = "SELECT class_name, name, mailbox_id, exec_time, interval_millis, metadata " + "FROM " + TABLE_SCHEDULED_TASK;
        if (className != null) {
            sql += " WHERE class_name = ?";
        }
        if (mailboxId > 0) {
            if (className == null) {
                sql += " WHERE mailbox_id = ?";
            } else {
                sql += " AND mailbox_id = ?";
            }
        }
        stmt = conn.prepareStatement(sql);
        int i = 1;
        if (className != null) {
            stmt.setString(i++, className);
        }
        if (mailboxId > 0) {
            stmt.setInt(i++, mailboxId);
        }
        rs = stmt.executeQuery();
        while (rs.next()) {
            className = rs.getString("class_name");
            String name = rs.getString("name");
            ScheduledTask task = null;
            // Instantiate task
            try {
                Object obj = Class.forName(className).newInstance();
                if (obj instanceof ScheduledTask) {
                    task = (ScheduledTask) obj;
                } else {
                    ZimbraLog.scheduler.warn("Class %s is not an instance of ScheduledTask for task %s", className, name);
                    continue;
                }
            } catch (Exception e) {
                ZimbraLog.scheduler.warn("Unable to instantiate class %s for task %s.  " + "Class must be an instance of %s and have a constructor with no arguments.", className, name, ScheduledTask.class.getSimpleName(), e);
                continue;
            }
            // Set member vars
            task.setMailboxId(rs.getInt("mailbox_id"));
            task.setExecTime(DbUtil.timestampToDate(rs.getTimestamp("exec_time")));
            task.setIntervalMillis(rs.getLong("interval_millis"));
            try {
                setProperties(task, rs.getString("metadata"));
            } catch (ServiceException e) {
                ZimbraLog.scheduler.warn("Unable to read metadata for %s.  Not scheduling this task.", task, e);
                continue;
            }
            tasks.add(task);
        }
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to get all DataSourceTasks", e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
    ZimbraLog.scheduler.info("Loaded %d scheduled data source tasks", tasks.size());
    return tasks;
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ScheduledTask(com.zimbra.cs.mailbox.ScheduledTask) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) ServiceException(com.zimbra.common.service.ServiceException) SQLException(java.sql.SQLException)

Example 3 with ScheduledTask

use of com.zimbra.cs.mailbox.ScheduledTask in project zm-mailbox by Zimbra.

the class ShareExpirationListener method scheduleExpireAccessOpIfReq.

static void scheduleExpireAccessOpIfReq(MailItem item) {
    // first cancel any existing task for this item
    try {
        ExpireGrantsTask.cancel(item.getMailboxId(), item.getId());
    } catch (ServiceException e) {
        ZimbraLog.scheduler.warn("Error in canceling existing ExpireGrantsTask for (id=%s,mailboxId=%s)", item.getId(), item.getMailboxId(), e);
        return;
    }
    ACL acl = item.getACL();
    if (acl == null) {
        return;
    }
    // 0 indicates never expires
    long nextExpiry = 0;
    for (ACL.Grant grant : acl.getGrants()) {
        long expiry = grant.getEffectiveExpiry(acl);
        if (expiry != 0) {
            nextExpiry = nextExpiry == 0 ? expiry : expiry < nextExpiry ? expiry : nextExpiry;
        }
    }
    if (nextExpiry == 0) {
        // there isn't any grant that's going to expire
        return;
    }
    ScheduledTask task = new ExpireGrantsTask();
    task.setMailboxId(item.getMailboxId());
    // schedule after a minute of the next expiry time
    task.setExecTime(new Date(nextExpiry + Constants.MILLIS_PER_MINUTE));
    task.setProperty(ExpireGrantsTask.ITEM_ID_PROP_NAME, Integer.toString(item.getId()));
    try {
        ScheduledTaskManager.schedule(task);
    } catch (ServiceException e) {
        ZimbraLog.scheduler.warn("Error in scheduling task %s", task.toString(), e);
    }
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) ACL(com.zimbra.cs.mailbox.ACL) ScheduledTask(com.zimbra.cs.mailbox.ScheduledTask) Date(java.util.Date)

Aggregations

ScheduledTask (com.zimbra.cs.mailbox.ScheduledTask)3 ServiceException (com.zimbra.common.service.ServiceException)2 Date (java.util.Date)2 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)1 DbScheduledTask (com.zimbra.cs.db.DbScheduledTask)1 ACL (com.zimbra.cs.mailbox.ACL)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1