/// <summary> /// Sends the specified linqExpression for execution. /// </summary> /// <param name="linqExpression">The linqExpression.</param> /// <param name="timeOut">The time out.</param> /// <param name="data">The data.</param> /// <returns></returns> /// <remarks>Your expression must return a type of object, or the JSON serializer may throw casting errors</remarks> public async Task <IReceivedMessage <object> > SendAsync(LinqExpressionToRun linqExpression, TimeSpan timeOut, IAdditionalMessageData data = null) { var message = new MessageExpression(MessageExpressionPayloads.FunctionText, _compositeSerialization.InternalSerializer.ConvertToBytes(linqExpression)); return(await _queue.SendAsync(message, timeOut, data).ConfigureAwait(false)); }
/// <summary> /// Compiles the input linqExpression into a Linq expression tree /// </summary> /// <param name="linqExpression">The linqExpression.</param> /// <returns></returns> public Action <object, object> CompileAction(LinqExpressionToRun linqExpression) { using (_compileActionTimer.NewContext()) { return(_handler.CompileAction(linqExpression)); } }
/// <inheritdoc /> public IQueueOutputMessage Send(LinqExpressionToRun linqExpression, IAdditionalMessageData data = null) { var message = new MessageExpression(MessageExpressionPayloads.ActionText, _compositeSerialization.InternalSerializer.ConvertToBytes(linqExpression)); return(_queue.Send(message, data)); }
/// <summary> /// Compiles the input linqExpression into a Linq expression tree /// </summary> /// <param name="linqExpression">The linqExpression.</param> /// <returns></returns> public Action <object, object> CompileAction(LinqExpressionToRun linqExpression) { if (linqExpression.Unique) //don't bother caching { _counterActionCacheUnique.Increment(); return(_handler.CompileAction(linqExpression)); } var key = GenerateKey(linqExpression); var result = (Action <object, object>)_cache[key]; if (result != null) { _counterActionCacheHit.Increment(key); return(result); } result = _handler.CompileAction(linqExpression); if (!_cache.Exists(key)) { _counterActionCacheMiss.Increment(key); _cache.Add(key, result); _cache.Expire(key, ExpirationMode.Sliding, _itemPolicy.SlidingExpiration); } return(result); }
/// <summary> /// Adds the task. /// </summary> /// <typeparam name="TTransportInit">The type of the transport initialize.</typeparam> /// <param name="jobQueueCreation">The job queue creation.</param> /// <param name="name">The name.</param> /// <param name="queueConnection">Queue and connection information.</param> /// <param name="schedule">The schedule.</param> /// <param name="autoRun">if set to <c>true</c> [automatic run].</param> /// <param name="window">The window.</param> /// <param name="actionToRun">The action to run.</param> /// <param name="expressionToRun">The expression to run.</param> /// <param name="route">The route.</param> /// <param name="rawExpression">if set to <c>true</c> this expression will not be serialized. This will fail unless an in-process queue is being used.</param> /// <param name="producerConfiguration">The producer configuration.</param> /// <returns></returns> /// <exception cref="JobSchedulerException">Cannot add a task after Shutdown has been called.</exception> private ScheduledJob AddTaskImpl <TTransportInit>( IJobQueueCreation jobQueueCreation, string name, QueueConnection queueConnection, IJobSchedule schedule, bool autoRun, TimeSpan window, Expression <Action <IReceivedMessage <MessageExpression>, IWorkerNotification> > actionToRun, LinqExpressionToRun expressionToRun, string route, bool rawExpression, Action <QueueProducerConfiguration> producerConfiguration = null) where TTransportInit : ITransportInit, new() { Guard.NotNull(() => schedule, schedule); Guard.NotNullOrEmpty(() => name, name); ScheduledJob job; lock (_lockTasks) { if (IsShuttingDown) { throw new JobSchedulerException("Cannot add a task after Shutdown has been called."); } if (_tasks.ContainsKey(name)) { RemoveJob(name); } if (expressionToRun != null) { job = new ScheduledJob(this, name, schedule, _jobQueue.Get <TTransportInit>(jobQueueCreation, queueConnection, producerConfiguration), expressionToRun, _getTime.Create(), route) { Window = window, IsAttached = true }; _tasks.Add(name, job); } else { job = new ScheduledJob(this, name, schedule, _jobQueue.Get <TTransportInit>(jobQueueCreation, queueConnection, producerConfiguration), actionToRun, _getTime.Create(), route, rawExpression) { Window = window, IsAttached = true }; _tasks.Add(name, job); } } job.OnException += TaskOnOnException; job.OnEnQueue += JobOnOnEnQueue; job.OnNonFatalFailureEnQueue += JobOnOnNonFatalFailureEnQueue; if (autoRun) { job.StartSchedule(); } return(job); }
/// <summary> /// Compiles the input linqExpression into a Linq expression tree /// </summary> /// <param name="linqExpression">The linqExpression.</param> /// <returns></returns> public Action <object, object> CompileAction(LinqExpressionToRun linqExpression) { if (linqExpression.Unique) //don't bother caching { _counterActionCacheUnique.Increment(); return(_handler.CompileAction(linqExpression)); } var key = GenerateKey(linqExpression); return(_cacheActions.Execute(context => _handler.CompileAction(linqExpression), new Context(key))); }
/// <summary> /// Generates a key for the cache /// </summary> /// <param name="linqExpression">The linq expression.</param> /// <returns></returns> private string GenerateKey(LinqExpressionToRun linqExpression) { var builder = new StringBuilder(); builder.Append(linqExpression.Linq); foreach (var field in linqExpression.References) { builder.Append(field); builder.Append("|"); } foreach (var field in linqExpression.Usings) { builder.Append(field); builder.Append("|"); } return(builder.ToString()); }
internal ScheduledJob(JobScheduler scheduler, string name, IJobSchedule schedule, IProducerMethodJobQueue queue, LinqExpressionToRun expressionToRun, IGetTime time, string route ) { _scheduler = scheduler; Name = name; Schedule = schedule; _queue = queue; _expressionToRun = expressionToRun; _getTime = time; Route = route; }
/// <summary> /// Adds a new job or updates an existing job. /// </summary> /// <typeparam name="TTransportInit">The type of the transport initialize.</typeparam> /// <param name="jobQueueCreation">The job queue creation.</param> /// <param name="jobname">The jobname.</param> /// <param name="queueConnection">Queue and connection information.</param> /// <param name="schedule">The schedule.</param> /// <param name="actionToRun">The action to run.</param> /// <param name="route">The route.</param> /// <param name="producerConfiguration">The producer configuration.</param> /// <param name="autoRun">if set to <c>true</c> [automatic run].</param> /// <param name="window">The window.</param> /// <returns></returns> public IScheduledJob AddUpdateJob <TTransportInit>( IJobQueueCreation jobQueueCreation, string jobname, QueueConnection queueConnection, string schedule, LinqExpressionToRun actionToRun, string route = null, Action <QueueProducerConfiguration> producerConfiguration = null, bool autoRun = true, TimeSpan window = default) where TTransportInit : ITransportInit, new() { Guard.NotNullOrEmpty(() => schedule, schedule); Guard.IsValid(() => jobname, jobname, i => i.Length < 256, "The job name length must be 255 characters or less"); return(AddTaskImpl <TTransportInit>(jobQueueCreation, jobname, queueConnection, new JobSchedule(schedule, GetCurrentOffset), autoRun, window, null, actionToRun, route, false, producerConfiguration)); }
/// <summary> /// Compiles the input linqExpression into a Linq expression tree /// </summary> /// <param name="linqExpression">The linqExpression.</param> /// <returns></returns> public Action <object, object> CompileAction(LinqExpressionToRun linqExpression) { Guard.NotNull(() => linqExpression, linqExpression); Guard.NotNullOrEmpty(() => linqExpression.Linq, linqExpression.Linq); var compiler = _objectPool.GetObject(); try { return(compiler.CompileAction(linqExpression)); } catch (Exception error) { throw new CompileException($"Failed to compile linq expression {linqExpression.Linq}", error, linqExpression.Linq); } finally { _objectPool.ReturnObject(compiler); } }
/// <inheritdoc /> public async Task <IJobQueueOutputMessage> SendAsync(IScheduledJob job, DateTimeOffset scheduledTime, LinqExpressionToRun linqExpression) { if (!_started) { throw new DotNetWorkQueueException("Start must be called before sending jobs"); } return(await _sendJobToQueue.SendAsync(job, scheduledTime, linqExpression).ConfigureAwait(false)); }
/// <summary> /// Sends the specified linqExpression for execution. /// </summary> /// <param name="linqExpression">The linqExpression.</param> /// <param name="timeOut">The time out.</param> /// <param name="data">The data.</param> /// <returns></returns> /// <remarks>Your expression must return a type of object, or the JSON serializer may throw casting errors</remarks> public IReceivedMessage <object> Send(LinqExpressionToRun linqExpression, TimeSpan timeOut, IAdditionalMessageData data = null) { var message = new MessageExpression(MessageExpressionPayloads.FunctionText, _compositeSerialization.InternalSerializer.ConvertToBytes(linqExpression)); return(_queue.Send(message, timeOut, data)); }
/// <summary> /// Sends the specified dynamic linqExpression to be executed. /// </summary> /// <param name="job">The job.</param> /// <param name="scheduledTime">The scheduled time.</param> /// <param name="linqExpression">The linqExpression to execute.</param> /// <returns></returns> public async Task <IJobQueueOutputMessage> SendAsync(IScheduledJob job, DateTimeOffset scheduledTime, LinqExpressionToRun linqExpression) { using (IScope scope = _tracer.BuildSpan("SendJobAsync").StartActive(finishSpanOnDispose: true)) { scope.Span.SetTag("JobName", job.Name); return(await _handler.SendAsync(job, scheduledTime, linqExpression)); } }
/// <summary> /// Compiles the input linqExpression into a Linq expression tree /// </summary> /// <param name="linqExpression">The linqExpression.</param> /// <returns></returns> public Action <object, object> CompileAction(LinqExpressionToRun linqExpression) { _compiler.References = DefaultReferences.Union(linqExpression.References).ToArray(); _compiler.Usings = DefaultUsing.Union(linqExpression.Usings).ToArray(); return(_compiler.ParseLambdaExpr <Action <object, object> >(linqExpression.Linq).Compile()); }
/// <summary> /// Sends the specified dynamic linqExpression to be executed. /// </summary> /// <param name="job">The job.</param> /// <param name="scheduledTime">The scheduled time.</param> /// <param name="linqExpression">The linqExpression to execute.</param> /// <returns></returns> public async Task <IJobQueueOutputMessage> SendAsync(IScheduledJob job, DateTimeOffset scheduledTime, LinqExpressionToRun linqExpression) { using (var scope = _tracer.StartActivity("SendJobAsync")) { scope?.SetTag("JobName", job.Name); return(await _handler.SendAsync(job, scheduledTime, linqExpression)); } }
/// <inheritdoc /> public async Task <IJobQueueOutputMessage> SendAsync(IScheduledJob job, DateTimeOffset scheduledTime, LinqExpressionToRun expressionToRun) { var messageData = new AdditionalMessageData(); var data = StartSend(job, scheduledTime, messageData); if (data != null) { return(data); } var message = await Queue.SendAsync(expressionToRun, messageData).ConfigureAwait(false); var result = ProcessResult(job, scheduledTime, message); if (result != null) { return(result); } //try one more time result = ProcessResult(job, scheduledTime, await Queue.SendAsync(expressionToRun, messageData).ConfigureAwait(false)); return(result ?? new JobQueueOutputMessage(JobQueuedStatus.Failed)); }
/// <inheritdoc /> public async Task <IQueueOutputMessage> SendAsync(LinqExpressionToRun linqExpression, IAdditionalMessageData data = null) { var message = new MessageExpression(MessageExpressionPayloads.ActionText, _compositeSerialization.InternalSerializer.ConvertToBytes(linqExpression)); return(await _queue.SendAsync(message, data).ConfigureAwait(false)); }