public override StateData GetStateData(string jobId)
        {
            if (jobId == null)
            {
                throw new ArgumentNullException("jobId");
            }
            var converter = StringToInt64Converter.Convert(jobId);

            if (!converter.Valid)
            {
                return(null);
            }
            return(Storage.UseSession(session =>
            {
                var job = session.Query <_Job>()
                          .Where(i => i.Id == converter.Value)
                          .Select(i => new { i.StateName, i.StateData, i.StateReason })
                          .SingleOrDefault();
                if (job == null)
                {
                    return null;
                }


                return new StateData
                {
                    Name = job.StateName,
                    Reason = job.StateReason,
                    Data = new Dictionary <string, string>(
                        JobHelper.FromJson <Dictionary <string, string> >(job.StateData),
                        StringComparer.OrdinalIgnoreCase)
                };
            }));
        }
        public override void SetJobParameter(string id, string name, string value)
        {
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            var converter = StringToInt64Converter.Convert(id);

            if (!converter.Valid)
            {
                return;
            }
            Storage.UseSession(session =>
            {
                session.UpsertEntity <_JobParameter>(i => i.Id == converter.Value,
                                                     i => i.Value = value, i =>
                {
                    i.Job = new _Job {
                        Id = converter.Value
                    };
                    i.Name = name;
                });
                ;
            });
        }
        public override JobData GetJobData(string jobId)
        {
            if (jobId == null)
            {
                throw new ArgumentNullException("jobId");
            }
            var converter = StringToInt64Converter.Convert(jobId);

            if (!converter.Valid)
            {
                return(null);
            }
            Logger.InfoFormat("Get job data for job '{0}'", jobId);

            return(Storage.UseSession(session =>
            {
                var jobData =
                    session
                    .Query <_Job>()
                    .SingleOrDefault(i => i.Id == converter.Value);

                if (jobData == null)
                {
                    return null;
                }

                var invocationData = JobHelper.FromJson <InvocationData>(jobData.InvocationData);
                invocationData.Arguments = jobData.Arguments;

                Job job = null;
                JobLoadException loadException = null;

                try
                {
                    job = invocationData.Deserialize();
                }
                catch (JobLoadException ex)
                {
                    loadException = ex;
                }

                return new JobData
                {
                    Job = job,
                    State = jobData.StateName,
                    CreatedAt = jobData.CreatedAt,
                    LoadException = loadException
                };
            }));
        }
        public override void PersistJob(string jobId)
        {
            Logger.TraceFormat("PersistJob jobId={0}", jobId);
            var converter = StringToInt64Converter.Convert(jobId);

            if (!converter.Valid)
            {
                return;
            }
            AcquireJobLock();

            QueueCommand(session =>
                         session.CreateQuery(SqlUtil.UpdateJobExpireAtStatement)
                         .SetParameter(SqlUtil.ValueParameterName, null)
                         .SetParameter(SqlUtil.IdParameterName, converter.Value)
                         .ExecuteUpdate());
        }
        public override void ExpireJob(string jobId, TimeSpan expireIn)
        {
            Logger.TraceFormat("ExpireJob jobId={0}", jobId);
            var converter = StringToInt64Converter.Convert(jobId);

            if (!converter.Valid)
            {
                return;
            }
            AcquireJobLock();

            QueueCommand(session =>
                         session.CreateQuery(SqlUtil.UpdateJobExpireAtStatement)
                         .SetParameter(SqlUtil.IdParameterName, converter.Value)
                         .SetParameter(SqlUtil.ValueParameterName, session.Storage.UtcNow.Add(expireIn))
                         .ExecuteUpdate());
        }
        public override string GetJobParameter(string id, string name)
        {
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }
            var converter = StringToInt64Converter.Convert(id);

            if (!converter.Valid)
            {
                return(null);
            }
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            return(Storage.UseSession(session =>
                                      session.Query <_JobParameter>()
                                      .Where(i => i.Job.Id == converter.Value && i.Name == name)
                                      .Select(i => i.Value)
                                      .SingleOrDefault()));
        }
        public override void SetJobState(string jobId, IState state)
        {
            Logger.TraceFormat("SetJobState jobId={0}", jobId);
            var converter = StringToInt64Converter.Convert(jobId);

            if (!converter.Valid)
            {
                return;
            }
            AcquireStateLock();
            AcquireJobLock();
            QueueCommand(session =>
            {
                var job = session.Query <_Job>().SingleOrDefault(i => i.Id == converter.Value);
                if (job != null)
                {
                    var jobState = new _JobState
                    {
                        Job       = job,
                        Reason    = state.Reason,
                        Name      = state.Name,
                        CreatedAt = session.Storage.UtcNow,
                        Data      = JobHelper.ToJson(state.SerializeData())
                    };
                    session.Insert(jobState);
                    session.Flush();

                    job.StateData          = jobState.Data;
                    job.StateReason        = jobState.Reason;
                    job.StateName          = jobState.Name;
                    job.LastStateChangedAt = session.Storage.UtcNow;

                    session.Update(job);
                    session.Flush();
                }
            });
        }
        public override void AddJobState(string jobId, IState state)
        {
            Logger.TraceFormat("AddJobState jobId={0}, state={1}", jobId, state);
            var converter = StringToInt64Converter.Convert(jobId);

            if (!converter.Valid)
            {
                return;
            }
            AcquireStateLock();
            QueueCommand(session =>
            {
                session.Insert(new _JobState
                {
                    Job = new _Job {
                        Id = converter.Value
                    },
                    Name      = state.Name,
                    Reason    = state.Reason,
                    CreatedAt = session.Storage.UtcNow,
                    Data      = JobHelper.ToJson(state.SerializeData())
                });
            });
        }